CSE-1020: Introduction to Computer Science I
York University
Midterm Labtest
GuessingGame
2:30pm Thursday 22 October 2009
(Lab-04)
  Welcome to the Labtest Environment

During the test, you will not be able to access your regular home directory, access, receive, or send e-mail, print, or access the Internet.

At the end of your test session, this machine will be converted back to a standard, unrestricted Prism Lab machine. At this time, any files written by you during the test, except those that have been submitted, will not be recoverable. Make sure you submit your code before the end of the test . (Instructions to submit your code are below). If you do not submit your work on time, you will receive a grade of zero. (There are no exceptions).

Time Limit

You have about 80 minutes to complete this test (regular lab time). The computers will automatically shutdown at the end of the lab (e.g., 3:50pm), warning you several minutes in advance. No additional time will be provided, so you must submit your work prior to this; otherwise, there will be no way to recover it (it will be lost), and you will receive a grade of zero (no exceptions).

You are encouraged to submit often during the test. Newer submissions overwrite older ones.

Submitting Your Work

When you submit a file, you must include at the top of the file your name (surname, given name) and your Prism lab login. These should be placed in a comment so that the file will compile. Note that files which do not compile will receive a large penalty when marked, no matter how small the error that prevented compiling.

Submit the class FractionStatistics.java before the time deadline. Here is the command to submit your work:

% submit 1020 midtermR2.30 GuessingGame.java

(Further details regarding the submit command can also be obtained by typing man submit.)

Once again, you are encouraged to submit regularly. Newer submissions simply overwrite older ones.

Unlike eCheck assignments, there is partial credit possible. If you are able to finish all aspects of the program (for example, say, input validation was requested but you did not do this), you should still check it in.

Useful APIs

Here are the common APIs that you may access:

 
  The Task

Write a Java application called GuessingGame.java.

You have been asked to write a program to help teach kids about fractions. You have decided to implement a simple game where the user tries to guess the value of a fraction (its numerator and denominator) using at most 3 guesses. The program asks the user to guess the fraction. If the guess is

  • smaller than the target fraction, the program tells the user that the guess was too small
  • larger than the target fraction, the program tells the user that the guess was too big
  • the same as the target fraction, the program tells the user that the guess was correct and stops running.

If the user fails to guess the correct fraction after 3 guesses, the program tells the user what the target fraction was and stops running.

To test the program correctness, you decide to use fractions of the form 1/d where d is a user-specified integer value between 1 and 40. If the user enters a denominator outside of this range, an exception is thrown using ToolBox.crash.

Sample Runs

Here are some sample runs of a correctly written program. Anything typed by the user is shown in red. Your exception message might report a different line number in GuessingGame.java than shown.


% java GuessingGame
Enter the test fraction denominator ... 10
The test fraction is 1/10

Enter guess 1 ... 1 15
1/15 is too small.

Enter guess 2 ... 1 5
1/5 is too big.

Enter guess 3 ... 1 8
1/8 is too big.

Sorry, out of guesses. The fraction was 1/10
%
% java GuessingGame
Enter the test fraction denominator ... 12
The test fraction is 1/12

Enter guess 1 ... 1 10
1/10 is too big.

Enter guess 2 ... 1 12
1/12 is correct!
%
% java GuessingGame
Enter the test fraction denominator ... -5
Exception in thread "main" java.lang.RuntimeException: Denominator out of range.
        at type.lib.ToolBox.crash(ToolBox.java:25)
        at GuessingGame.main(GuessingGame.java:16)
%
% java GuessingGame
Enter the test fraction denominator ... 41
Exception in thread "main" java.lang.RuntimeException: Denominator out of range.
        at type.lib.ToolBox.crash(ToolBox.java:25)
        at GuessingGame.main(GuessingGame.java:16)
%

Coding

You should use good programming practices as described in the textbook, and your code should conform to the style guide in the textbook. Your code should be adequately commented.