Lab 2: First Java Program

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.

Part I: Compiling a program.

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:

  1. Copy the file 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 .
    
  2. Open the file in Xemacs.
    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.

  3. Look over the program. The part at the top with the asterisks along the left side is documentation. It doesn't contain any commands to the computer, just some information about the program. Update the documentation so that it indicates that you and your partner are the authors, and give the correct date. Leave the pattern intact.
  4. The rest of the file will look like a lot of nonsense---but isn't it exciting to think that in a few weeks it will all make sense? All this program does is write a message to the screen. Change the message (the part enclosed in quotes) to something of your choice. Leave the rest of the program intact.
  5. In the shell, look list the files in the current directory (remember, use the command 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.

  6. List the files again. You will see that a new file called 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.

Part II: A more interesting 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.

Part III: Turn-in

That's it. Now, here are the procedures for handing in labs (which are similar for handing in projects):

  1. Start a script. Remember this is done using the script command, giving the name of a script file.
  2. Delete any class files. This is done using the rm command. rm *.class will delete all files that end in .class. Be careful--- don't delete your .java files!
  3. For each program,
    1. Print the file, (for example, cat Greeting.java)
    2. Compile the program, as you did before. This proves to the grader that your program compiles correctly from scratch.
    3. Run the program, as you did above. This demonstrates that it works correctly.
  4. Exit the script.
  5. Print the script. I recommend using 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.


Thomas VanDrunen
Last modified: Mon Sep 4 10:00:27 CDT 2006