S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
It is currently Thu, 23 May 2013 22:38:40 UTC

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Java Programming - Please help
PostPosted: Wed, 8 Jun 2005 18:04:11 UTC 
Offline
Member

Joined: Wed, 8 Jun 2005 02:50:42 UTC
Posts: 49
This program is to write a Java main class to play a number guessing game. The user thinks of a number between 1 and 128 and the computer guesses the number by asking several yes/no questions.
The program's main class should:

- Use JOptionPane to tell the user to think of a numer between low = 1 and high = 128
- Start a while loop
- Guess at the number by calculating the number half way between low and high, (low+high)/2
- Use JOptionPane to ask the user if your guess is correct; yes or no
- If the answer is yes the break from the loop
- If the answer is no then ask the user if your guess is bigger than the nunber, yes or no
- Depending on the answer adjust low or high
- End the while loop
- Display the correct guess and the number of guesses

--------------------------------------------------------------------------------
import javax.swing.JOptionPane;

public class game {

public game() {
int low = 1, high = 128, guess = 0;
String check = "", check2;
boolean check3 = true;
JOptionPane.showMessageDialog(null,"Think of a number between 1 and 128");

while(true){
guess = (low + high)/2;
check = JOptionPane.showInputDialog("Is your number " + guess +"? (y/n)");
if(check.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "The number you guessed is "+ guess);
check3 = false; break;
}
else if (check.equalsIgnoreCase("N")){
check3 = true;
check2 = JOptionPane.showInputDialog("Is the number greater than or less than "+ guess+"? (< >)");
if(check2.equals("<")) //less than
high = guess;
else if(check2.equals(">")) // greater than
low = guess;
}
else
check = JOptionPane.showInputDialog("Please specify (Y or N):");
}
}

public static void main(String[] args) {
new game();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed, 8 Jun 2005 22:06:36 UTC 
Offline
Member of the 'S.O.S. Math' Hall of Fame
User avatar

Joined: Wed, 1 Oct 2003 04:45:43 UTC
Posts: 9633
Hi Sami,

Did you have any questions? For the most part, your code works fine. I have some comments, though:

(1) The program doesn't print out the number of guesses after the game is over. It should do this (according to the specs you provided).

(2) Choosing 'cancel' any time during the game causes a NullPointerException to be thrown. You may or may not want to handle this.

(3) If I guess the number 128, then the game never ends (the highest number the computer ever guesses is 127).

(4) It is the convention that every Java class begins with a capital letter. I would change the name of the class (and consequently that of the constructor) to Game instead of game.

Otherwise, everything looks good to me. Nice work so far.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2011 phpBB Group.
Copyright © 1999-2013 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA