13.3k views
2 votes
Create a class named Console, and move all the methods that retrieve and validate user input to that class. These methods can remain static. Create a class named Game, and move all the methods that display messages and handle user guesses to that class. Adjust these methods so they aren't static, and use instance variables of the Game class to keep track of numbers, guesses, and so on. Update the application to use these classes and their methods. Make sure the application functions the same as it did before.

User Delon
by
4.7k points

1 Answer

4 votes

Answer:

Here is the Console class:

import java.util.Scanner; // to accept input from user

public class Console { // class name

public Console() {} //class constructor

public int getInt(Scanner sc, String prompt) { //gets user's number

int i = 0; //initialize i to 0

boolean isValid = false; // checks for valid input

while (!isValid) { //loop iterates as long as input remains valid

System.out.print(prompt); //displays prompt

if (sc.hasNextInt()) { // checks for the next token (input number)

i = sc.nextInt(); // scans and reads the next number

isValid = true; // checks if input is valid

} else { //if input is not valid

System.out.println("Error! Invalid integer value. Try again."); } //displays error message

sc.nextLine(); // discard any other data entered on the line }

return i; } //returns number

public int getIntWithinRange(Scanner sc, String prompt, //checks users guess if outside parameters or not

int min, int max) {

int i = 0;

boolean isValid = false; //sets isValid to false

while (!isValid) { //keeps iterating and stops when isValid is false

i = getInt(sc, prompt); //calls getInt method to get users number

if (i <= min) { //if number is less than value of min

System.out.println("Error! Number must be greater than " + min); //displays this message

} else if (i >= max) { //if number is greater than that of max

System.out.println("Error! Number must be less than " + max); //displays this message

} else { //if guess is right

isValid = true; } } //sets isValid to true

return i; } //returns that number

public String getRequiredString(Scanner sc, String prompt) { //Asks for user's string input

String s = "";

boolean isValid = false; //sets isValid to false

while (!isValid) { //keeps iterating and stops when isValid is false

System.out.print(prompt); //displays prompt

s = sc.nextLine(); //reads next line (read next string input value)

if (s.equals("")) { //if user enters nothing

System.out.println("Error! This entry is required. Try again."); //display this message

} else { //if user enters a string input

isValid = true; } } //sets isValid to true

return s; } //returns string

public String getChoiceString(Scanner sc, String prompt, //Checks for users input for Y/y ignores caps

String s1, String s2) {

String s = "";

boolean isValid = false; //sets isValid to false

while (!isValid) { //iterates until isValid is false

s = getRequiredString(sc, prompt); //calls method to get the input string

if (!s.equalsIgnoreCase(s1) && !s.equalsIgnoreCase(s2)) { // checks users input for Y/y ignores caps

System.out.println("Error! Entry must be '" + s1 + "' or '" + s2 + "'. Try again."); // displays error message that entry must be y or Y

} else { //if user enters y or Y

isValid = true; } } //sets isValid to true

return s; } } //returns y or Y

Step-by-step explanation:

Here is the Game class

public class Game { // class name

public Game() {} //class constructor

public void displayWelcomeMessage() { //displays message

System.out.println("Welcome to the Guess the Number Game");

System.out.println("++++++++++++++++++++++++++++++++++++");

System.out.println(); }

public void displayPleaseGuessMessage() { //displays message for user to guess from 1-100

System.out.println("I'm thinking of a number from 1 to 100.");

System.out.println("Try to guess it.");

System.out.println();}

public void displayCorrectGuessMessage(int counter) { //display user's current guess count

System.out.println("You got it in " + counter + " tries.");

if (counter <= 3) { //if user guesses the number in 3 or less tries

System.out.println("Great work! You are a mathematical wizard.\\");

} else if (counter > 3 && counter <= 7) { //if user guesses the number greater than 3 but less than or equals to 7 tries

System.out.println("Not too bad! You've got some potential.\\");

} else { //if user guesses the number after more than 7 tries

System.out.println("What took you so long? Maybe you should take some lessons.\\"); }}

public void displayGuessAgainMessage(int number, int guessNumber) { //Asks user to guess again

int difference = guessNumber - number;

if (guessNumber > number) { //if guessNumber is greater than number

if (difference > 10) { //if difference between guessNumber and number is greater than 10

System.out.println("Way too high! Guess again.\\"); //guessed number is higher than correct one

} else {

System.out.println("Too high! Guess again.\\"); }

} else {

if (difference < -10) { //if difference between guessNumber and number is less than -10

System.out.println("Way to low! Guess again.\\"); //guessed number is lower than correct one

} else {

System.out.println("Too low! Guess again.\\");}}}}

The main program and explanation of these above programs are given in the attached document.

The output of the program is attached.

Create a class named Console, and move all the methods that retrieve and validate-example-1
User Noman Amir
by
4.4k points