Goals
Your task is to try some the experiments on this page.
and write a page of notes on how Java Applications and Objects work.
Deliverables
Show me your notes and code on your web site
at the end of the session.
No Applets are needed in this laboratory.
Hints
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.
Then modify Henrici.java in some way, and see what happens. Repeat this until you like 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!
Polymorphism Basics
Here is some bad news: You won't get far with Java without understanding polymorphism.
The good news is that in Java it is simple and intuitive: Objects know
who they are and automatically behave in character. When a variable refers to
an object then all operations applied to that variable automatically are
taken form that objects class.
Here is a nice example. [ Crispy.java ] that you can look at, download, compile, and execute on your work station.
C++ has "virtual functions" but Java does not need them. If you want to review C++ see the optional experiments (Optional) below.
Nearly all Java methods are "virtual! 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 in both classes. myVar behaves both like a MyClass and like a MyExtension. Given the choice (when the same method is in both classes) it will choose MyExtension. Most important myVar can tell at run time that it is an instance of MyExtension.
Polymorphism Review
If you need to review polymorphism see
[ Polymorphism in cs320wuml ]
and then do the following erxperiment.
More Polymorphism
Study the following documentation for a useless but generic class
of Things:
[ Thing.html ]
The code is in
http://ftp.csci.csusb.edu/public/faculty/dick/Thing.javaDownload a copy of the code (Shift click): [ Thing.java ] and compile and run Doit. Make changes to the main function in class Doit that help you see what is going on.
Note. Newer versions of Java have Bytes, and other new classes and classes and methods that extract runtime information about the class of an object -- like what methods it has.
Threads in Java
Have a look at the Sun documentation on Threads in Java:
[ Thread.html ]
Also follow some of the links on this page.
You can see implementation in the UNIX shell, Ada,
C, C++, Java, etc, in
[ http://csci.csusb.edu/dick/cs320/sieve/ ]
The Cryptogram Problem
Here is another problem that has a clean solution
as two concurrent processes:
[ Specification ]
plus a sample of test data
[ test.dat ]
and the design
[ Design ]
using XBNF.
The code includes a Makefile [ Makefile ] , to process that can be independently compiled, and run using a pipe to connect them ( p1 | p2 ): [ p1.c ] [ p2.c ]
It is easy to convert p2.c into a restartable function: [ p2s.c ] and so produce [ crypto.c ] or you can use the UNIX C pipe() and fork() functions [ cryptop.c ] to get two concurrent processes.
In Ada it is easy to create a main program [ crypto.ada ] that has a package with two tasks P1 and P2.
You might like to develop a Java translation of the original design.
. . . . . . . . . ( end of section Non-Java Concurrency) <<Contents | End>>
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 ]
First try it out, then look at the code... try figure out how I use the ready made Java classes to create a simple user interface.
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.
Try modifying this application in some way or other.
For example, When I wanted to test using HTTP in Java I extended TextDemo so that something(...) accessed a given URL.
Review C++ Objects
C++ provides a confusing set of possible ways to describe
a function. One of these is the word "virtual".
Here are some programs
designed to demonstrate it:
The 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?
. . . . . . . . . ( end of section Optional) <<Contents | End>>
. . . . . . . . . ( end of section CS320: Second Java Laboratory -- Applications) <<Contents | End>>
Check the Preparation for next class
[ ../17.html ]