Strings and Loops
- Due Jul 1, 2020 by 4pm
- Points 20
- Submitting a file upload
Part 1: Phone Words
Write a program named PhoneWord.java
that prompts the user for a “phone word,” an alphabetic mnemonic for a phone number. Then, print out the phone number corresponding to that sequence.
Here is how your program must translate letters to numbers:
ABC | 2 |
DEF | 3 |
GHI | 4 |
JKL | 5 |
MNO | 6 |
PQRS | 7 |
TUV | 8 |
WXYZ | 9 |
You must accept letters in either upper or lower case. If the phone word translates to more than seven digits, keep only the first seven. If the phone word translates to fewer than seven digits, print an error message. Ignore any characters other than a letter or digit.
Here is an example of several runs of the program:
Enter a phone word: warbler The number is 9272537. Enter a phone word: GOOD4U2 The number is 4663482. Enter a phone word: OMG Your phone word is not long enough for a phone number. Enter a phone word: GREAT DEALS The number is 4732833.
Extra challenge: Print the phone number with a hyphen, for example 473-2833
.
Part 2: Statistics
Write a program named Statistics.java
that repeatedly asks a user to enter a price of an item or -1 when finished. When the user enters -1, your program will compute and display the total number of items, the average price, and the standard deviation of the prices. Here is the formula for standard deviation:
If the user enters -1 right away, print a message that you cannot calculate any statistics. If the user enters only one number, calculate the average, but give a message that you cannot calculate a standard deviation.
Here is some sample output from running the program several times:
Enter first price, or -1 to quit: $15.20 Enter next price, or -1 to quit: $16.45 Enter next price, or -1 to quit: $17.00 Enter next price, or -1 to quit: $15.85 Enter next price, or -1 to quit: $-1 Number of items: 4 Average price is $16.13 Standard deviation of prices is $0.78
Enter first price, or -1 to quit: $34 Enter next price, or -1 to quit: $-1 Number of items: 1 Average price is $34.00 Cannot calculate standard deviation for one item.
Enter first price, or -1 to quit: $-1 Number of items: 0 No data entered. Cannot calculate statistics.
Hint: Your program needs variables to keep track of the number of items entered, the sum of the items, and the sum of squares. Every time you get a price (other than -1), your program will have to add one to the total number of items, add the price to the sum, and add the price squared to the sum of squares. Your program should not calculate the average or standard deviation until all the numbers have been entered. Your program may not use an Array or ArrayList as part of the solution.
Rubric
Criteria | Ratings | Pts | |||
---|---|---|---|---|---|
Phone word input
threshold:
pts
|
|
pts
--
|
|||
Phoneword handles digits
threshold:
pts
|
|
pts
--
|
|||
Keeps only 7 digits in phoneword
threshold:
pts
|
|
pts
--
|
|||
Error for less than 7 digits in phoneword
threshold:
pts
|
|
pts
--
|
|||
Phoneword ignores bad data
Ignores *all* non-letters and non-digits
threshold:
pts
|
|
pts
--
|
|||
Phone output
threshold:
pts
|
|
pts
--
|
|||
Phoneword programming style
Includes names of variables, indenting, efficiency (using 36 if statements would be extremely inefficient)
threshold:
pts
|
|
pts
--
|
|||
Statistics input
threshold:
pts
|
|
pts
--
|
|||
Statistics number of items
threshold:
pts
|
|
pts
--
|
|||
Statistics average
threshold:
pts
|
|
pts
--
|
|||
Statistics standard deviation
threshold:
pts
|
|
pts
--
|
|||
Statistics output
threshold:
pts
|
|
pts
--
|
|||
Statistics programming style
Includes names of variables, indenting, efficiency
threshold:
pts
|
|
pts
--
|
|||
Total Points:
20
out of 20
|