CS 124, Spring 2014
Lab 1: Introduction to Java
This lab introduces you to programming in Java using the command-line interface in Linux. You should be familiar with Linux from last Friday's pre-lab.
In this lab, you will write your first Java programs. You work should be stored in a directory named lab1, and you should submit your work by copying that directory into your homework folder as discussed below. You must turn in your work by the beginning of next week's lab.
Starting the Lab
To start the lab, log into Linux and open a Terminal. Use the following three commands to create a directory to hold your work for this lab and to change into that directory:
cd cs124 mkdir lab1 cd lab1
The name of the directory starts with the lower case letter "L" and ends with the number "1". Note that the name has no spaces in it. You shouldn't use spaces or other special characters in file and directory names, since it makes it harder to work with those names on the command line.
The first Java program that you write will be in a file named GreetMe.java. To create that file and start editing it, use the following command in the Terminal:
gedit GreetMe.java
File names are case-sensitive, and you should use an upper case "G" and an upper case "M" in the name. The file is created in the current directory, which is lab1 if you entered the three previous commands correctly.
Your First Java Program
For now, you will used gedit to write Java programs, you
The traditional first program just says "Hello World"—but let's make it a little more personal. Type the following lines of Java code into the editor window, except replace XXX with your own name. (Hint: Instead of typing it, you could also open this lab worksheet in a web browser, and use copy-and-paste to copy the code into gedit!)
public class GreetMe { public static void main(String[] args) { System.out.println("Hello, XXX!"); System.out.println("Pleased to meet you."); } }
The name of the class is "GreetMe" with upper case "G" and "M". The name of the class must match the first part of the file name, GreetMe.java. After typing in the code, save your file by hitting Control-S or by selecting "Save" from the "File" menu. Then exit from gedit by hitting Control-Q or by selecting "Quit" from the "File" menu. Back on the command line, compile the program by entering the command
javac GreetMe.java
If you have made no mistakes while typing the program, this command will succeed with no error messages; you will simply get another command prompt. If there are errors in the program, they will be listed. In that case, you have to edit the file to fix the errors, using the command gedit GreetMe.java again. After you have made the corrections, quit gedit and give the command to compile the program again. Once the program has been compiled successfully, you will have a new file named GreetMe.class. Use the ls command to check that it is there. GreetMe.class is the compiled program. Once you have GreetMe.class, you can run the program by entering the command
java GreetMe
Note that this command uses the class name GreetMe, not the name of the file GreetMe.class. The computer should say Hello to you. Before proceeding, if you haven't already seen an error from the compiler, you should change the program to introduce an error, and see what happens when you compile it. (Try removing one of the semicolons, for example.) Note that the error messages from compiler will contain line numbers that tell you where the compiler found an error. Pay attention to the line numbers—they can help you find the errors. But the line numbers aren't foolproof; often the line that you have to change to fix the error is not the same as the line where the compiler noticed a problem.
It can quickly become tiresome to have to close the gedit window every time you want to compile and run your program. To avoid this, add a "&" at the end of the command that you use to run the editor:
gedit GreetMe.java &
The "&" tells the computer to run the program "in the background." It lets you work in the Terminal while the gedit window is open, switching back and forth between the gedit window and the Terminal window. However, you must always remember to save your program before you compile it. The compiler always works on the version that has been saved, not the version that is in the editor window
As another command-line trick, try using the arrow keys when you type commands. The left- and right-arrow keys work as you probably expect, moving you back and forth within the current line. The up- and down-arrow keys are even more useful: They can be used to go back through the list of previous commands, to retrieve a command that you typed earlier. This can save a lot of typing when you are going through the edit-compile-run cycle over and over!
A Program With Computation
For a second program, write the Fahrenheit-to-Celsius conversion program that we used as an example in class. To begin, use the command:
gedit Temperature.java &
in the Terminal. Then type the following program into the gedit window (or copy-and-paste it from a Web browser window):
public class Temperature { public static void main(String[] args) { double degreesFahrenheit; double degreesCelsius; degreesFahrenheit = 98.6; degreesCelsius = (degreesFahrenheit - 32) / 1.8; System.out.print(degreesFahrenheit); System.out.print(" degrees Fahrenheit equals "); System.out.print(degreesCelsius); System.out.println(" degrees Celsius"); } }
Save your program, compile it, and run it, to make sure it works, using the commands
javac Temperature.java java Temperature
in the Terminal.
Two Programs On Your Own
Here are two more exercises for you to work on:
For you third exercise, write a program that converts 1835 ounces into an even number of pounds plus some number of ounces left over. For this program, you must use variables of type int. If a variable ounces of type int holds the total number of ounces, then the expression
ounces / 16
computes the integral number of pounds. When used with int values, the operator "/" computes the whole number of times that one number goes into another. The expression
ounces % 16
computes the number of ounces left over above that number of pounds. The operator "%" computes the remainder when dividing one integer by another. The value of ounces % 16 is between 0 and 15.
For your fourth exercise, write a program to solve the following problem: You are throwing a little party for all of HWS's 2272 students. You want to have 3 glasses of root beer available for each student. A keg of root beer costs $89.95 and serves 210 glasses. How many kegs should you buy, and how much will they cost? Your program should do all the calculations starting from the basic data given here, and then print out the two answers (the number of kegs and the total cost). In your program, give names to the basic data, and work from there:
people = 2272; glassesPerPerson = 3; glassesPerKeg = 210; costPerKeg = 89.95;
You can name your programs anything you want. Note that the output should be more than just numbers. You want to include some words to describe the meaning of the output.
Getting Input from the User
The programs that you have written so far are not very useful, since they do exactly the same thing every time you run them. More useful programs would get input from the user and would give outputs that depend on what the user enters into the program. For your fifth and sixth exercises, you should make new versions of the Fahrenheit-to-Celsius program and of the keg party program. The new versions will use input from the suer. We have not yet covered user input in class, and you will probably have to work on these exercises after today's lab.
To do user input, you need a copy of TextIO.java. This file must be in the same directory as the program that uses it, so you need to copy TextIO.java into your lab1 directory. To do this, make sure that you are working in your lab1 directory, and use the command:
cp /classes/cs124/TextIO.java .
This command ends with a space followed by a period. The period is a name for "the current directory," so in this case it means the lab1 directory that you are working in. (The cp command is a little complicated; when you copy a file to an existing directory, a copy is created inside that directory with the same name as the original file.)
To do the fifth exercise, you should start by making a copy of Temperature.java, with a different name. Do that with the command:
cp Temperature.java TemperatureWithInput.java
(When you use cp to copy a file to a destination that does not yet exist, a new copy of the file is created, where the name of the copy is the destination. In this case, you get a new file named TemperatureWithInput.java that is a copy of the original Temperature.java.)
Edit the file TemperatureWithInput.java to use input from the user. In this case, the user should type in the total number of ounces. As a matter of style, the program should ask the user a question before reading the input. And don't forget to change the name of the program to match the name of the file!
For the sixth exercise, you should make a copy of your keg party program, using a different name, and edit the copy to do use input. You should decide which values you want to input from the user and which ones should be built into the program as constants.
Turning in Your Work
After doing all the assignments in this lab, you should have a directory named "lab1" that contains six programs. For each program, there should be a ".java" source code file and a ".class" compiled file. The directory will also contain TextIO.java and TextIO.class.
To complete the lab, you must copy your entire lab1 folder into your homework directory in /classes/cs124/homework. This must be done by the beginning of next week's lab. To use the command line, make sure that cs124 is your current directory, and use a command like:
cp -r lab1 /classes/cs124/homework/zz9999
but replacing zz9999 with your own username. (This is the last time I will give you explicit instructions for submitting a lab directory. Please make sure that you understand this command and can remember how to use it.)