Earn points by posting questions, comments and answers to the CS320 BBS.
Now try to make each one run (1) as an application and (2) as an Applet in an HTML document.
Compare your theories with the results and (if necessary) modify your theories.
Subprogram Parameters in Java
Java is designed to make it easy to figure out a method call:
O = IO.name(I);O is the output, IO is handled in in-out mode, and I are all input parameters.
Here are some rules about Java...
Java Command Line arguments and Arrays
First study the file
[ Echoes.java ]
and try to reason out what it will do when compiled(giving Echoes.class)
and run as an application like this
java Echoes this is a testModify the code so that it outputs the same data in the reverse order. Hint?: [ Array in java.classes ]
Simple Graphics
Here is a page with a rather nice looking mathematical curve:
[ test.Henrici.html ]
You need to get copies of the source code for this page and
the source code for the applet (in Java) so that you can
experiment with it. On your workstation download/save
the code(as text) and the page(as source). Then use
FTP to copy these to a Sun (Orion). Make sure that
Orion's DISPLAY is set to your machine:0, and your machine
will host Orion's displays(xhost orion). On Orion, you should
now be able to compile the java code( use Q), and point Sun's
Applet viewer at the page... Then you should be able (on Orion)
to publish the page, code, documentation, and class:
publish *Henrici*Then you can point your workstation's Netscape at the published page.
Explore the documentation for Henrici
You should have generated and published the documentation
of the Henrici class. Try out some of the links in this page.
Also look in [ java.class.tree.html ] and [ java.classes.html ] to find out more about some of the classes used in Henrici.java.
Experiment with Graphics
Then modify Henrici.java in some way, and use
Sun's Appletviewer to test it...
Repeat this until you like the result
and publish the page, the class, the code and the documentation.
Link you public index and/or home page to the result.
Disclaimer... the code for Henrici does not take advantage of
objects to any great extent. There are lots of good stylistic changes you
can make as well!
Extending an Existing Class
In C++ you "derive a class C from a base class B". In Java you "extend
class B to give class C". In Java all public parts of a class are
also present in its extensions.... but some parts can be overwritten.
Examine the following code and documentation that constructs a (useless) hierarchy of extensions. See the code:
[ Extension.java ]
and documentation of the 4 classes in it:
[ Extension.html ]
[ Extension2.html ]
[ Extension3.html ]
[ Extension4.html ]
Notice how the documentation shows you the hierarchy and also
links each method to any method it over-rides.
Get a copy of the code [ Extension.java ] First look at it and then compile and run it. Find examples of (1) an inherited method, (2) an over-ridden method, (3) passing a constructor up to the parent(super), (4) a class which can not have any objects unless they are objects of its children(abstract).
Also notice that the variables e2,e3,e4 are declared as
type extension, but are actually instances of subclasses...
and they know what they really are!
Objects know who and what they are
C++ has "virtual functions" but Java does not need them. As a
rule if you have some code like this:
class MyExtension extends MyClass{ ....public MyExtension(...) }
and
do something like this:
MyClass myVar = new MyExtension(....);then myVar is an in both classes. myVar behaves both like a MyClass and like a MyExtension. Most important myVar can tell at run time that it is an instance of MyExtension.
Start by studying the following documentation for a generic class of Things: [ Thing.html ] The code is in [ polymorphic.java ]
Get a copy of the code and compile and run it. Make changes to the main function that help you see what is going on.
Graphic Classes: Buttons, Panels, and Texts
In Java there are many ready made classes for building Graphical
User Interfaces(GUIs). Java has classes for Buttons, menus(Choice),
Graphics(Canvas), windows(Frame), ...
and text objects(TextArea,...). You can assemble these into complicated
interface. There is a rather clever scheme
that lets Java layout the components of a graphic for you.
There are classes (LayoutManagers) that control the layout of the parts
within a complex object.
This means you don't have to work out the precise positions of buttons
and menus on the screen. (Especially as the screen is on a strange computer
running a weird OS in another galaxy far away...)
The following is a classic GUI... the user is given a box with text inside it that they can edit. When Happy they click the OK button and something happens... else the push the 'No' button and all the changes are undone. Study how the Java code assembles the picture piece by piece... [ TextDemo.java ] and [ testTextDemo.html ]
Using these classes makes GUI work easier: for example (1) On each platform the same code fits the look-and-feel of that platform and (2) the pieces will adjust themselves if the user changes the size of the window.
Text Input and Output
It is very easy to let the user edit an area of text on the screen,
read the result and react to a button push. See
[ testTextDemo.html ]
First try it out, then look at the documentation and the
code... try figure out how I use the ready made Java classes
to create a simple user interface.
In the next lab You will be asked to modify this applet in some
way or other.
Vectors
Use the documentation provided by Sun.... find out what a Vector
is and how it differs from an array. Start your search from
[ java.classes.html ]
and search for arrays and vectors.
Quick Sort Algorithm in Java
Here is a much more complex application. It one of a series
of similar programs I developed to teach myself Java.
Save or download the following application, compile it, document it, and run it. [ qsort3.java ] This in one of a series of experiments that I tried while learning Java. [ Qsort in index ]
Modify it so that it sorts a different list of Strings. Can you change it so that it sorts a different type of Object or a data type?
Publish Your Programs
You must now publish the code, classes, test pages:
publish *.java *.classes test*htmlPrepare a page that lets people browse the code, and see the Applets... Publish this and link it into your Program Languages page.
You may wonder if intefaces are useful. You can look at the list of ready-made classes and intterfaces in [ java.class.tree.html ] You will see many of the items are interfaces not classes. Each time you see the word implements you see an interface being used. Here are two interfaces thet are worth studying:
LayoutManager
AudioClipThe first has several different implementations for different layouts. The second is defined, but there is no implementation provided...
Interfaces let you describe how to use a set of methods, but they do not let you declare complete methods. To that you need a class (not an inteface). Here is an example of an interface, a generic class that implements it, and a class that then tests a special case: [ Order.java ] Download, compile, document and study the results...
Parameter work arounds
In Java there are no out or in-out parameters.
The effect of a single out para is easy to get:
by returning it as the result of the method. You can get the effect
of a set of out parameters by returning an object with one field(piece of
data) for each parameter.
Outparas=object.method(Inparas);
Outpara1=Outparas.outpara1();
Outpara2=Outparas.outpara2();
To get the effect of an inout parameter you can change the method so that the parameter is the object to which the method is applied:
IO.method(in mode parameters);
Or you can use an explicit call_by_value_result:
IOpara=something.method(IOpara);
Invent an example of this kind of coding.
. . . . . . . . . ( end of section CS320: Second Java Laboratory) <<Contents | End>>