THIS IS A JAVA PROGRAM that needs writing Number guesser You

THIS IS A JAVA PROGRAM that needs writing

Number guesser

Your task is to implement a number guesser that works on the principle of a binary search. In each step, the computer cuts the query interval in half. When the interval contains a single number, it proclaims the answer. The user of the program selects a number between 1 and 100. The computer is then asked to guess the number. A sample run is below:

Be sure to include two classes Numbers and NumbersGuesser. Let NumbersGuesser have the main method.

Solution

import java.util.Scanner;

/* NumberGuesser Class
It contains the main method which contains an object of the number class
and calls it guessNumber method.
*/
public class NumberGuesser{

public static void main(String []args){
Number num = new Number();
num.guessNumber();
}
}

##############################################################################################

import java.util.Scanner;

/* Number Class
It contains the guessNumber method which guesses the number between 1 to 100
based on the response entered by the user. It uses the concept of binary
serach for guessing the number.
*/
public class Number {
  
public void guessNumber() {
Scanner scanner = new Scanner(System.in);
int min = 1;
int max = 100;
String answer, message;
  
do{
int value = min + (max-min)/2;
// User will be asked again and again untill the machine can identify the number
System.out.print(\"Is your number greater than \" + value + \": \");
answer = scanner.nextLine();
switch(answer.toLowerCase()) {
case \"yes\":
min = value+1;
break;
case \"no\":
max = value;
break;
}
}while(min != max);
  
System.out.print(\"Is your number \"+ min +\": \");
answer = scanner.nextLine();
message = min + \" is the answer. \ Thank you for playing the guessing game.\";
  
System.out.println(message);
}
}

THIS IS A JAVA PROGRAM that needs writing Number guesser Your task is to implement a number guesser that works on the principle of a binary search. In each step
THIS IS A JAVA PROGRAM that needs writing Number guesser Your task is to implement a number guesser that works on the principle of a binary search. In each step

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site