Computer Science 161

Fall 2008

Laboratory exercise #2

October 30, 2008

 

A laboratory exercise in simple interaction with the user.  I will say some words at the start of the session, and then spend my time wandering around to give help where asked.

What you'll need:

Copy these into your workspace before we begin.

The Scanner class:

The Scanner class gives us a basic way to communicate with the user through the terminal window.  It is a method which calls us back to the early days of interactive computing (1970's) when users interacted with a running program through a video terminal or typewriter console.

Open up the Scanner01 class. What we will do in this class will be to write a constructor and a method which will ask the user for their name, and return a greeting to the user.

Some basics: 

 In the constructor for this class, set reader to be a new Scanner.  The Scanner class has a number of constructors (we may use this later to read input from a file, for example).  For now, we want the input to come in from System.in (the terminal window), so we write

Write a void method called hello which does the following

Try this out a bit.

For further reading

If you do this during the lab session, I will be able to help with any difficulties.

Exercise

(Due Tuesday, November 4):  Write a new public void method for your Inventory class called removePart which takes as input the index of a part in your inventory ArrayList.  Check to see that the index is in range (>= 0, < inventory.size()).  Retrieve the name of the part and use it in a message to the user asking if the user to confirm that that part is to be deleted.  If the first character of the user's response is a 'Y' or a 'y' delete the record (inventory.remove(index)) otherwise do nothing.  In either case, write a brief message saying that the part has or has not been deleted.  For this exercise, turn in a listing of the Inventory class with the added method.

Many thanks!