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


    CS620: First Java Laboratory

      Check out New things on the Course Web Page

      [ ]


      Your task is to try some the experiments on this page.


      1. Keep your Java handout handy.
      2. Put an applet called AaAa in file not in a file called
      3. See [ java.html ] and [ ]

      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, the documentation, and bytecode that I prepared from it. Follow the links to the Source code, Documentation and Byte Code.

      You can use this page as a model for your own examples of Java Applets.

      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. You can also follow the two links to the Java source code and to the documentation generated by the source code.

      Note. You don't have to understand why this picture is called an Henrici spiral or what the mathematics means. Its just a formula that generates rather nice looking graphics -- enjoy it.

      Your First Java Program

      Here you will need to have two windows working at one time: One running a browser (say Netscape on your workstation) and the other to edit, compile, and run commands in the Java Development Kit(JDK). The various versions can be listed by inputing the UNIX command into a command/terminal window:
       		ls -ld /share/j*
      is linked to the last of the reliable ones!

      In your browser window look at this:

       	import java.lang.*;
       	public class Hello {
       	    public static void main(String argv[]){
       	        System.out.println("Hello, World!");
      It is a traditional first C program written in Java.

      Use your favorite editor to create a copy of the program above in a file called.

      Notice: not, not, but This is important!

      Compile it like this:

      Note: Do not hold your breath waiting for this to compile, the compiler was very slow when I test it.
        I theory you should be able to write
        but this did not work for me recently.

      1. Make sure your PATH variable has /share/java1.4.1/bin added to it when you next plan to use Java.
      2. To add /share/j2sdk1.4.1/bin to your UNIX shell PATH
          edit your ~/.bash_profile file so that the PATH is defined like this

        (End of Net)
        This is read whenever you login or open a terminal window.

      (End of Net)
    1. If the above worked you now have a file called 'Hello.class'. It is a binary file. Do not try editing 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:

     		/share/java1.4.1/bin/java Hello

    1. 'java' and 'java Hello.class' do not work!
    2. The source is compiled, but a class is 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 -- to output a message with your name on it. (use the same name for the file and the class) Then try out my quickie or Q program to compile, document and run your program.

      I named the program for a character played by John De Lancey in StarTrek: The Next Generation, my quickie program is quirky and powerful. It has information about compiling and interpreting many languages and codes on our servers and workstations.

      If your shell reports that 'Q can not be found', then you can

      1. Use
      2. Add /share/bin to your UNIX shell PATH inside your .bash_profile

    A Third Silly Application

    This is an application that was used to teach me what a loop was, back in the 1960s. It also shows how Java Input/Output works. 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.

    A Modern Version

    These days users expect to point and click rather than input text to answer questions. To see how the previous program looks as an event-driven applet follow this link: [ test.Luv5.html ] While there follow the links to the Code and Documentation.

    Your First Applet

    Your task is to construct a public WWW page that has an applet that displays 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.

    Alternately you can copy all the *HelloWorld* files to your public directory and view them with Netscape..... BUT make sure it is Netscape 6.* not 4.* on your workstation. Or else try Konqueror or Mozilla (and turn on the Java security and console).


    1. Java is supposed to offer: Write Once, Run Anywhere.
    2. What you get is: Write once, Debug Everywhere

    (End of Net)

    A Second Applet

    Your next task is to change your program to output the result in color by adding
    to Compile and then publish to check the result. Make sure you shut netscape down and restart it.... otherwise you'll probably get the OLD cached version:-(

    Next change the font in your graphic:

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

    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 your 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!

    An 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 how to read files into Java code using your favorite editor.

    Publishing Your Applet

    Don't forget that you must always publish the *.class files for all Applets on your web pages... and all the classes that they call. You should always make the documentation available on any class you publish so that others can use it. In this class you also need to publish the source code so that It can be graded! You should have noticed that my favorite technique for Applets is to add links to a published test page that links to the source code.

    Edit test.HelloWorld.html so that is has a links to the Source code and documentation after the end of the applet:

     		<a href="">Source Code</A>
    and publish all the files by copying them all to your public directory:
     		cp *HelloWorld* /www/public/..../
     		cp *HelloWorld* /web/public/..../
    (depending where your public WWW/Web directory is).

    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.

    Tell Me


  1. Application::java=a normal program that is executed on its owner's machine.
  2. Applet::java=a program that can be sent accross the web and executed inside a web page on a remote user's computer.

. . . . . . . . . ( end of section CS620: First Java Laboratory) <<Contents | Index>>


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

    (End of Net)
  • WWW::= See, index to web site for this class.
  • 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