Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CSci620] >> lab12 [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13]
Thu May 6 07:24:26 PDT 2004


    CS620: Second Java Laboratory -- Applications

      Check out New things on the Course Web Page

      [ ]


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


      Show me your notes at the end of the session. Bring questions to the next class.


      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 [ lab11.html ] first.
      4. If stuck look in your handout, [ Java.htm ] , my samples [ java.html ] and [ ] (examples).

      Review: Applications and Applets

      Here are three Java files: [ ] [ ] [ ] 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 =;
      O is the output, IO is handled in in-out mode, and I are all input parameters.

      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 [ ]
      2. The X Files [ ]
      3. The Why Files [ ]
      4. The Judge Program [ ]
      5. The Refs Program [ ]
      6. The Fun Program [ ]

      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 [ ] 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. Hint?: [ Array in java.classes ]

      Change Your HelloWorld Applet

      Can you figure out how to change HelloWorld in some way... a longer message? A different position on the Applet's window? an interesting color? a different font/typeface/pointsize? or some such.... Can you duplicate the first demonstration program with out copying its code?

      Experiment with Graphics

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

      Then modify 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. [ ] 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 experiements (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
      Download a copy of the code (Shift click): [ ] 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.

      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... [ ]

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

        Translate C++ to Java

        Think about what happens when you try to translate the following C++ program [ ] 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. [ ] 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?

        The CS620 textbook stack

        Input the books code defining and testing stacks. Compile, correct, tun, test, and correct (if needed).

        Can you put Strings on these stacks?

        The CS620 nonrecursive parser

        For masochists with nothing better to do.... input and test the nonrecursive parser. Give me any errors....

      . . . . . . . . . ( end of section Optional) <<Contents | Index>>

    . . . . . . . . . ( end of section CS620: Second Java Laboratory -- Applications) <<Contents | Index>>


  1. BNF::="Backus-Naur Form", for syntax and grammar, developed by Backus and Naur.
  2. EBNF::="Extended " BNF.
  3. HTML::= "HyperText Markup Language", used on the WWW.
  4. HTML_page::syntax= "<HTML>" head body.
  5. Java::="An " OO " Language from Sun".
  6. LISP::= "LISt Processing Language".
  7. LRM::="Language Reference Manual".
  8. OO::="Object-Oriented".
  9. Prolog::="Programming in Logic".
  10. TBA::="To Be Announced".
  11. UML::="Unified Modeling Language".
  12. URL::=Universal_Resource_Locator,
  13. Universal_Resource_Locator::syntax= protocol ":" location, where
    1. protocol::= "http" | "ftp" | "mailto" | ... ,
    2. location::= O( "//" host) O(pathname).

    (End of Net)
  14. WWW::= See, index to web site for this class.
  15. XBNF::="eXtreme" BNF, developed by the teacher from EBNF, designed to ASCII input of syntax, semantics, and other formal specifications.

Formulae and Definitions in Alphabetical Order