Reputation: 19
I'm trying to finish a project for one of my courses and I'm almost done, but having issues. For this I was able to get something to work but I'm not really sure how I should go about breaking it into the required classes.
The final project is supposed to have a GuessApp class that runs the simple guessing game using the GuessLogic (which I have as correct) which handles the logic of the game. In other words, the GuessApp class is not to keep track of the correct answer, the number of guesses made, the numbers previously guessed, or whether a guess is legal. On the other hand, the GuessApp class is responsible for all of the I/O. In other words, the GuessLogic class is not to print anything (except possibly for the purposes of debugging).
So my question is essentially how do I break up my code into these 2 classes and we were also supposed to implement a toString method in your GuessLogic class that returns the state of the GuessLogic object (that is, all of its member variables) as a single string. How would I do this?
My code thus far:
package guessapp;
import java.util.HashSet;
import java.util.Scanner;
public class GuessApp {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
int guess;
int NumGuess = 1;
do {
System.out.print("Enter a guess: ");
guess = keyboard.nextInt();
if (hs.contains(guess)) {
System.out.println("You have already entered this number");
continue; // this will repeat the loop
}
if (guess < 0 || guess > 10) {
System.out.println("Your guess is out of the specified range. Please try again.");
continue; // this will repeat the loop
}
System.out.println("Your guess is " + guess);
if (guess == GuessLogic) {
System.out.println("You got it right!! Congrats!! Total Number of Guesses: " + NumGuess);
return; // this will stop the loop
}
else if (guess < GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Higher, Guess number: " + NumGuess);
NumGuess++;
}
else if (guess > GuessLogic) {
System.out.println("You are wrong!!! Hint: Guess Lower, Guess number: " + NumGuess);
NumGuess++;
}
hs.add(guess);
} while (true);
}
}
Upvotes: 1
Views: 79
Reputation: 7403
Well, do it by steps.
Create the GuessLogic class, and move to it all the data fields.
public class GuessLogic {
HashSet<Integer> hs = new HashSet<>();
int GuessLogic = (int) (Math.random() * 10 + 1);
int guess;
int NumGuess = 1;
...
Provide a method to add a guess
public void guess(int guess){
this.guess=guess;
this.NumGuess++;
validate();
}
Implement the validate method. Here you have loads of choices. You could keep an enumeration with the current state, something like
enum State {
START,
DUPLICATE,
OUT_OF_RANGE,
LOWER,
HIGHER,
MATCH
}
And validate would set the state.
Then your App queries the state and print the actual message.
Or, which would be simpler, your logic should calculate the message and just maintain a boolean shouldStop, that the app would query to know if it should prompt again or exit
the toString() method, you could just concatenate all the field values (most objects in the Java API have a meaingful toString(). Hint: a good IDE can generate the toString() automatically from the fields.
Hope this helps, and don't be afraid to try things!!
Upvotes: 2