Com S 362 --- Object-Oriented Analysis and Design EXERCISE: RUNNING JAVA (File $Date: 2004/08/25 22:22:12 $) The purpose of this exercise is for you to learn the mechanics of writing and running Java programs with command line tools and Eclipse. As with all exercises, this is to be done individually, not in teams. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs362/syllabus.shtml). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I couldn't get it to work; say what you tried and what you didn't understand, such as "I tried running 'javac HelloWorld.java' but the system kept telling me it couldn't find the command javac.) During the class where this exercise is discussed, you should try to get help with these by explaining what you did and what your problems or confusions are. Don't be shy; there will be other people with the same problem, and everyone can learn by discussing these issues. 1. [Using the command line interface for Java tools] Read the course's "Running Java" web page for help in running Java and also for help getting a copy of Java and Eclipse for your home computer. http://www.cs.iastate.edu/~cs362/running_java.shtml For this edition of the course, it's best to use J2SDK 1.4.2 (not 1.5.0 beta 2), as that will work on all platforms and works with the version of JML we will use in the course. The command line interface is useful because it can be run from Makefiles and from slower machines. Put the following text in a file named "HelloWorld.java". (You can use cut and paste.) /** The classic "Hello, world!" program */ public class HelloWorld { /** Print "Hello, world!" on the standard output. */ public static void main (String args[]) { System.out.print("Hello, world!"); } } Then compile and run this program using the J2SDK command line tools (javac and java). Write down for an answer for this problem: a. What is the CLASSPATH and how does it affect compiling and running Java programs? b. Summarize the key points of how (you would tell someone) to use the J2SDK command line tools to compile and run Java programs. c. What happens if you make your CLASSPATH empty or not set? As always if you cannot do this or get stuck, write down the specific problems or questions you encountered instead. (However, even if you cannot do this or get stuck, you must continue on with the rest of the questions in this exercise. You must can write down questions for them too, but you have to look at everything). 2. [Using packages with the command line tools for Java] Read (in addition to the course's "Running Java" web page) chapter 13 of Ken Arnold, James Gosling, and David Holmes's book The Java Programming Language Third Edition (Addison-Wesley, Reading, Mass., 2000). Change the file "HelloWorld.java" so that it is in the "hello" package. Hand in a printout of this file along with your answers to this homework, and answer the following questions: a. If you just compile the file HelloWorld.java using javac, and then try to run the result with the Java interpreter, what happens? b. How do you fix the problem described in part (a) above? c. How does your CLASSPATH affect the compiling and running of the program? d. How does the directory you operate from affect the compiling and running of the program? As always if you cannot do this or get stuck, write down the problems you encountered instead. 3. [Using packages with Eclipse] Read the course's Running Java web page, and the Eclipse documentation for help with Eclipse. a. Set up Eclipse to automatically put your name in the @author documentation comment in each Java class you write. b. Repeat problem 2, part (a) using Eclipse instead of using the J2SDK command line tools, being sure that your name is in the comment for the class. c. What corresponds to the CLASSPATH setting in Eclipse? d. How does that affect the running of your code? e. Make a printout of your code from Eclipse. Be sure your name is in the comments in your code. WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL READINGS If you are new to Java you might also want to read the interesting (to you) parts of the "new to Java" section on-line at http://java.sun.com/learning/new2java/index.html (If you read that link, I would advise starting with the "getting started" step, which is step 2.) See also Mary Campione, Kathy Walrath, and Alison Huml's book The Java Tutorial: Third Edition (Addison-Wesley, Reading, MA, 2000). Online at: http://java.sun.com/docs/books/tutorial/uiswing/index.html