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
write a function that returns a list, where each member of list contains previous day’s value multiplied by 2.​
sergeinik [125]

Answer:

Explanation:

The following code is written in Java, the function takes in a list with the previous day's values. The function then uses that list, loops through it and multiplies each individual value by 2 and returns the modified list. The first red  square represents the test case for the function, while the second red square in the image represents the output.

 public static ArrayList<Integer> doubleIt(ArrayList<Integer> mylist) {

       for (int x = 0; x<mylist.size(); x++) {

           mylist.set(x, mylist.get(x)*2);

       }

       return mylist;

   }

5 0
3 years ago
Which of the following is time-dependant? Group of answer choices
Nookie1986 [14]

Answer:

A dynamic model accounts for time-dependent changes in the state of the system,

while a static model calculates the system in equilibrium, and thus is time-invariant.

Dynamic models typically are represented by differential equations or difference equations

7 0
3 years ago
The ____ command can be used to view the contents of a record when it is anticipated that the record will need to be modified. I
GREYUIT [131]

Answer:

The correct answer is B)

Explanation:

The commit command allows us to:

• Consolidate, confirm

• committing refers to the idea of confirming a set of provisional changes permanently.

• A popular use is at the end of a database transaction.

•

The LOCK TABLE command should be used:

• when a lock of the mode specified in the table can be acquired.

• The command will wait for everything you need to get the lock,

• In case you cannot acquire the lock immediately, the NOWAIT option is specified so that the command cancels the transaction.

6 0
2 years ago
What is the very first tag you must enter into a new HTML Document?
podryga [215]

Answer:

<HTML> tag

Explanation:

The first tag in any HTML file is the <HTML> tag. This tells web browsers that the document is an HTML file

4 0
3 years ago
Read 2 more answers
After a webcast is completed there are no files left on your computer.
astra-53 [7]
What are you askinggggggggggg

7 0
3 years ago
Other questions:
  • What type of organizational structure would you want to use for this company (by function, by process, by product, and so on)? E
    11·1 answer
  • How is the execution speed of machine language compared to that of high-level languages? The execution speed of machine language
    10·2 answers
  • Markaplier nand jackceptieye are the best YT ever who agrees
    5·2 answers
  • Abusive behavior, which involves the use of an electronic communications device, that is degrading, humiliating, hurtful, insult
    14·1 answer
  • The 'WIMP' environment is much more user friendly,why?​
    11·2 answers
  • 01110100 01101000 01101001 01110011 00100000 01110011 01110101 01100011 01101011 01110011 00100000 01100010 01100001 01101100 01
    12·2 answers
  • Anyone know how to delete that I need ASAP
    13·1 answer
  • An insurance company utilizes SAP HANA for its day-to-day ERP operations. Since they can’t migrate this database due to customer
    5·1 answer
  • The BCD number for decimal 473 is( ). a) 111011010; b) 010011110011; c) 010001110011; d)0010110110​
    15·1 answer
  • Chris is working with other employees on a worksheet. the other employees have made comments, but they are not visible.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!