patternjavaMinor
Deitel Java exercise 6.30: Number-guessing game
Viewed 0 times
numberexercisejavagameguessingdeitel
Problem
I missed points for putting implementation in the driver class, rather than having the driver class only make call methods in other classes. I'm not sure how to move the implementation outside of the main class. Suggestions are welcome!
/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Scanner;
/**
*Driver class for number guessing game
*/
public class GuessDriver {
/**
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
*instantiate guess class
*/
Guess Guess = new Guess();
/*
*loops program until user guesses correct number
*/
while (Guess.getGuess() != Guess.secretNumber){
/*
*takes input from user
n*/
System.out.println("Enter a number from 1 to 1000: ");
int setGuess = input.nextInt();
Guess.setGuess(setGuess);
/*
*nested if-else statements compares guess against secret number
*/
if(Guess.secretNumber Guess.guess){
System.out.println("too low");
} else {
System.out.println("Congratulations, you win!");
}
}
}
}/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Random;
/**
* Takes in and stores an integer, sets value for random number
*/
public class Guess {
/**
*instance variable
*/
public int guess;
/**
*instance variable
*/
public int secretNumber = (int) (Math.random() * 999 + 1);
/**
*return secret number
* @return
*/
public int getSecretNumber() {
return secretNumber;
}
/**
*return guess
* @return
*/
public int getGuess() {
return guess;
}
/**
*set guess
* @param guess
*/
public void setGuess(int guess) {
this.guess = guess;
}
}Solution
Guess Guess = new Guess();Java's naming convention for variables are in
camelCase, so I will suggest writing that as Guess guess = new Guess();.With that said, the fact that Java being an OOP language doesn't mean that everything must be done object-style. For this relatively trivial exercise, all you need is just a random number:
int randomNumber = getRandomNumber();And everything can be done within the
main() method itself:public static void main(String[] args) {
int randomNumber = getRandomNumber();
try (Scanner scanner = new Scanner(System.in)) {
for (int guess = prompt(scanner); guess != randomNumber; guess = prompt(scanner)) {
System.out.println("too " + (guess < randomNumber ? "low" : "high"));
}
System.out.println("Congratulations, you win!");
}
}I will suggest using
try-with-resources on the Scanner instance to handle its underlying I/O resources efficiently. Instead of a 'plain' while loop, I opted to showcase how a similar loop can be done using the for-loop construct: it is somewhat more expressive, and the slight benefit is that it nicely scopes the guess variable within itself.The implementation for
getRandomNumber() and prompt(Scanner) shall be left for the reader... What I can further advise for the latter is that it should safely handle invalid inputs internally and only returns an int value for comparison.Oh, and one more thing... you current implementation for the random number does not generate it in the range \$[1 ... 1000]\$, assuming that you really intend the range to be bound-inclusive:
(int) (Math.random() * 999 + 1)Tip:
Math.random() will not give you exactly 1.0. In addition the cast to int truncates the precision, so a value like 999.999 is rounded down to 999. You'll need an alternative way of getting 1000. :)Code Snippets
Guess Guess = new Guess();int randomNumber = getRandomNumber();public static void main(String[] args) {
int randomNumber = getRandomNumber();
try (Scanner scanner = new Scanner(System.in)) {
for (int guess = prompt(scanner); guess != randomNumber; guess = prompt(scanner)) {
System.out.println("too " + (guess < randomNumber ? "low" : "high"));
}
System.out.println("Congratulations, you win!");
}
}(int) (Math.random() * 999 + 1)Context
StackExchange Code Review Q#106851, answer score: 4
Revisions (0)
No revisions yet.