The goal of this lab is to write, compile, and test your first Java program, trying out the concepts we have learned in class. Remember to switch roles every ten minutes or so.
Now you will copy a Java program, compile, and run it. First, change directory into your directory for this class.
cd cs235
Make a directory for this lab, and then change into that directory.
mkdir lab2 cd lab2
To tell where you are, you can use the command pwd
,
which
stands for "path to working directory."
Now that you have a directory for this lab, do the following steps:
Greeting.java
from the class
public directory.
The class public directory is /homeemp/tvandrun/pub/235
.
Most of the labs and projects will involve some piece of code that you can
copy from there; I also will store in-class examples and old handouts there.
Remember, the command for copying files is cp
.
Don't forget to indicate where you want to copy the file to---and you can
indicate the current directory using a dot:
cp /homeemp/tvandrun/pub/235/Greeting.java .
xemacs Greeting.java &
Greeting.java
contains a program written in Java---the
source code.
Files that contain the source for a Java program should end in .java
.
ls
).
You should see something like
Greeting.java Greeting.java~
The "~" file is a backup version that Xemacs automatically generates when you
modify a file. Now, compile the program.
The Java compiler is invoked by the command javac
(for
"Java Compiler").
javac Greeting.java
If the compiler reports errors, look back at the Xemacs window and double check that you have not made any other changes to the file; as for help if you're not sure.
Greeting.class
has appeared. This is the file that javac
produces, containing
the bytecode or compiled version of the program.
Bytecode files always end in .class
.
Now run the program; the Java compiler is invoked using the command
java
,
giving the name of the program.
java Greeting
One difficult thing to remember at first is that when you compile, you need
to say Greeting.java
, but when you run the program, you need to
say Greeting
, not Greeting.java
or Greeting.class
.
This is because javac
compiles a file, so you need to
give the name of a file, whereas java
runs a program,
and when you give the name of a program, it knows the add .class
to find the bytecode file containing the compiled version of the program.
Open a new file in xemacs, call it Circle.java
.
Type (or copy and paste--- selecting text with your mouse will
automatically copy it, and clicking the middle mouse button in the
place where you want it to go will paste it) the following into it.
/** * Circle.java * * This is a simple experimental program for lab 2. * * @author Your Name * Wheaton College, CS 241, Spring 2006 * Lab 2 * Sept 5, 2006 */ import java.util.Scanner; public class Circle { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); // input from keyboard System.out.print("Please enter the radius-->"); double radius = keyboard.nextDouble(); // radius to work with // add code here } }
Don't wory about parts of this that you do not yet understand. Your task is to compute five things:
Pi is predefined in Java; use Math.PI
.
All of the values you compute should be double
.
That's it. Now, here are the procedures for handing in labs (which are similar for handing in projects):
script
command, giving the name of a script file.
rm
command. rm *.class
will delete all files that
end in .class
. Be careful--- don't delete your .java
files!
cat Greeting.java
)
a2ps -P sp
(name of script file).
a2ps
prints the file in "2-up" mode. Remember that
sp
is the name of the printer.
Make sure you have exited the script file before you print, or else it will print forever!
Then turn in the hard copy to me.