Lab 1


The objective of this lab to familiarize everyone with the Java environment. You will setup reality in your account on acpub, write, compile and run a simple java program, modify it a bit and then move on to compile and run reality.

Setting up your account

  1. create a directory called 'egr54' from your root directory.
  2. set read permissions for the Prof. Stetten and the TA's: from now on, any directories you create under 'egr54' will inherit these permissions. you can check permissions on the directory:
  3. change into your egr54 directory, and create the following directories: later, we will copy Reality.java into the 'reality' directory. First we will write a simple java program in the 'world' directory.

Hello Java

  1. change into the 'world' directory.
  2. open up a text editor (xemacs, vi, pico, etc...) and type in the following program, and save it as 'Hello.java'.
  3. compile this simple program:
  4. if the file compiled successfully, you shouldn't see any error messages, and you should find a Hello.class file in your directory. run Hello.class using the java virtual machine:
  5. now, we want another class to use in this hello program. create another file called 'Greeter.java'. create a class much like the Hello class, but without a main function. add a constructor method (method = function in java), which would be a method of the same name as the class, like in C++. it should have an access modifier (public) and no parameters:
  6. add another method, called 'greet' to the class that will print out a line of your choice. this should come after the constructor definition:
  7. now we have a class that we can create (instantiate) somewhere else and call it's methods to do things for us. go back to the Hello.java file and add a line after the "Hello Java" statement that creates a new Greeter and calls its greet() method. to do this, we use the new keyword along with a call to the constructor:
  8. recompile and run the code following the same steps above, hopefully you should get output that makes sense.
  9. we're going to take this one step further. go back to the Greeter code in 'Greeter.java' and add the flexibility of a parameter to the greet method. this is done by adding parameter declarations in between the ()'s, exactly like C/C++. we'll add a person to greet as the parameter in the form of a string:
  10. again, go back to 'Hello.java' and modify your code to match. you should call greet() with one parameter, a string.
  11. try compiling and running this code. one last thing you might try doing is "overloading" the greet method in the Greeter class. add another method, again called greet, but with no parameters again. now, when you call jim.greet() or jim.greet("another person") java will distinguish between the two and call the correct version.

On to Reality

  1. change to your reality directory under the egr54 directory:
  2. copy the reality source code from the main repository and link to the std_graphics directory:
  3. compile and run with DemoSun