.Open CS320: First Java Laboratory . Check out New things on the Course Web Page .See http://www.csci.csusb.edu/dick/cs320/ . Goals Your task is to try some the experiments on this page. . Deliverable Write notes on three experiments and include links to the code. If the experiment includes an applet then embedd the applet in your web page. . Browser In JBH358 the Firefox browser has the Java plugin loaded. Use the command .As_is firefox in a terminal window to run this browser. . Hints .Box Keep your Java handout handy. Put an applet called AaAa in file AaAa.java not in a file called aaaa.java. See .See http://www.csci.csusb.edu/dick/samples/java.html and .See http://ftp.csci.csusb.edu/public/faculty/dick/ .Close.Box . Demonstration 1 Here .See http://ftp.csci.csusb.edu/public/faculty/dick/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 .See http://ftp.csci.csusb.edu/public/faculty/dick/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: .As_is ls -ld /share/j* Different machines seem to have different relaible ones so you may have to try out several below. In your browser window look at this: .As_is import java.lang.*; .As_is public class Hello { .As_is public static void main(String argv[]){ .As_is System.out.println("Hello, World!"); .As_is } .As_is } It is a traditional first C program written in Java. Perfect for experimenting with the compilers and JVMs. Use your favorite editor to create a copy of the program above in a file called. .As_is Hello.java Notice: not `hello.java`, not `h.java`, but `Hello.java`. This is important! Compile it like this: .As_is /share/java1.4.1/bin/javac Hello.java or perhaps .As_is /share/j2sdk1.4.1/bin/javac Hello.java Note: Do not hold your breath waiting for this to compile, the compiler was very slow when I tested it. .Box If the above worked you now have a file called 'Hello.class'. It is a binary file. Do not try editing or listing it! If the 'javac...' doesn't work you may have a typographical error... First check that you did type '.../javac Hello.java' as shown. Then try downloading .See http://ftp.csci.csusb.edu/public/faculty/dick/Hello.java as Source into a file called "Hello.java" and try again. .Close.Box Run the compiled class like this: .As_is /share/..../bin/java Hello .Box Notice 'java Hello.java' and 'java Hello.class' do not work! The `source` is compiled, but a `class` is interpreted. Java code was designed to be interpreted by a machine independent `virtual` machine that runs bytecode. bytecode::=http://www/dick/samples/java.html#bytecode. .Close.Box . Your Second Java Application Modify your Hello.java 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. .As_is Q Hello.java .Box I named the program Q for a character played by John De Lancey in `StarTrek: The Next Generation`, my `Q` program is almost as quirky and powerful. If your shell reports that 'Q can not be found', then you can .List Use .As_is ~dick/bin/Q Hello.java instead. .Close.List .Close.Box . Demonstration of Polymorphism in Java Application In the class we met a simple example of polymorphism in Java .See ../15q.java and in C++ .See ../15q.cpp download these files as source, save them, compile them and make the C++ program do the same thing as the Java application. . 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: .As_is test.HelloWorld.html and it should contain the following HTML: .As_is Test Hello World .As_is .As_is You can not see this brilliant Java Applet. .As_is .As_is You now need a file called: .As_is HelloWorld.java containing .As_is import java.applet.*; .As_is import java.awt.*; .As_is public class HelloWorld extends Applet { .As_is public void init() { .As_is setSize(150,25); .As_is }//init .As_is public void paint(Graphics g) { .As_is g.drawString("Hello world!", 50, 25); .As_is }//paint .As_is }//HelloWorld The Applet is compiled just like any other program: .As_is /share/java1.4.1/bin/javac HelloWorld.java This will generate a file with "HelloWorld.class". Notice that the compiler forces you to name the file .As_is HelloWorld.java and the class in the file must match .As_is HelloWorld and be `public` for it to be used over the World Wide Web. You can view this file with a browser..... BUT make sure it is Netscape 6.* not 4.* on your workstation. Or else try Konqueror or Mozilla (and turn on the Java and console). Konqueror need to know where to find the 'java' JVM as well! Before others can access your work you must copy all the *HelloWorld* files to your public directory we will worry about this in a later! Note .Net Java is supposed to offer: Write Once, Run Anywhere. What you get is: Write once, Debug Everywhere .Close.Net . A Second Applet Your next task is to change your HelloWorld.java program to output the result in color by adding .As_is g.setColor(Color.blue); to HelloWorld.java. 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: .As_is g.setFont(new Font("Helvetica", Font.BOLD, 24)); .Box If your browser 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. 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. .Close.Box . 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: .See http://ftp.csci.csusb.edu/public/faculty/dick/HelloWorld.java and compile and test it like this: .As_is javac HelloWorld.java .As_is java HelloWorld Once this runs ok, change the color and form of the graphic string. Make the result run. .Box Adding a suitable main function is a viable development technique. However Java does not make it easy to add a function to a class. It is worth keeping 'main' in a special file and learning how to read files into Java code using your favorite editor. .Close.Box . 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: .As_is Source Code and publish all the files by copying them `all` to your public directory: .As_is cp *HelloWorld* /www/public/..../ or .As_is 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. . Earn credit for the lab Write up your experience in a new CS320/java page or (if it works) on your test.HelloWorld.html page. You can get full credit for programs that run in private in this lab. .Open Extras . A 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 .See http://ftp.csci.csusb.edu/public/faculty/dick/Luv.html Study the documentation a bit before you get your own copy of the source code .See http://ftp.csci.csusb.edu/public/faculty/dick/Luv.java 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: .See http://ftp.csci.csusb.edu/public/faculty/dick/test.Luv5.html While you are there follow the links to the Code and Documentation. .Close Extras . Setting your UNIX up for Java Make sure your PATH variable has /share/java1.4.1/bin or else /share/j2sdk1.4.1/bin added to it when you next plan to use Java. To add /share/j2sdk1.4.1/bin to your UNIX shell PATH .Net edit your ~/.bash_profile file so that the PATH is redefined precisely like this .As_is PATH="$PATH:/share/j2sdk1.4.1/bin" .As_is PATH="$PATH:/share/bin" .Close.Net This is reread whenever you login or open a terminal window. Test it before you logout by executing .As_is . ~/.bash_profile .Close.Net . Glossary Application::java=`a normal program that is executed on its owner's machine`. Applet::java=`a program that can be sent accross the web and executed inside a web page on a remote user's computer`. Polymorphism::jargon=`In a polymorphic language a pointer declared to point at a high level object can point at a low level extension and get the extended behavior`. .Close CS320: First Java Laboratory . Check the Preparation for next class .See ../16.html