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
- create a directory called 'egr54' from your root directory.
- set read permissions for the Prof. Stetten and the TA's:
fs setacl egr54 stetten rl
fs setacl egr54 vtp1 rl
fs setacl egr54 gw1 rl
from now on, any directories you create under 'egr54' will inherit these
permissions. you can check permissions on the directory:
- 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
- change into the 'world' directory.
- open up a text editor (xemacs, vi, pico, etc...) and type in the
following program, and save it as 'Hello.java'.
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
- compile this simple program:
- 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:
- 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:
public class Greeter
{
public Greeter()
{
}
}
- add another method, called 'greet' to the class that will print out
a line of your choice. this should come after the constructor
definition:
public class Greeter
{
public Greeter()
{
}
public void greet()
{
// some of your code
}
}
- 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:
public static void main(String args[])
{
System.out.println("Hello Java");
Greeter jim = new Greeter();
jim.greet();
}
- recompile and run the code following the same steps above,
hopefully you should get output that makes sense.
- 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:
public void greet(String name)
{
System.out.println("why, hello " + name);
}
- again, go back to 'Hello.java' and modify your code to match. you
should call greet() with one parameter, a string.
jim.greet("you");
- 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
- change to your reality directory under the egr54 directory:
- copy the reality source code from the main repository and link to
the std_graphics directory:
cp /afs/acpub/project/web/egr54/src/1_00/* .
cp /afs/acpub/project/web/egr54/src/1_01_patch/* .
ln -s /afs/acpub/project/web/egr54/lib/std_graphics
- compile and run with DemoSun
javac *.java
java Reality DemoSun