.Open CS320: Second Java Laboratory -- Applications . 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. and write a page of notes on how Java $Applications and Objects work. . Deliverables Show me your notes and code on your web site at the end of the session. No Applets are needed in this laboratory. . Hints .Box (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. 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"! Review your previous lab page. If stuck look in your handout, .See http://www/dick/cs620/Java.htm , my samples .See http://www.csci.csusb.edu/dick/samples/java.html , .See http://ftp.csci.csusb.edu/public/faculty/dick/ (examples) and .See http://www/dick/cs320/resources.html#Java (links to documentation). .Close.Box . Review: Applications and Applets Here are three Java files: .See http://ftp.csci.csusb.edu/public/faculty/dick/A.java .See http://ftp.csci.csusb.edu/public/faculty/dick/B.java .See http://ftp.csci.csusb.edu/public/faculty/dick/C.java Try to answer the following questions without running the code: .Box Which files define applications? Which of these define Applets? Which contains both an application and an Applet? .Close.Box Note down your answers - best guess will do. Plus your reasons (theories). If in doubt see .See http://www/dick/samples/java.html#Overview and .See http://www.csci.csusb.edu/dick/samples/java.html#Applications Can Run Applets 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: .As_is O = IO.name(I); O is the output, IO is handled in in-out mode, and I are all input parameters. Here are some rules about Java... .Box (Rule 0): All functions belong to an object or to a class. No naked functions. (Rule 1): Parameters of primitive types (int ...) are passed by value, only. (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. (Rule 3): Parameters and results are single values or Objects but an object can contain many parts. (Rule 4): No IO parameters. But you can fake them be declaring a class of References. (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. (Rule 6): No function parameters. But you can fake it by declaring a class of functions. .Close.Box Here are some example programs that to some extent demonstrate the above rules: .Box The NakedGun File .See http://ftp.csci.csusb.edu/public/faculty/dick/NakedGun.java The X Files .See http://ftp.csci.csusb.edu/public/faculty/dick/X.java The Why Files .See http://ftp.csci.csusb.edu/public/faculty/dick/Why.java The Judge Program .See http://ftp.csci.csusb.edu/public/faculty/dick/Paras.java The Refs Program .See http://ftp.csci.csusb.edu/public/faculty/dick/Refs.java The Fun Program .See http://ftp.csci.csusb.edu/public/faculty/dick/Fun.java .Close.Box 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 .See http://ftp.csci.csusb.edu/public/faculty/dick/Echoes.java and try to reason out what it will do when compiled(giving Echoes.class) and run as an application like this .As_is java Echoes this is a test Modify the code so that it outputs the same data in the reverse order. . Experiment with Graphics Download .See http://ftp.csci.csusb.edu/public/faculty/dick/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. .See http://ftp.csci.csusb.edu/public/faculty/dick/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: .As_is class MyExtension extends MyClass{ ....public MyExtension(...) } and do something like this: .As_is 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 .See http://www/dick/cs320/handouts/cs320wuml.html#Polymorphism and then do the following erxperiment. . More Polymorphism Study the following documentation for a useless but generic class of Things: .See http://ftp.csci.csusb.edu/public/faculty/dick/Thing.html The code is in .As_is http://ftp.csci.csusb.edu/public/faculty/dick/Thing.java Download a copy of the code (Shift click): .See http://ftp.csci.csusb.edu/public/faculty/dick/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: .See http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html Also follow some of the links on this page. .Open 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 .See http://www/dick/cs320/sieve/Specification You can see implementation in the UNIX shell, Ada, C, C++, Java, etc, in .See http://www/dick/cs320/sieve/ . The Cryptogram Problem Here is another problem that has a clean solution as two concurrent processes: .See http://www/dick/cs320/crypto/Specification plus a sample of test data .See http://www/dick/cs320/crypto/test.dat and the design .See http://www/dick/cs320/crypto/Design using XBNF. The code includes a Makefile .See http://www/dick/cs320/crypto/Makefile , to process that can be independently compiled, and run using a pipe to connect them ( p1 | p2 ): .See http://www/dick/cs320/crypto/p1.c .See http://www/dick/cs320/crypto/p2.c It is easy to convert p2.c into a restartable function: .See http://www/dick/cs320/crypto/p2s.c and so produce .See http://www/dick/cs320/crypto/crypto.c or you can use the UNIX C `pipe()` and `fork()` functions .See http://www/dick/cs320/crypto/cryptop.c to get two concurrent processes. In Ada it is easy to create a main program .See http://www/dick/cs320/crypto/crypto.ada that has a package with two tasks P1 and P2. You might like to develop a Java translation of the original design. .Close Non-Java Concurrency .Open Optional . 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... .See http://ftp.csci.csusb.edu/public/faculty/dick/TextDemo.java 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: .Box A fun example .See http://www/dick/examples/rice.cc A detailed example of the difference between virtual and non-virtual functions in complex hierarchies: .See http://www/dick/examples/vf.cc .See http://www/dick/examples/virtual_fun.cc Run Time Type Information in C++ allows an object to know what type of thing it is .See http://www/dick/examples/rtti.cc this can also be linked to Source Code Control Indentification(SCCI) .See http://www/dick/examples/vn.cc .Close.Box . Translate C++ to Java Think about what happens when you try to translate the following C++ program .See http://www/dick/examples/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. .See http://ftp.csci.csusb.edu/public/faculty/dick/qsort3.java This in one of a series of experiments that I tried while learning Java. .See http://ftp.csci.csusb.edu/public/faculty/dick/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? .Close Optional .Close CS320: Second Java Laboratory -- Applications . Check the Preparation for next class .See ../17.html