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
How computer can affect the life of people?
SCORPION-xisa [38]
<h2>Answer These are the negative effects</h2><h2>Low grades In school: others use the computer not for studying but something else. Which makes them unfocused in school.</h2><h2 /><h2>Waist and wrist pains: Due to long sitting ppl experience pains all over the body. </h2>

<h2>Addiction: People get addicted to the computer and makes them forget they have something important doing.</h2>
4 0
2 years ago
The ______ clause allows us to select only those rows in the result relation of the ____ clause that satisfy a specified predica
baherus [9]

Answer:

1. Where,

2. From

Explanation:

In SQL query language when working on a database, a user can use certain clauses to carry out some functions.

Hence, The WHERE clause allows us to select only those rows in the result relation of the FROM clause that satisfy a specified predicate.

This is because the "Where clause" selects the rows on a particular condition. While the "From clause" gives the relation which involves the operation.

5 0
2 years ago
Once you have saved a block of text for easy insertion in other documents, it will show up where?
kherson [118]
Click file and look up the name
7 0
2 years ago
A user needs to use her laptop to give a presentation using a video projector. she called asking you how to switch the display t
Doss [256]

<span>I would advise her to press the fn keyboard key along with the function key with a monitor connected on it. She should ensure that she has the correct cable to connect the laptop to the projector. Most laptops require a manual switch by pressing these combination keys but for some, they will automatically detect the external display and switch to video output</span>

 

4 0
3 years ago
There are a number of roles held by various users of online digital media, including contributors, conversationalists,
Maru [420]

Answer:

The mass media is a huge phenomenon. Through the various different platforms, print or broadcast, the media is able to reach millions of people like no other force. Without the media, powerful speeches by politicians would affect no one, local events would remain local, and performances by great actors would be seen only by the people in the immediate audience. The media overcomes distances, and builds a direct relationship with the audience. Many sociologist

4 0
3 years ago
Read 2 more answers
Other questions:
  • Complete the recursive function raisetopower(). ex: if userbase is 2 and userexponent is 4, then raisedvalue is assigned with 16
    5·1 answer
  • Universal Containers is setting up an external Business Intelligence (BI) system and wants to extract 1,000,000 Contact records.
    11·1 answer
  • I have been trying to work on this for a while now, and this is on excel
    11·1 answer
  • Synthesizing information can be done by creating
    15·2 answers
  • What symbol following a menu command lets you know that a dialog box will be displayed? an arrow a check mark an ellipse a radio
    5·2 answers
  • What is an activity that can help you enhance the appearance of your computer’s desktop?
    13·1 answer
  • PLEASE HELP ASAP!!!
    8·1 answer
  • A cybersecurity analyst is currently investigating a server outage. The analyst has discovered the following value was entered f
    9·1 answer
  • Can someone write a 5 sentence summary about Virtual Assistants. Please. I'm in a rush. Personal problems.
    5·1 answer
  • These brainly bots need to stop!!
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!