Midterm Exam

CMSC 220

SPRING 2002


Problems :



 1.
Class Guessing (attached below) has four data fields :

    private int secretNumber;
            secret random number between 3 and 12 (i.e. 9).
    private int numberOfAttempts = 0;
            number of attempts the ser has made so far in guessing the secret number (initially 0).
    private int guessNumber = 0 ;
            number that the user guessed (intialized to 0).
    private final static int MAXNUMOFGUESSES = 5;
            maximal number of guesses that the user is allowed to have (5).

Its member methods have the following meaning :

    public Guessing()
            constructor method producing a secret int between 3 and 12 and storing it into the data field
            secretNumber.
    public Guessing copy()
            copies this Guessing object into another Guessing object and returns the reference to the newly
            created object (i.e. in Guessing gO1 = new Guessing();  Guessing gO1Copy = gO1.copy();
            gO1Copy is a different Guessing object but containing the same secretNumber component as
            the gO1 object.
    private int produceSecretNumber()
            produces and returns a random int whose value is between 3 and 12 (including the boundaries).
    private int askForGuess()
            first prepares and displays the question for the user. After the user provides a guess, it reads it and
            returns its value.
    public void play()
            allows the user to play the guessing game by repeatedly prompting for a guess. After the guess is provided, it
            is stored into the data field guessNumber, the value in numberOfAttempts is updated and the user is
            informed if the guess was too low, too high or right by displaying the corresponding message ("Too High",
            "Too Low" or "CORRECT") in a message dialog box. The game ends when the user provides a correct
            guess or the number of attempts exceeded MAXNUMOFGUESSES (5).
    public gameOver()
            announces that the game is over, its winning status, the number of attempts and the correct value.

    Add missing Java source code into the class Guessing methods (where requested by the presence of the comments // YOUR CODE) to get the methods to behave as follows :

    (i)    Design private int produceSecretNumber() method so that it produces and returns a random int whose value is between 3 and 12 (including the boundaries),
    (ii)    Write the code for the method public Guessing() so that this code will be getting the random int between 3 and 12 into the data field secretNumber using the services of the method from (i).
    (iii)   Write the code for the the method private int askForGuess() that prepares and displays the question for the user. After the user provides a guess, it reads it and returns its value.
    (iv)    Write the missing code for the sections (1) and (2) of the method public void play() so that this code will get the method to behave as it has been described above.
    (v)    Write the code for the method public gameOver() so that this code will congratulate the user (saying a sentence like "Your answer 11 is correct and it took you 3 attempts. Congratulations.") or inform him/her that the answer was wrong (by saying sentences in the following format : "Your answers were wrong. The correct answer is 11.").
    (vi)    Create an application class "GuessingApp" that (by calling corresponding member methods) :
            (a)    creates an instance of the "Guessing" class,
            (b)    instructs it to play the guessing game, and finally,
            (c)    displays the results.
 

Attachment :

import javax.swing.*;

public class Guessing {

    private int secretNumber;
            // secret random number between 3 and 12 (i.e. 9).

    private int numberOfAttempts = 0;
            // number of attempts the ser has made so far in guessing the secret number (initially 0).

    private int guessNumber = 0 ;
            // number that the user guessed (intialized to 0).

    private final static int MAXNUMOFGUESSES = 5;
            // maximal number of guesses that the user is allowed to have (5).
 

    public Guessing() {
           // (ii) YOUR CODE
           secretNumber = produceSecretNumber();
     }

    public Guessing copy() {
        Guessing copyObj = new Guessing();
        copyObj.secretNumber = this.secretNumber;

        return copyObj;
    }

     private int produceSecretNumber() {
           // (i) YOUR CODE
           return (int) (Math.rand() * 10.0) + 3.0);
     }
 

    private int askForGuess() {
           // (iii) YOUR CODE
            String guessStr = JOptionPane.showInputDialog ("Please, enter your guess (number between 3 and 12) :");
            return Integer.parseInt(guessStr);
    }
 

    public void play() {
            do {
            // (iv) (1) YOUR CODE
            } while // (iv) (2) YOUR CODE
    }

    public void play() {
            do {
                guessNumber = askForGuess();
                numberOfAttempts++;
                if (guessNumber == secretNumber)
                    JOptionPane.showMessageDialog (null, "COREECT");
                else if (guessNumber secretNumber)
                    JOptionPane.showMessageDialog (null, "Too High");
                else
                    JOptionPane.showMessageDialog (null, "Too Low");
            } while ( (numberOfAttempts <= MAXNUMOFGUESSES) && (guessNumber != secretNumber) )
    }
 

    public void gameOver() {
        // (v) YOUR CODE
                if (guessNumber == secretNumber)
                    JOptionPane.showMessageDialog
                            (null, "Your answer " + guessNumber + " is correct and it took you "
                            + numberOfAttempts + " attempts. Congratulations.");
                else
                    JOptionPane.showMessageDialog
                            (null, "Your answers were wrong. The correct answer is " + secretNumber);
    }

// GuessingApp.java

class GuessingApp {
        public static void main (String[] args) {
                // (a)
            // (vi) YOUR CODE
                Guessing g = new Guessing();

                // (b)
           // (vi) YOUR CODE
                g.play();

                // (c)
           // (vi) YOUR CODE
                g.gameOver();
        }
}
 

(60 (=9+9+9+9+9+5+5+5)  points)


2.
        Find all syntax errors in the following JAVA method and correct them. Then, explain what it does. Please be brief and up to the point.

public void puzzle ( Guessing gObj1; gObj2
            {
                    if ( gObj1 !== gObj2 ) {
                        gObj3 = gObj1;
                        gObj1 = gObj2;
                        gObj2 = gObj3;
                        return true
 
                    return false
          };
 

public boolean puzzle ( Guessing gObj1, Guessing gObj2 )
            {
                    if ( gObj1 != gObj2 ) {
                        Guessing gObj3 = gObj1;
                        gObj1 = gObj2;
                        gObj2 = gObj3;
                        return true;
                    }
                    return false;
          }

This method swaps values of reference variables gObj1 and gObj2 if they don't refer to the same object and returns true to indicate that the swap took place. After the swap, gObj1 points to the object that gObj2 originally pointed to, and gObj2 points to the object that gObj1 originally pointed to. If the gObj1 and gObj2 refer to the same object, no swap is performed and the value returned is false.

(15 points)


3.
   (a)    List all Java basic data types,

char, byte, short, int, long, float, double, boolean

    (b)    List all Java wrapper classes and how they relate to Java basic data types,

Character, Integer, Float, Double, Boolean

    (c)    Briefly explain the rationale behind the idea of having wrapper classes in addition to the basic data types.

Basic data types are equiped only with a basic set of arithmetic operations and relations (for numeric types), and basic logical operators (for boolean type). Wrapper classes provide for additional functionality with their set of member methods. I.e. int Integer,parseInt(String), float Float.parseFloat(String), String Integer.toString(int), etc., etc...
 

(25 (=8+8+9)  points)

 


2.
        Find all syntax errors in the following JAVA method and correct them. Then, explain what it does. Please be brief and up to the point.

public int puzzle ( Guessing gObj1; Guessing gObj2 )

                    if ( gObj1 !== gObj2 )
                        gObj3 == gObj1;
                        gObj1 == gObj2;
                        gObj2 == gObj3;
                        return true;
 
                    return false;
          };
 

public boolean puzzle ( Guessing gObj1, Guessing gObj2 )
            {
                    if ( gObj1 != gObj2 ) {
                        Guessing gObj3 = gObj1;
                        gObj1 = gObj2;
                        gObj2 = gObj3;
                        return true;
                    }
                    return false;
          }

This method swaps values of reference variables gObj1 and gObj2 if they don't refer to the same object and returns true to indicate that the swap took place. After the swap, gObj1 points to the object that gObj2 originally pointed to, and gObj2 points to the object that gObj1 originally pointed to. If the gObj1 and gObj2 refer to the same object, no swap is performed and the value returned is false.

(15 points)


3.
        Explain briefly :
    (a)    Difference between void methods and methods returning values,

Void methods return no value and are invoked as stand alone statements. Methods returning values are invoked as parts of expressions of the same type as the type of their returned value.

    (b)    List all possible sources from where methods can receive their data for processing (input), as well all possible destinations for output values produced by methods.

Inputs to a method can be coming from :
    keyboard,
    input parameters
    input files
    method's accessing object.
Outputs from a method can be going to :
    screen,
    output parameters
    output files
    method's accessing object.
 

(25 (=12+13)  points)