This page is part of the CS320: Programming Languages Course on the Computer Science Department's WWW Server at CalState, San Bernardino, California, USA. It was generated by Doc Dick Botting. Please use lookup320 to search for information on CS320.


    CS320: First Java Laboratory: Introduction & Elements


      Two points for each comment about Java/HTML/etc on the BBS. Aim to get some Java Code, Applets, and documentation into your portfolio before the final... and ideally before that.

      Essential Lab Work

        In this lab you will learn how to do simple 'applets' like the demonstration below using the Sun Java Development Kit and some local tools.

        Demonstration 1

        Here [ test.Goodbye.html ] is a simple Java applet in an HTML page. The page also has links to the Code I wrote and the documentation and bytecode that I prepared on Orion from it. Follow the links to the Source code, Documentation and Byte Code.

        Demonstration 2

        Here [ test.Henrici.html ] is a demonstration of something that was easy to program in Java. Have a look at it.... the graphic is produced by the Java applet in the HTML page.


        I have gathered together a simple tutorial and reference to Java. Spend 5 or 6 minutes looking at the sections called:
         		Java at CSCI.CSUSB.EDU
        in [ java.html ]

        Your First Java Program

        Here you will need to have two windows working at one time: One running a brower (say Netscape on your SGI) and the other to edit, compile, and run code on Orion.

        To open another window and use it to work on Orion we need a standard xterm that executes (-e) a rlogin orion command:

         		xterm -e rlogin orion &
        (without closing down Netscape!)

        Use your browser window to look at the program below:

         	import java.lang.*;
         	public class Hello {
         	    public static void main(String argv[]){
         	        System.out.println("Hello, World!");
        In the Orion window use your favorite editor to create a copy of the program in a file called.
        Notice: not, not, but This is important!

        Compile it like this in your Orion window:


        1. If the above worked you now have a file called 'Hello.class'. It is a binary file. Do not try editting or listing it!
        2. If the 'javac...' doesn't work you may have a typographical error... First check that you did type 'javac' as shown. Then try downloading [ ] as Source into a file called "" and try again.

        Run the compiled class like this:

         		 java Hello

        1. 'java' and 'java Hello.class' do not work!
        2. The file is compiled, but a class in interpreted.
        3. Java code was designed to be interpreted by a machine independent virtual machine that runs bytecode.
        4. bytecode::= See

        Your Second Java Application

        Modify your program -- using your favorite editor on Orion -- (use the same name for the file and the class -- to output a banner with your name on it. Then try out my quickie or Q program to compile, document and run your program on Orion.

        Now see what new files you have:

         		ls -ltr | tail
        The "Hello.html" file is documentation generated by 'javadoc'.

        A Third Silly Application

        The documentation for the "Luv" example is at [ Luv.html ]

        Study the documentation a bit before you get your own copy of the source code [ ] Compile and test the application.

        Experiment with the UNIX 'file' and the Java disassembler tool:

         		file Luv.class
         		javap Luv
        Note.... javap uses the class name(Luv) not a file name(Luv.class).

        Properties of our Java System

        Get a copy of an application that prints out the properties of our Java interpreter etc: [ ]

        Study it, compile it, run it.

        Now is the time to investigate all the classes you have created in this session

         		ls -ltr *.class

        Also generate documentation for PrintProperties and Delay:

        		~dick/bin/fix.javadoc PrintProperties
        		~dick/bin/fix.javadoc Delay
        Then use Lynx on Orion to look at the documentation:
         		lynx PrintProperties.html
         		lynx Delay.html
        Or you can publish the documentation and classes using the publish command on Orion and use Netscape on an SGI to look at them... You could link the documentation into a public page very easily....

      Dates in Java

      We looked at how you would develop a record for handling dates (calendar not deglet noor!) in LISP and Prolog. Java comes with a large number of predefined classes including one called Date. It was used in the Properties application:
       		System.out.println(new Date());
      Now System.println doesn't know what to do with a Date! Instead it sees an Object of unknown type and sends it the toString() message. The Date does know how to represent itself as a String! This String is printed by System.println.

      Here is a pointer to Sun's official documentation on class Date: "" [ _top_ in java.util.Date ] Find out what else you can do with Dates.... and try some of these functions/operation out in a program...

      Strings in Java

      With luck you noticed in one of the last to experiments that Java allows you to add two strings:
                               "Total Memory = "
                               + rt.totalMemory()
                               + " Free Memory = "
                               + rt.freeMemory()
      Given the following clues, can you find out what else can be done with strings?
      1. There are important classes called String and StringBuffer [ java.class.tree.html ]

      Again develop a small experimental application that will remind you of some of the things you can do with Strings and/or StringBuffers.

      Your First Applet

      Your task is to construct a public WWW page that has an applet that display some text. The name of the page is:
      and it should contain the following HTML:
       		<head><title>Test Hello World</title></head><body>
                <APPLET CODE="HelloWorld.class" HEIGHT=150 WIDTH=150>
       		You can not see this brilliant Java Applet.
      You now need a file called:
       	import java.applet.*;
       	import java.awt.*;
      	public class HelloWorld extends Applet {
      	      public void init() {
      	      public void paint(Graphics g) {
      	          g.drawString("Hello world!", 50, 25);

      The Applet is compiled just like any other program:

      This will generate a file with "HelloWorld.class". Notice that the compiler forces you to name the file
      and the Class
      must be public for it to be used over the World Wide Web.

      Here is how you use can use Sun Apletviewer to test the HelloWorld applet:

       		appletviewer test.HelloWorld.html

      1. If you get complaints about not being able to open a display or contact a host.... see The CS Dept FAQs: [ How do I access one Machine from another in CS_FAQ ]
        1. On your workstation give it permission to display screens from Orion
           		xhost orion
          Then find out its your workstations name:
        2. Then tell Orion where to display things:
           		export DISPLAY=your_workstations_name:0

      A Second Applet

      Your second task is to change your program to output the result in color by adding
      to Compile and then use the Applet Viewer to test the result.

      Next change the font in your graphic:

       		g.setFont(new Font("Helvetica", Font.BOLD, 24));

      If you publish the page and the class then you can use Netscape on an SGI to look at them...

      1. If Netscape doesn't show your Applet... Look under Netscape's Option menu and set the "Show Java Console" option. This will show any errors and also any System.out messages.

      2. Netscape does not make a useful tool when developing Java applets for one simple reason. Netscape does not want to reload you Java Applet's compiled code. The reload button makes no difference! Sometimes a SHift or Control click on the reload button may work. Personally I include a test harness in the class itself, as follows.

      An Application that runs an Applet!

      A Application is a class with a 'main' function. An Applet needs a graphic window to run in..... so if we write a main function for HelloWorld that creates a graphic window then it can run the Applet. Get a copy of this: [ ] and compile and test it like this:
       		java HelloWorld
      Once this runs ok, change the color and form of the graphic string. Make this run.
      1. Adding a suitable main function is a viable development technique. However Java does not make it easy to add a function to a class.
      2. It is worth keeping 'main' in a special file and learning hown to read files into Java coulde using your favorite editor.

      Generate Documentation

      Run these two commands:
       		~dick/bin/fix.javadoc HelloWorld
      and you create a file "HelloWorld.html" that documents the program... complete with links to documentation on what String and Applet, etc. are.

      Publish Your Work

      Edit test.HelloWorld.html so that is has a linkx to the Source code and documentation after the end of the applet:
       		<a href="">Source Code</A>
       		<a href="HelloWorld.html">Documentation</A>
      and publish all the files:
       		publish *HelloWorld*
      Now use Netscape to look at the page... Follow the links to the code and then to the documentation, and from there to the documentation on String.

      A WWW scavenger hunt

      Explore the rest of my java.html pages: [ java.html ] and my collection of Java WWW sites: [ java.www.sites.html ]
      1. Hint: Use the Netscape Find Button

      Can you find the following: If you do and nobody else has posted it.... post the URL to the Class BBS.

    . . . . . . . . . ( end of section Essential Lab Work) <<Contents | End>>

    If you have time

      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?


      Use the documentation provided by Sun.... (find links in [ java.html ] ) find out what a Vector is and how it differs from an array.

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

      Modify it so that it sorts a different list of Strings. Perhaps compare it with some other qsort[0..7].java in the same directory? [ ]

    . . . . . . . . . ( end of section If you have time) <<Contents | End>>

. . . . . . . . . ( end of section CS320: First Java Laboratory) <<Contents | End>>