CS 124, Spring 2021
Lab 1: Introduction to Java

This is an ambitious first lab, since it introduces you to a lot of background material. You should not expect to complete all of the lab exercises during the lab period. How much you get done will depend on how comfortable you are using a computer, whether you have had any previous programming experience, and how much preparation you have done. Hopefully, you have already set up an Eclipse workspace and made sure that you know how to write and run programs in it; if not, you will need to do that first. And hopefully you have read these rather long lab instructions in advance. But remember that you have a full week to complete the exercises, and that you can get help from Teaching Fellows and during office hours.

(Ordinarily, your work for a lab will be due at the next lab. Since this is the first lab, you will have a few days longer to turn in your work. And depending on how things are going, that time might be extended a bit further.)

Submitting Your Work From This Lab

Your work from this lab should be submitted by the beginning of next week's lab, using the Submit web page at http://math.hws.edu/submit. This is a small web app that will allow you to upload your work. You have a user name for the site, which is the same as your HWS user name — two letters and four digits, similar to de1631. You have not been assigned a password for the site; you will need to generate one before you can log on. To do that, click on the link labeled "Click here if you forgot your password or would like to change it," at the bottom of the login form. You will need to enter your user name. You will receive an email in your HWS email account that will let you enter the password that you want to use. Once you have logged in, you will see something like this:

You will submit four short Java programs, one for each of the four exercises in this lab. Note that for the site to accept your programs, they must be named Pounds.java, RootBeer.java, RootBeerWithInput.java, and Conversation.java.

When you are ready to submit a program, you should log into the site. Click on the "Browse" button for the program that you want to submit. Or browse for several programs if you want to submit more than one. Then click submit. You will see a report on the success or failure of the submission.

When you browse for a file that you want to submit, you will need to know where your Eclipse workspace is located on your computer. You should browse to the Eclipse workspace folder, then into the folder with the same name as the project that contains the program, then into the src folder inside that project folder. That src folder contains your Java files for the project.

You do not need to submit all the programs at the same time. You can resubmit a program as many times as necessary, if you need to make corrections. The site does not check that a program runs correctly, only that it can be successfully compiled. This means that the site checks that the program is capable of being run, but not that it does the correct thing when it is run.

After completing Exercise 1, you might want to submit the file Pounds.java to make sure that you know how to use the program submission site.

Setting Up Eclipse

You received an email last week about setting up the Eclipse programming environment on your computer. If you have not already done that, you will need to do so before you can complete the work for this lab.

To prepare for Exercises 1 and 2, you should complete the setup through the section titled "Write and Run a Java Program" in the setup instructions, which can be found at


That section shows how to create and run a Java program named "HelloWorld.java". You will need to create a new Java program for each of the four exercises in this lab.

To prepare for Exercises 3 and 4, you should complete the section titled "Using TextIO for User Input" in the setup instructions. That section explains how to get the file TextIO.java and add it to a Java project. It is also advisable, though not essential, to do the next section in the setup, titled "Using TextIO for User Input," which gives an example of creating a program that uses TextIO.

Exercise 1: Preparing for Exercise 1

As explained in the Eclipse setup instructions, your Java programs are stored in "projects" in a "workspace". You should already have a workspace set up with at least one project, and you should be working in the workspace. You might want to start a new project named Lab1 for this lab. However, no particular name is required, and you could also put your work in a project that you have already created. Note that you do not need a new project for each exercise, since a project can contain any number of programs.

To begin Exercise 1, create a new class named Pounds. To be able to submit your work, you must use this name exactly, with the "P" in uppercase and the rest of the letters in lowercase. The program will be stored in a file named Pounds.java.

You should remove all of the text from Pounds.java and replace it with the following text. You do not have to retype this; you can copy-and-paste it from this web page into the Eclipse window.

public class Pounds {

   public static void main(String[] args) {
      int feetPerMile;
      double miles, feet;
      miles = 2.5;
      feetPerMile = 5280;
      feet = miles * feetPerMile;
      System.out.print("The number of feet in ");
      System.out.print(" miles is ");


Run the program to see what happens. (You just get one line of output in the Eclipse Console.)

The actual program instructions come after the line public static void main... You are not supposed to understand what this funny line means at this point, but you do need to understand the instructions starting with int feetPerMile. The instructions tell the computer what to do when the program is run. Here is a brief explanation of what they mean. For full details, see the textbook.

A program must be able to do computations, and it must be able to show results to the user. In Java, the values that are used in computations are stored in variables. Variables have names. This program uses three variables, named feetPerMile, miles, and feet. The command "int feetPerMile;" creates a variable named feetPerMile and tells the computer that the value of the variable will be an int. That is, the value will be an integer such as 17, -42, or 5280. The next line, "double miles, feet;", tells the computer that that there are two variables, named miles and feet, whose values are doubles. That is, their values are real numbers such as 3.14159, 17.0, or 2.5. (The word "double" is a funny name for real numbers, but that's just the way it is in Java.)

Values are stored into variables using assignment statements, such as "miles = 2.5;". The left side of the "=" in an assignment statement is the name of a variable. The right side is a value (followed by a semicolon). The value can be a constant such as 2.5 or an arithmetic expression such as miles*feetPerMile. An expression computes a value from other values, which can be given as variables or constants. ("2.5*feetPerMile" would also be a legal expression.) Among other things, expressions can use the operators +, −, *, and /. A "*" represents multiplication, and a "/" represents division.

To show a value to the user, a program can use the commands System.out.print and System.out.println. The difference between them is that System.out.println outputs a carriage return after the value, whereas with System.out.print, the next output will continue on the same line.

You can output a string, that is, some text enclosed between quotes. The text that is inside the quotes is shown to the user. You can also output a variable, as in "System.out.print(miles);". This statement outputs the value of the variable named miles. The value of the variable is shown to the user. (If you wanted to output the actual word miles instead, you would have to put the word in quotes.) It's possible to output more complicated things, but we'll leave it at that for now.

Exercise 1: Outputting Data and Using Variables

A funny thing about the Pounds program is that it has nothing to do with pounds.

For a short Exercise 1, you should edit Pounds.java so that the so that instead of converting 2.5 miles into feet, the modified program will convert 178 ounces into pounds. Change the names of the variables so that they make sense for the new problem! As you edit the program, you will introduce errors into the program, which will be noted by Eclipse. You will get some errors as soon as you start changing variable names. Try to understand what some of the errors mean. You will have to eliminate all the errors before you will be able to run the program again.

You will need the fact that one pound contains 16 ounces. Note: For this program, you have to divide, not multiply. Multiplication in Java is indicated by "*"; division is indicated by "/". And you have to be clear about what it is you need to divide to get the right answer.

Exercise 2: Root Beer Computation

Let's say you're a planning a party for all of HWS's 2061 students (to celebrate the end of the pandemic). You want to have 3 glasses of root beer available for each student. A keg of root beer costs $87.69 and serves 210 glasses. How many kegs should you buy, and how much will they cost? You should write a program that computes both values and tells the user the answers. The Java file must be named RootBeer.java, which means that you must start by creating a new class named RootBeer. The outline of the program must look like

public class RootBeer {

   public static void main(String[] args) {

You will fill in the instructions for the program, after the public static void main line. You will need to declare variables, give them initial values, do some computations, and output results to the user. This is the same basic outline as Pounds.java, but this program is longer. You can use the following code in your program to give initial values to the variables that represent the basic data needed for the computations.

people = 2061;
glassesPerPerson = 3;
glassesPerKeg = 210;
costPerKeg = 87.69;

You should expect to declare additional variables to hold results of computations.

Preparing for Exercise 3

Exercises 3 and 4 use TextIO for input. As explained in the Eclipse setup instructions, this means that your project must contain the file TextIO.java, inside a folder named textio, inside the project's src folder. If you already have the textio folder in another project, you can just copy-and-paste it from that project into the project that you are using for this lab. (That is, find textio in the Package Explorer on the left edge of the Eclipse window; right-click it and select "Copy"; right click on the destination src folder in the Package Explorer; and select "Paste".) If you do not already have textio in an existing project, you will need to create it, as explained in the Eclipse setup instructions. Here is a sample program that uses TextIO:

import textio.TextIO;

public class Square {

   public static void main(String[] args) {
       double side, area;
       System.out.print("What is the length of a side? ");
       side = TextIO.getlnDouble();
       area = side * side;
       System.out.print("The area of the square is ");

Any program that uses TextIO must start with the command "import textio.TextIO;". TextIO can then be used on the right-hand side of an assignment statement to get a value from the user and store that value into a variable. For this lab, there are three different TextIO statements that you can use, depending on the kind of variable you are using. To get a value for a double variable, use TextIO.getlnDouble(). To get a value for an int variable, use TextIO.getlnInt(). To get a value for a String variable, use TextIO.getln(). We have not yet seen any String variables, but they are simply variables whose values are strings of text. Here is an example of some commands that read some text from the user into a String variable, named color:

String color;

System.out.print("What is your favorite color? ");
color = TextIO.getln();

System.out.print("I also like ");

(Of course, there's no guarantee that the user will actually type in a color name in response to the question!)

By the way, any time you get input from the user, you should first output some kind of question or prompt to let the user know that the input is expected. Otherwise, the program will simply stop doing anything, while waiting for the user to type some input, but the user will have no idea that they are expected to type anything!

When you run a program in Eclipse, the output appears in the Console at the bottom of the Eclipse window. When the program is waiting for input, the user (who is probably you) must first click in the Console to make sure that their typing goes to the correct place. After typing their input, they must press return.

Exercise 3: Using TextIO for Input

The program RootBeer.java that you wrote for Exercise 2 is not very useful, since it does the same calculation every time it is run. Make a new version of the program that uses TextIO to get initial values for the variables people and costPerKeg, instead of just assigning constant values to those variables. This means that the answers computed by the program will depend on the values that the user types in when the program is run. The new program must be named RootBeerWithInput.

Instead of writing a whole new program, you should just make a copy of RootBeer.java: In the Package Explorer on the left side of the Eclipse window, find the name of the file "RootBeer.java" and right-click it; select "Copy" from the popup menu. Then right-click again and select "Paste" from the popup menu. You will be asked to enter a name for the new class. Enter RootBeerWithInput, and click "OK". The file named "RootBeerWithInput.java" will appear inside the default package inside the src folder. (If you pasted it somewhere else by accident, you can just drag it into the correct position.) The new file will not be automatically opened for editing — double-click on the file name to open it.

Remember that before reading the user's input, a program should prompt for that input by asking a question. That is, you need a System.out.println or System.out.print statement, followed by an assignment statement that uses TextIO to read a value for a variable.

Note that this is really a very short exercise.

Exercise 4: Converse With Your Computer

For your final assignment, you should design and write a program that will hold a conversation with the user. The name of the program must be Conversation.java. The program will use TextIO to get the input from the user. The user's responses will be stored in variables. The output from the program will be based partly on the user's responses.

The topic and the structure of the conversation are up to you. Requirements are: You should ask the user at least four questions (and read the user's responses). More questions would be even better! Some of the responses should be numbers, and you should do at least two calculations using the input numbers. (For example, you could ask what year the user was born and have the computer compute the user's age.) At least one of the responses should be a string, such as the user's name or favorite color. You should use the user's inputs somehow in your outputs. (For example, if the user says their favorite color is blue, then the program might output "How about that, Fred, blue is my favorite color too.")

Your grade will be partly based on how ambitious the program is and how natural and interesting the conversation is. Feel free to be amusing, obnoxious, philosophical, or whatever. Don't forget that you have to do two computations with the user's input! A conversation might go something like this:

 Hi, my name is Kirk.  What's your name?
 Gee, Spock is a funny name.
 Say, you look young to be a StarFleet Science officer.
 How many years old are you?

 Let's see, this is the year 2471, so you must have been born in 2284.
 That was a good year.

But you're not required to duplicate this conversation. The assignment is to make up your own!