[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] /[CS320 Course Materials] [Text Version] lab/16.html Thu May 26 14:08:54 PDT 2011
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]


    CS320: Second Java Laboratory -- Applications

      Check out New things on the Course Web Page

      [ http://cse.csusb.edu/dick/cs320/ ]


      Your task is to try some the experiments on this page. and write a page of notes on how Java Applications and Objects work.


      Show me your notes and code on your web site at the end of the session.

      No Applets are needed in this laboratory.


      1. (Applications): A Java application is a class with a public main method Applications run from a terminal window using the command: java nameOfClass. Applications only run on a web page if they are also public Applets.
      2. When you compile a class it can be used in applications compiled in the same directory. You can extended it etc. No need for "import"!
      3. Review your previous lab page.
      4. If stuck look in your handout, [ Java.htm ] , my samples [ java.html ] , [ ../java/ ] (examples) and [ Java in resources ] (links to documentation).

      Review: Applications and Applets

      Here are three Java files: [ ../java/A.java ] [ ../java/B.java ] [ ../java/C.java ] Try to answer the following questions without running the code:
      1. Which files define applications?
      2. Which of these define Applets?
      3. Which contains both an application and an Applet?

      Note down your answers - best guess will do. Plus your reasons (theories). If in doubt see [ Overview in java ] and [ Applications Can Run Applets in java ]

      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. However if I has a Object then it is passed be reference and methods can be applied to it to change it.

      Here are some rules about Java...

      1. (Rule 0): All functions belong to an object or to a class. No naked functions.
      2. (Rule 1): Parameters of primitive types (int ...) are passed by value, only.
      3. (Rule 2): Objects are references. A method can change the content of an Object. Afterwards the argument refers to the same object with different data in it.
      4. (Rule 3): Parameters and results are single values or Objects but an object can contain many parts.
      5. (Rule 4): No IO parameters. But you can fake them be declaring a class of References.
      6. (Rule 5): If you want a subprogram that treats an object as in-out then the subprogram can be declared as method in that objects class.
      7. (Rule 6): No function parameters. But you can fake it by declaring a class of functions.

      Here are some example programs that to some extent demonstrate the above rules:
      1. The NakedGun File [ ../java/NakedGun.java ]
      2. The X Files [ ../java/X.java ]
      3. The Why Files [ ../java/Why.java ]
      4. The Judge Program [ ../java/Paras.java ]
      5. The Refs Program [ ../java/Refs.java ]
      6. The Fun Program [ ../java/Fun.java ]

      Your mission, should you choose to except it, is to find out which of the above rules are illustrated by which of the above programs. Hint: There may not be a unique correspondence, and some rules tell you that some of the above can not compile.

      Java Command Line arguments and Arrays

      First study the file [ ../java/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 test
      Modify the code so that it outputs the same data in the reverse order.

      Experiment with Graphics

      Download [ ../java/Henrici.java ] , compile, and run it. Don't try to understand it, yet...

      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. [ ../java/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: [ ../java/Thing.html ] The code is in
      Download a copy of the code (Shift click): [ ../java/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.

      Non-Java Concurrency

        Prime Number Sieve

        I have developed some examples of various kinds of concurrency in Java and other languages using Tony Hoare's Prime Number Sieve [ Specification ]

        You can see implementation in the UNIX shell, Ada, C, C++, Java, etc, in [ http://cse.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>>


        GUI 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... [ ../java/TextDemo.java ] and here is a test page [ ../java/testTextDemo.html ]

        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:
        1. A fun example [ rice.cc ]
        2. A detailed example of the difference between virtual and non-virtual functions in complex hierarchies: [ vf.cc ] [ virtual_fun.cc ]
        3. Run Time Type Information in C++ allows an object to know what type of thing it is [ rtti.cc ] this can also be linked to Source Code Control Indentification(SCCI) [ vn.cc ]

        Translate C++ to Java

        Think about what happens when you try to translate the following C++ program [ vmf.cc ] into Java.

        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. [ ../java/qsort3.java ] This in one of a series of experiments that I tried while learning Java. [ ../java/index.html#Qsort ]

        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 ]