CSc 2310 Principles of Computer Programming I
Spring 1999
Programming Assignment #4
Due: 22 March 1999 (Monday)

  1. Write a Java program, called Scores.java, which reads a collection of exam scores (integers) ranging from 1 to 100 and counts and prints the number of `A' scores, `B' scores, `C' scores, `D' scores and `F' scores based on the following grading scale: A: 90 to 100; B: 80 to 89, C: 70 to 79, D: 60 to 69 and F: 1 to 59. The program should also print the number of scores, the average score, the maximum score, and the minimum score. A sample run of the program is shown below:

    A:> java Scores
    Enter exam scores one per line (enter 0 to quit):
    63 
    75 
    72 
    72 
    78 
    67 
    80 
    63 
    75 
    90 
    89 
    43 
    59 
    99 
    82 
    12 
    100
    0
    
    Number of "A" scores =  3
    Number of "B" scores =  3
    Number of "C" scores =  5
    Number of "D" scores =  3
    Number of "F" scores =  3
    -------------------------
    Total Scores         = 17
    
    Average Score = 71
    Maximum Score = 100
    Minimum Score = 12

  2. Write a Java program for the game of BULLS and COWS, called Game.java. The objective of this game is to make the user (player) guess a 4-digit number which is randomly generated in the program. The 4-digit number should not have repeating digits and will be in the range 1000 to 9999. The player will be given a maximum of 10 tries to guess. The player WINS if he/she guesses the number within 10 tries; otherwise the player LOSES the game.

    The main program should first display a Welcome Message explaining the rules of the games. Then, it should generate a 4-digit random number between 1000 and 9999 with no repeating digits. After that, it should repeatedly ask the user for his/her guess and report the BULLS and COWS for the guess compared with the random number. The game stops when either the player has made a correct guess or when the player had made 10 guesses.

    You should make use of the class methods which you have written for the previous assignment. You will need two additional class methods:

    public static int produceRandomTarget()
    public static void printWelcomeMessage()

    The game interaction should look something like this:

    Welcome to the game of BULLS and COWS.
    The Objective in this game is for you to guess a 4-digit number
    The computer responds with how close your guess is to the target
    BULLS = # common digits with exact matches and
    COWS  = # common digits in wrong position.
    
    1.  Enter your guess: 1234
        Bulls = 1  Cows = 0
    2.  Enter your guess: 8567
        Bulls = 0  Cows = 1
    3.  Enter your guess: 1233
        Your guess should not contain repeating digits
    4.  Enter your guess: 7654
        Bulls = 0  Cows = 1
    5.  Enter your guess: 7642
        Bulls = 0  Cows = 0
    6.  Enter your guess: 5913
        Bulls = 1  Cows = 2
    7.  Enter your guess: 5091
        Bulls = 2  Cows = 1
    8.  Enter your guess: 5039
        Bulls = 4  Cows = 0
    
        Congratulations; You Won!!
    or a message saying that you lost and PRINT THE ACTUAL NUMBER.



Raj Sunderraman
Fri Mar 5 19:04:39 EST 1999