  Spring Semester 2005

Lab Notes Seven: Loops, `String`s, `StringTokenizer`s.

In this lab you will have to implement three problems.

Some help will be offered (posted) but Tuesday afternoon.

Here are the problems:

1. Implement a vending machine.
2. Implement a standard deviation calculator (I).
3. Implement a standard deviation calculator (II).

In all three cases you will need to make use of `StringTokenizer`s.

Here are now the details on the above problems:

1. The Vending Machine. (The `Vendor` Program)

Write a program that simulates a vendor that sells books of stamps which are worth 3 dollars and 40 cents each. (The vendor only sells whole books, each book is \$3.40). Your program (called `Vendor`) starts by greeting the user, then accepting money. Your vendor accepts the following monies only:

• `cent`
• `nickel`
• `dime`
• `quarter` and
• `dollar`
Your vendor accepts money one line at a time, and should allow more than one coin to be entered on a line. After that, the program calculates and prints the amount that is still needed to purchase the item. When the amount finally exceeds 340 cents the program prints the change (if any) and thanks the user for using the program. In the example below what the customer enters is rendered in blue.

Example run:

```prompt> java Vendor
Welcome. We sell stamps (\$3.40) Please enter money:
enter> nickel nickel dollar dollar quarter
Thanks. Your credit is \$2.35 I need \$1.05 more.
enter> cent cent
Thanks. Your credit is \$2.37 I need \$1.03 more.
enter> dollar quarter
Thanks. Your credit is \$3.62 The stamps are yours.
Thanks for using this program. ```

• Initially you start from having received nothing (zero).
• With each line you need to use a `StringTokenizer`.
• That's because more than one coin might be specified per line.
• So you need a loop for each line, to look at the words.
• Every word looked at translates into a coin value.
• After every line you check to see if you have enough money.
• If not, you keep going, asking for more (and indicating how much).
• So the loop for each line is inside a bigger loop for the conversation.
• But you knew that, already!
2. The Standard Deviation (Part I)

Write a program that reads a set of floating-point data values from the input.

When the end of file is reached, print out the count of the values, the average, and the standard deviation.

The average of a data set is The standard deviation is However that formula is not as suitable for our task.

By the time you have computed the mean, the individual `xi`'s are long gone.

Until you know how to save these values, use the numerically less stable formula You can compute this quantity by keeping track of the count, the sum, and the sum of squares as you process the input values.

3. The Standard Deviation (Part II)

Use a `StringTokenizer` to go twice through the values, using the first formula.

```frilled.cs.indiana.edu%javac StdDev.java
frilled.cs.indiana.edu%java StdDev
Numbers> 90 90 92 86
4 numbers, average is: 89.5
Standard deviation is: 2.516611478423583
Numbers> 4 4 4 1
4 numbers, average is: 3.25
Standard deviation is: 1.5
Numbers>
0 number, average is: NaN
Standard deviation is: NaN
Numbers> 1
1 number, average is: 1.0
Standard deviation is: NaN
Numbers> done
frilled.cs.indiana.edu%```

Last updated: Sun Feb 20 19:36:07 EST 2005 by Adrian German for A201