CMPU 101 - Lab 1

In this lab you will learn how to use the DrJava environment in the Mac lab, and experiment with simple Java programs.  Programming is best learned by writing programs.  So let's get started!

Step 1: Log In

Choose any of the Mac computers in the Mac lab.  At the login prompt, type "cs101" and hit return.

Along the top of the screen are a number of icons arranged in a toolbar:

These start programs you will need to use, including a web browser and the DrJava environment you will use to write and test Java programs.

Step 2: Download the Lab File

Start a web browser by clicking the toolbar icon that looks like this:

In the web browser, open the class web page, http://www.cs.vassar.edu/~cs101/hovemeyer.  Scroll to the bottom.  Right-click on the file for Lab 1, FirstProgram.java, and choose "Save Link As...".  Save the file in the "Home" directory.

Step 3: Starting DrJava

After you have saved the FirstProgram.java file, start DrJava by clicking on the toolbar icon that looks like this:

It will take a minute or so for DrJava to start.  Eventually you will see the DrJava window.  From the menu, choose "File->Open...".  You should see "FirstProgram.java" in the file browser in the directory "cs101" (which is the home directory).  Select it and click the "Open" button.

Step 4: A First Java Program

Initially, "FirstProgram.java" will look like this:

public class FirstProgram {
  public static void main(String[] args) {
  }
}

Change the body of the main method (the part between the "{" and "}" characters) to add the following statements:

int yearFounded = 1861;
System.out.println("Vassar founded in " + yearFounded);

The first statement declares a variable called yearFounded that has type int, and sets it to the value 1861.  The second statement prints a message to the "standard output": every program that runs can write messages to the standard output.  In Java, any value passed to System.out.println is written to the standard output.

Once you have finished modifying the main method, save the file ("File->Save"), and then press the "Compile All" button.  If everything goes well, the "Compiler Output" window will display the message

Last compilation completed successfully.

If you see error messages, then check the program carefully to make sure you typed everything correctly.

Once the program compiles successfully, click the "Interactions" window, and inside it type:

java FirstProgram

Then press "return".  You should see the output

Vassar founded in 1861

Congratulations!  You have written and executed a Java program!

Step 5: Variables and Expressions

Next, try adding some more statements to the the main method, just after the two statements you have already added.  Specifically, add statements that looks like these:

int testValue = yearFounded * 10;
System.out.println("Test value: " + testValue);

These statements multiply the value of the yearFounded variable by 10, and prints the result to the standard output.  (Using "File->Save", "Compile All", and "Interactions->java FirstProgram", save, compile and run the program again to verify that this is so.)

Your task is to modify the expression so that the value of the testValue variable becomes negative.  Recall that because Java integers are finite, computations that produce values too large to be represented will wrap around to become negative.  How large a multiplier do you need before testValue becomes negative?

Step 6: Submitting Your Work

Before you submit, please show your work to an instructor or lab coach.

Open a web browser and go to https://mr42.cs.vassar.edu/dropbox/cs101/upload.html.  Enter your CS 101 login name (which is the same as your Vassar email account name), and your CS 101 password.  Click the "Browse..." button next to the "Upload File" box.  In the file browser dialog, choose "FirstProgram.java" from the Home directory.  Finally, click the "Upload" button.

Step 7: Logging Out

When you have submitted your work, log out by clicking the "System" menu on the top toolbar, choosing "Log Out", and then choosing "Log Out" when the dialog box appears.

When you log out, all of the files you created during your session will be deleted automatically.  Make sure you submit your work before logging out!