Fall 2003
Problems :
private String secretWord;
secret four
letter word.
private String guessWord = "" ;
// word that
the user guessed (intialized to an empty string "").
private int numberOfAttempts = 0;
number of
attempts the ser has made so far in guessing the secret number (initially 0).
private final static int MAXNUMOFGUESSES = 12;
maximal
number of guesses that the user is allowed to have (12).
Its member methods have the following meaning :
public Guessing()
constructor
method producing a random secret four letter word consisting only of lower case
letters and storing it into the data field
secretWord.
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 secretWord
component as
the gO1
object.
private String produceSecretWord()
produces and
returns a random, secret four letter word consisting only of lower case
letters.
private String 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 guessWord, the value in numberOfAttempts
is updated. After that, the user is
informed if
the guess was correct for any of the four letters in the secret word. A
corresponding message is displayed in a message
dialog box (e.g "You
guesed correctly: 1st and 3d letter ", or "You
guessed correctly: No letter."). The game ends when the
user provides a
correct guess or the number of attempts exceeded MAXNUMOFGUESSES (12).
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 String
produceSecretWord() method so that it produces and returns a
random secret four letter word consisting only of lower case letters,
(ii) Write the code for the method public
Guessing() so that this code will be getting the
random secret four letter word (consisting only of lower case
letters)
into the data field secretWord using the services of the method
from (i).
(iii) Write the code for the the method private
String 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 'blue' 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 'grey'.").
(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 String secretWord;
// secret
random four letter word consisting only of lower case letters.
private int numberOfAttempts = 0;
// number of
attempts the user has made so far in guessing the secret number (initially 0).
private String guessWord = "" ;
// word that
the user guessed (intialized to empty string "").
private final static int MAXNUMOFGUESSES = 12;
// maximal
number of guesses that the user is allowed to have (12).
public Guessing() {
// (ii)
YOUR CODE
secretWord =
produceSecretWord();
}
public Guessing copy() {
Guessing copyObj = new Guessing();
copyObj.secretWord = this.secretWord;
return copyObj;
}
private String produceSecretWord() {
String lowerCaseLet =
"abcdefghijklmnopqrstuvwxyz";
// (i)
YOUR CODE
int l0 = (int)
(Math.rand() * 26);
int l1 = (int)
(Math.rand() * 26);
int l2 = (int)
(Math.rand() * 26);
int l3 = (int)
(Math.rand() * 26);
return
lowerCaseLet.charAt(l0) + lowerCaseLet.charAt(l1) +
lowerCaseLet.charAt(l2)
+ lowerCaseLet.charAt(l3);
}
private int askForGuess() {
//
(iii) YOUR CODE
String
guessStr = JOptionPane.showInputDialog ("Please, enter your guess (a four lower
case letters word) :");
return
guessStr;
}
public void play() {
do {
// (iv) (1) YOUR CODE
} while
// (iv) (2) YOUR CODE
}
public void play() {
do {
boolean let1Match = false;
boolean let2Match = false;
boolean let3Match = false;
boolean let4Match = false;
guessWord = askForGuess();
numberOfAttempts++;
String message = "You guesed correctly: ";
if (guessWord.charAt(0) == secretWord.charAt(0)) {
let1Match = true; message += "1st letter
";
}
if (guessWord.charAt(1) == secretWord.charAt(1)) {
let2Match = true; message +=
" 2nd letter";
}
if (guessWord.charAt(2) == secretWord.charAt(2)) {
let3Match = true; message +=
" 3d letter";
}
if (guessWord.charAt(3) == secretWord.charAt(3)) {
let4Match = true; message += " 4th
letter";
}
if !
(let1Match || let2Match || let3Match || let4Match)
message += "No letter";
message += "."
JOptionPane.showMessageDialog (null, message);
} while ( (numberOfAttempts <= MAXNUMOFGUESSES) && !(let1Match && let2Match && let3Match && let4Match) )
// } while (
(numberOfAttempts <= MAXNUMOFGUESSES) &&
!(guessWord.equal(secretWord)) )
}
public void gameOver() {
// (v) YOUR CODE
if (guessWord.charAt(0) == secretWord.charAt(0)) {
let1Match =
true;
}
if (guessWord.charAt(1) == secretWord.charAt(1)) {
let2Match = true;
}
if (guessWord.charAt(2) == secretWord.charAt(2)) {
let3Match = true;
}
if (guessWord.charAt(3) == secretWord.charAt(3)) {
let4Match = true;
}
if
(let1Match && let2Match && let3Match && let4Match )
// if ((guessWord.equal(secretWord))
JOptionPane.showMessageDialog
(null, "Your answer " + guessWord + " is correct and it took you "
+ numberOfAttempts + " attempts. Congratulations.");
else
JOptionPane.showMessageDialog
(null, "Your answers were wrong. The correct answer is " + secretWord);
}
// 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();
}
}
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.
3.
Explain briefly :
(a) Difference between public and private
member methods of a class,
Public could be accessed from within the same class, as well as from outside of it. Private can be referred to only from within the same class.
(b) Difference between a class and an object (instance).
A class is a blueprint description of a new data type with its member methods and data members. An object is an instance of the class with its own data members (the non-static ones) and member methods.
(c) The meaning of one class (subclass)
extending another class (superclass).
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.
3.
Explain briefly how java interpreter
handles :
(a) application programs.
Java interpreter would look for the "main" method in the class and start the execution from the first executable statement in "main".
(b) applets.
Java interpreter would :
(i) create an object of the applet class,
(ii) call "init" method for the object from (i),
(iii) call "start" method for the object from (i),
(iv) call "paint" method for the object from (i).
(v) if and when the ".html" document with the applet is left for another ".html" document, it would call "stop" method for the object from (i).
(vi) if and when the browser viewing the ".html" document with the applet is terminated, it would call "stop" and "destroy" methods for the object from (i).
(25 (=12+13) points)