CS 117 Winter 2006 Lab 2
Introduction to Java


You should complete this lab by 11:59p on Tuesday, 10 January 2006 (but hopefully you'll finish it during class!), with the partner I've assigned to you.

To get started, create a directory called lab2. Feel free to go back and peek at lab #1 if you need to jog your memory on how to do so. Also, create a file lab2.txt in that directory, and put your names in it. You'll put your answers to the "in words" exercises into that file.

A really simple Java program

Click on the file Simple.java and save it to your "lab2" directory as "Simple.java". The parts of the program are labeled. Take a minute to look at the program to figure out what it will do once we run it. As we discussed in class, this program needs to be compiled into bytecode before it can be run. (As far as the computer is concerned, at this point it might as well be a poem.) To compile this program, type:

javac Simple.java

The program should compile without returning any messages. (Any messages you see are errors.) If you do get errors, get help from from the instructor or the prefector.

Once you've compiled the program successfully, do an ls. You should see a new file in the directory, named Simple.class. This is the "executable" (bytecode) file that you will run. To run the program, type:

java Simple

Notice that you do not include the .class extension when you run a Java program.

EXERCISE 1: Modify the program so that it prints out "Cold enough for ya, [your name] and [your partner's name]?". (You'll have to open the file with NEdit, make a change, rerun javac Simple.java, and rerun java Simple.)

A diversion: What a Java program looks like

Most Java programs look pretty similar. They all have the following format:

Simple.java does not contain any data, but it does contain one method. That method is called main. The main method is a special method in Java; it is always the one that the computer looks for to see how to run your program. The main method in Simple.java performs only one simple action: it prints out a (pretty inane) comment to the screen. Next you'll look at a program that does something just a shade more complicated.

Now download the file SimpleWithVariable.java into your lab2 directory. SimpleWithVariable.java is very similar to Simple.java; the difference is that SimpleWithVariable.java uses data. The line that starts with String is our data line. This line is an example of a variable declaration, which is a fancy way of saying the following: here's some data that we will use; it's a String (of characters—the name should evoke a string of beads, or maybe a string of Alphabits), its name is "message", and its value is "It could be worse; we could be in Duluth."

EXERCISE 2: Modify the message that the program writes out to the screen in SimpleWithVariable.java by changing the value of the variable.

Output in Java

The programs we've seen so far contain "output" statements, because they each write something to the screen. There are several ways to do output in Java; we're just going to look at one today.

The output of these programs is what we call "console output", because data is printed out to the console (another name for the terminal or xterm). In order to do console output, we use one of Java's many built-in classes. (One of the nice things about Java is that—in addition to writing our own classes—we can use other classes in our programs, either ones that we wrote previously, or ones that Java provides.) This class is the System class. The System class contains attributes and methods for doing various things, such as writing out data and errors to the console, exiting a program, etc. The System class contains an object named out, which knows how to write to the console. (What are these objects of which we're speaking? Stay tuned!) Finally, println() is a method (action) associated with the out object. So, System.out.println("hello!") is how we indicate that we want to write out something to the console.

Java input

Just as there are several ways to output data in Java, there are also several ways to input data to Java. We'll look at one input method today, too.

Save SimpleWithInput.java to your lab2 directory. Compile and run the program. What do you see this time?

Now look at the program in NEdit. You should notice several differences between this program and the previous program. First, the first line is new: we are "importing" all of the classes in the java.util package. (A "package" in Java is a set of related classes that are pre-written for us.) The java.util package contains "utility" classes that are useful to many types of programs, such as a random-number generator and classes that deal with groupings of items. The class that we're using here is the Scanner class, which handles getting input from the user in various ways (console, file, etc.). Second, we have two more variables that we've declared. These variables will store whatever the user types in to the console window (in this case, your name and your partner's name). Third, we have a statement that contains the word new. Statements that contain the word new are used to create objects. Here, we're creating an object from the Scanner class. In most cases, we need to create an object before we can use it; we create a Scanner object, name it "input", and use this object to read in data from the console that the user has typed in. (This whole "new" discussion should be a little mysterious right now, but we'll talk about this more in class very soon. Hang in there!)

EXERCISE 3: Make the following changes to the code:

  1. Change the variable named name2 to age, and change its type from String to int.
  2. Replace the prompt for your partner's name with the following lines:
    System.out.println("Enter your age: ");
    age = input.nextInt();
  3. Change the output message to
    "Well, I hope that 65 winters have prepared you well for this weather, Joan Baez!"
    where "65" and "Joan Baez" should change depending on the user's inputs. (And, yes, Joan Baez turns 65 on 9 January 2006.)

Compile and run the program.

EXERCISE 4: What happens if you enter a number when prompted for your name and a name when prompted for your age? If you get a message, what did the message say? In words, explain what you might do as a programmer to prevent a user from entering incorrect input. (I'm not looking for an exact answer, just a general idea as to the approach you'd take.) Put your answer in lab2.txt.

More exercises

Here are a few more exercises to get your Java juice flowing.

Exercise 5: Modify SimpleWithInput.java again so that it asks for the names of you and your partner and both of your ages, and taunts both users about their ages (you choose the taunts) on two separate lines. Save this program as SimpleWithMoreInputs.java.

Exercise 6: Go back to Simple.java, and replace System.out.println(...) with System.out.print(...). What is the difference between the println() and print() methods? Describe the difference in lab2.txt.

Exercise 7: Modify SimpleWithMoreInputs.java by doing the following:

  1. Add your age and your partner's age and write this out to the console. (HINT: The addition operator in Java is "+".)
  2. Subtract your age from your partner's age and write this out to the console. (HINT: The subtraction operator in Java is "-".)
  3. Compute your ages in dog years (seven times your human-years ages) and write them out to the console.
  4. You may have noticed that sometimes we use input.next() to get input from the user, and sometimes we use input.nextInt(). Based on what you observed, what is the difference between input.next() and input.nextInt()? Put your answer in lab2.txt.

Submit your answers to these seven exercises via hsp. (Note: you can submit entire directories using hsp, so it may be easiest to submit your entire "lab2" directory.) Answers to the questions should be in a text file named "lab2.txt". You should work on and submit the answers together with your partner. Only one of you should submit your lab with hsp, but make sure that you indicate in the lab2.txt both of your names.

Instructions for using hsp are here.


Authored originally by Amy Csizmar Dalal. Modified by Dave Musicant and David Liben-Nowell.