CSCE 155H

Homework Assignment 1:  Where Is the Ball?

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)

 

Problem Description

 

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!

 

 

Supplied Solution Components

 

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.

 

Random Numbers in Java

 

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.

 

Challenge - Extra Credit (10 points)

 

Several simple modifications can improve your program:

  1. Add a feature to allow the user to play multiple games without restarting the program.
  2. Keep track of how many games the user has played and how many total guesses the user has made. Use the DisplayStatistics() method to output the number of games played and the average number of guesses per game. Note: you will also need to modify other methods in the program to implement this functionality. 

 

Puzzle Assignment 1

 

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. 

 

Submission Procedure

 

This assignment is due Monday September 13, 2004 at the start of class. Your grade will be reduced by 10% for each day it is late. It is recommended you read the grading policy and grading guidelines on the course website for a complete explanation of how the assignments will be graded. Remember, your program should follow good programming style, include plenty of comments, and perform all of the functionality outlined above.

After completing the assignment, you must “handin” the following files on-line:

1.      Source file: GuessingGame.java

2.      Compiled file: GuessingGame.class

3.      Readme file: README.TXT

4.      The document that contains the algorithm to for the puzzle.  You may include descriptions of the algorithm to make it clearer: algorithm.doc.

 

In addition, you must submit a stapled paper copy of your source code (and README file) and your solution to the puzzle assignment.  Both these steps must be done by the start of the class on the day the assignment is due.