CS 117: Introduction to Java


Introduction to Java

To get started, create a directory called lab2. Feel free to go back and peek at the lab from last time if you need to jog your memory on how to do so.

A really simple Java program

Click on the file Simple.java and save it in your "lab2" directory. Open up the file in NEdit. The parts of the program are labeled. Take a couple of minutes to look at the program; see if you can figure out what it does.

Before we can run it, we need to convert this text into bytecode (a format that Java can read and interpret). We do this by compiling the program (running a special program that converts text into bytecode). Java's compiler program is named javac. 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 "Welcome to CS117, [your name and your partner's name]!".

In general, to compile a Java program type javac [filename], and to run a Java program type java [classname], where [classname] is the name of the class file, minus the .class extension.

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. main is a special method in Java; it is always the one that the computer looks for to see how to run your program.

EXERCISE 2: What action does the main method in Simple.java perform?

Now download the file Simple2.java into your lab2 directory. Simple2.java is very similar to Simple1.java; the difference is that Simple2.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 "here's some data that we will use; it's a String (of characters), its name is 'message', and its value is 'Are we having fun yet?."

EXERCISE 3: Modify the message that the program writes out to the screen in Simple2.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, not only can we write our own classes, but we can use other classes in our programs, either ones we write or ones that Java provides.) This class is the System class. System 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. Finally, println() is a method (action) associated with the out object. So, System.out.println("...") 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 today and one later on in the term.

Save Simple3.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, 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). (We'll talk about this more in class, so hang in there.)

Now 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 lines 27 and 28 with the following lines:
    System.out.println("Enter your age: ");
    age = input.nextInt();
  3. Change the output message to
    "Your name is " + name1 + " and you are " + age + " years old. Welcome!"

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.)

More exercises

Submit your answers to these exercises, and the ones above, 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.

  1. Modify Simple3.java again so that it asks for the names of you and your partner and both of your ages, and displays the information on separate lines. Save this program as Simple4.java.
  2. Go back to Simple.java, and replace System.out.println(...) with System.out.print(...). What is the difference between the println() and print() methods?
  3. Modify Simple4.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. 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()?

Instructions for using hsp are here.


Authored originally by Amy Csizmar Dalal. Modified by Dave Musicant.