CSCE 155H
Assigned: August 30, 2004
Due: Start of Class,
September 13, 2004
Note: This assignment is to be
completed individually - collaboration is strictly prohibited.
Points: 100 points (Programming: 85 points; Puzzle: 15 points)
Use
Java to construct a simple interactive, guessing game. In this game, there are
10 boxes ordered from left to right and a ball is stored (randomly) in one of
the boxes. The objective is to let user
guess in which box the ball is stored.
For each guess, the program reports whether the guess is on the left or
right to the correct box. When the user guesses the correct box, the program
prints a message indicating success and terminates execution. A sample run of
the program is shown below.
|
Sample Output |
|
Welcome
to the Guessing Game. There are 10 boxes
(from 1 to 10) for you to guess where a ball is stored. With each guess, I will tell you whether
your guess is to the left or right of the correct box in which the ball is
stored. The object of the game is to
find the correct box using as few guesses as possible. Let’s
start! Enter
your guess <or 0 to quit>: 5 Your
guess is to the right of the correct box! Enter
your guess <or 0 to quit>: 3 ***
Correct! You got it in 2 guesses! |
You can download the assignment files as a zip file, Homework1.zip, or as a tar file, Homework1.tar from the course web page. Create a folder called Homework1 in a safe place on your computer and use zip a utility such as WinZip or the tar utility (tar -xvf homework1.tar) to uncompress the files into your folder. When this is complete, the folder will contain a skeleton BlueJ project that you can edit to complete this assignment. To work on the project, open GuessingGame.java in the Homework1 folder using BlueJ (or IDE of your choice). The project contains a single Main class with several class (static) methods, including a main method. The project can be compiled and run as distributed, but only prints a single line of text for the game. Your job is to insert the missing functionality.
You need not be concerned with how to read input from the keyboard – this part of the program has been implemented in the getInput() method supplied with the program.. Note that import java.io.* is required at the head of the Main class to provide access to the methods used within getInput().
You will also find two class files included with this assignment: GuessingGame.class and GuessingGameDemo.class in the Homework1 folder. The GuessingGame.class file is simply a ready-to-run version of the code you are starting with. You may execute GuessingGameDemo.class to observe how your program should behave. In this version, the computer chooses numbers between 1 and 10 (as shown above) to keep the program simple. To make your own program more interesting, you may want to set the range to 100 boxes, which will cause the program to pick numbers in the range 1 to 100, inclusive.
While your program should generate output that closely resembles the output of the demo program, it is not necessary to precisely match the spacing and formatting of the sample program. Keep in mind however, that your output should be easy to understand, and free from spelling, punctuation and grammatical errors.
The generation of random numbers is quite simple in Java.
The class method Math.random()
returns a random double
r such that 0.0 <= r < 1.0, each time it is called. The
class Math
is implicitly available in Java, so no import statement is required to
use this method. Note: you will need
to cast (convert) the double
returned by the random() method to an int.
An example of
casting from type double to type int:
double aDouble = 2.3;
int aInteger = (int) aDouble;
The value of aInteger will be 2.
Several simple modifications can improve your program:
There is a knapsack that can hold K balls. You are given N balls. Each ball i has a value
. Your task is to
find select at most K balls to put into the knapsack such that the
knapsack will have the highest total of values.
So for example, if K = 3, and you are given 5 balls,
with the following values:
= $3,
= $5,
= $1,
= $3, and
= $2. Then the best solution is to pick ball 2,
ball 1, and ball 4, as this combination of three balls yields a total of $5 +
$3 + $3 = $11.
Write an algorithm that will assign find the best combination (yielding the highest total of values) for the knapsack.