4
\$\begingroup\$

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 high");
    } else 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;
}

}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ As already mentioned on the original question: Please include the full problem statement of the problem you are solving. Thanks"! \$\endgroup\$
    – Vogel612
    Commented Oct 7, 2015 at 16:07

1 Answer 1

4
\$\begingroup\$
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. :)

\$\endgroup\$
1
  • \$\begingroup\$ Beatiful avoiding of poverenvgeneering \$\endgroup\$
    – Caridorc
    Commented Oct 8, 2015 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.