1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vladimir2022 [97]
2 years ago
7

Create a class named Console, and move all the methods that retrieve and validate user input to that class. These methods can re

main 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.

Computers and Technology
1 answer:
otez555 [7]2 years ago
3 0

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

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.\n");  

} 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.\n");  

} 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.\n"); }}

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.\n");   //guessed number is higher than correct one

} else {  

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

} else {  

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

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

} else {  

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

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

The output of the program is attached.

You might be interested in
What is a guardian node summary for theta staking tool
Diano4ka-milaya [45]

Answer: Theta Guardian

Explanation:

THETA is used to stake as a Validator or Guardian node, contributing to block production and the protocol governance of the Theta Network. By staking and running a node, users will earn a proportional amount of the new TFUEL generated. Relayers earn TFUEL for every video stream they relay to other users on the network.

3 0
2 years ago
You are a running cable in a new network. You are running a series of cables from the wiring closet to the RJ-45 ports for the c
siniylev [52]

Answer:

IDF cable

Explanation:

An IDF cable or intermediate distribution frame is a cable rack that interconnects and manages a MDF or main distribution frame and a collection of workstations or computer systems. The MDF is a cable rack that interconnects several IDFs to itself.

4 0
2 years ago
____ contain instructions for the OS for hardware devices, such as the keyboard, mouse, and video card, and are stored in the sy
AnnyKZ [126]

The Motherboard contains instructions for the OS for hardware devices, such as the keyboard, mouse, and video card, and are stored in the systemroot\Windows\System32\Drivers folder.

Learned this in middle school, your welcome ;)

3 0
3 years ago
The ______________________ is the part of the ip address that is the same among computers in a network segment.
kozerog [31]
The answer is 
External IP address.
4 0
3 years ago
Online databases allow you access to material you may not be able to find when using popular search engines like Google. Select
Anna007 [38]

Answer:

True

Explanation:

6 0
3 years ago
Other questions:
  • The number of bits used to store color information about each pixel is called ____.
    13·1 answer
  • Knowing the meaning of the acronym WYSIWYG can be most helpful to you when you
    14·1 answer
  • The ________ of the operating system enables users to communicate with the computer system. modem window network adapter card us
    14·1 answer
  • What component of a computer system holds the operating system when the computer is not running
    6·2 answers
  • Which does the histogram resemble the most?
    10·1 answer
  • Discuss advantages and disadvantages of Twisted Pair?
    6·1 answer
  • What are 3 ways to select an entire document?
    15·1 answer
  • Which of the following is an example of a trade journal?
    7·2 answers
  • write a pay-raise program that requests a person's first name, last name, and current annual salary, and then displays the perso
    6·1 answer
  • The direction of a ratchet is reversed by _______________________.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!