Assignment #3
Due: Tues, June 10
Objective
This assignment will involve practice with loops.
Task
Write the following programs, each in a separate file. Filenames should
be:
- Numbers.java
- GuessingGame.java
(Make sure to use these exact filenames).
Exercise 1
Filename: Numbers.java
Write a program that will allow the user to enter a set of integers (as
many as they want), then prints out a summary of information, as follows:
- Prompt the user continually to enter a number, until the value 0 is
entered, which will be the signal to stop entering values. (This is
sometimes known as a "sentinel" value).
- Once the sentinel value has been entered, print out the following
information:
- The number of positive values that were input
- The number of negative values that were input
- The sum of the input values
- The average (to two decimal places) of the input values
- Your output must match mine exactly. See the sample runs below
Sample Runs
(user input is underlined, to distinguish it from output)
Sample Run 1
Input integer (0 to stop): 12
Input integer (0 to stop): 4
Input integer (0 to stop): -1
Input integer (0 to stop): -5
Input integer (0 to stop): 18
Input integer (0 to stop): 0
# of positives = 3
# of negatives = 2
Sum = 28
Average = 5.60
Sample Run 2
Input integer (0 to stop): 4
Input integer (0 to stop): 8
Input integer (0 to stop): 24
Input integer (0 to stop): 94
Input integer (0 to stop): -1
Input integer (0 to stop): 43
Input integer (0 to stop): 13
Input integer (0 to stop): 0
# of positives = 6
# of negatives = 1
Sum = 185
Average = 26.43
Exercise 2
Filename: GuessingGame.java
Write a program that plays a Hi-Lo guessing game with the user. It
should work as follows:
- Pick a random integer in the range of 1 through 100.
- Repeadedly prompt the user to enter a guess, and allow the user to
type in their guess
- For each guess the user enters, report to the user the
appropriate information -- that they are corerct, or that their guess
is too high or too low
- When the user guesses the correct value, include the number of
guesses it took in the output message
- Your output messages should match the ones in the sample runs
below
Sample Runs
(user input is underlined, to distinguish it from output)
Sample Run 1
Enter a guess (1-100): 50
You guessed too low
Enter a guess (1-100): 70
You guessed too low
Enter a guess (1-100): 80
You guessed too low
Enter a guess (1-100): 90
You guessed too low
Enter a guess (1-100): 97
You guessed too high
Enter a guess (1-100): 94
You guessed too low
Enter a guess (1-100): 95
You guessed too low
Enter a guess (1-100): 96
Correct! You guessed the number in 8 guesses.
Sample Run 2
Enter a guess (1-100): 50
You guessed too low
Enter a guess (1-100): 80
You guessed too high
Enter a guess (1-100): 60
You guessed too high
Enter a guess (1-100): 55
You guessed too high
Enter a guess (1-100): 53
You guessed too low
Enter a guess (1-100): 54
Correct! You guessed the number in 6 guesses.
Requirements for both programs
- Use the Scanner class for input
- When you write source code, make sure it is clear, readable, and
well-documented. (See style guidelines posted on home page)
Submitting:
Submit the files through the Assignment 3 Blackboard link. Make sure you
attach BOTH files before you click Submit:
Numbers.java
GuessingGame.java