CMPS12L, Assignment 1



In this assignment we will cover the basic process for compiling, running, and submitting your java programs. Along the way you'll learn a few Unix commands.

First, you need to log in. Now open up a terminal window by right clicking in the desktop area and clicking on the Tools menu and then clicking on Terminal. You might think of the terminal as the Unix version of the Window's command prompt.

In order to see what your “present working directory” is type pwd at the prompt. More than likely you are in your HOME (most often abbreviated with the ~ symbol) directory. Type ls to see a listing of the files in the directory (analagous to dir in DOS).

Now, let's create a separate directory for this assignment (don't type the $, that is the prompt):

$ mkdir lab1

We can move to that directory by typing:

$ cd lab1

Download the file SimpleInput.java to ~/lab1 (the directory you just created). Now if you type ls in your terminal window you should the file listed. The code is listed in the textbook in Section 2.6 and is accompanied by an explanation of the code. For a quick look at the contents of the file try typing:

$ more SimpleInput.java

There are two things worth noting in the code, which I'll reproduce below:

import tio.*;

class SimpleInput{

public static void main (String[] args){
int width, height, area;
System.out.println("type 2 integers for " +
"the width and height of a box.");

width=Console.in.readInt();
height=Console.in.readInt();
area=width*height;

System.out.print("The area of the box is ");
System.out.println(area);
}

}

First, notice that the name of source code file is the name of the class, SimpleInput, with .java appended. This is not a coincidence. It is good practice to always name the file after the class contained within. For all the assingments in this class you will be required to do this.

Second, notice the first line: import tio.*. This tells the compiler to use other code (referred to as “packages” in the Java world) when building the program. But as we'll see, we have to make sure that the compiler knows how to find it.


Compiling

It is a good idea to be familiar with how to compile by hand, as opposed to using an Integrated Development Environment (IDE) like Netbeans. You will need at least be able to follow these steps to make sure your code compiles when you move it from your home computer to unix.ic.ucsc.edu.

The format for compiling a set of files is:

$ javac file1 file2 file3 ...

Try compiling SimpleInput.java. You should get an error like this:

SimpleInput.java:1: package tio does not exist

........
3 errors

The compiler cannot find the tio package. So we need to tell it where it is by specifying the “classpath”. This is a list of directories that the compiler will search for packages that you refer to in your code. We use the -cp option (type java -h to see more options) to set the classpath like this:

$ javac -cp /afs/cats.ucsc.edu/users/s/mcdowell/java/public SimpleInput.java

Hopefully it did not return any errors. Now if we type ls we should see two files: SimpleInput.java and SimpleInput.class. The class file is what the Java Virtual Machine refers to when it wants to run your code.

Its kind of annoying to have to type that long path everytime you want to compile your code. We can get around this by setting the CLASSPATH variable. When no classpath is specified with the -cp option, the compiler uses the contents of this variable as the classpath. To do this:

$ setenv CLASSPATH .:/afs/cats.ucsc.edu/users/s/mcdowell/java/public

Now try compiling SimpleInput.java again but without the -cp option. We no longer get an error. To save yourself having to set the CLASSPATH variable everytime you log on, you can just put the above line into ~/.cshrc. Everytime you log on, the contents of this file are executed.

With all of that out of the way, now we can actually run the program. We do this by giving the Java Virtual Machine the name of the class containing our main method (which is starting point of every program). For this example:

$ java SimpleInput

Hopefully you will be prompted for 2 numbers.


Submitting

Now we need to submit the program. At the terminal prompt:

$ submit cmps012a-ap.w05 lab1 SimpleInput.java

$ peek cmps012a-ap.w05 lab1

The first command copies SimpleInput.java to a location which the Professor and TAs will have access to. The second command lets you double check that everything was submitted properly. Generally speaking, replace lab1 with the name of the assignment you are submitting and SimpleInput.java with the names of one or more files that you want to submit.


Submitting Files from Your Home PC

The basic steps for submitting files from home is:

  1. Use an FTP client to move the files to unix.ic.ucsc.edu. (see FAQ)

  2. Verify that your code compiles and runs.

  3. Log onto unix.ic.ucsc.edu with SSH and submit the files as described above. Help using SSH.

Also, if you are working on a Windows machine you should know that Windows and Unix use different newline characters. (see FAQ)