Develop the Guessing game last semester based on Object Oriented principles. Specifically, you should not have static methods or static variables except for the main method
copyable code:
import java.util.Scanner;
class game
{
//constructor
game()
{
System.out.println("Initilaizing constuctor....");
}
//method definition to guess the number
void guess()
{
int secretkey;
//generates the random number
secretkey = (int) (Math.random() * 9 + 1);
//gets input from the user
Scanner keyboard = new Scanner(System.in);
int key;
//gives tips based on the user input in finding the correct guess
do {
System.out.print("Enter a guess (1-10): ");
key = keyboard.nextInt();
if (key == secretkey)
System.out.println("Congratulations!,Guessed the correct key");
else if (key < secretkey)
System.out
.println(" Secret key is larger.");
else if (key > secretkey)
System.out
.println(" Secret key is lesser.");
} while (key != secretkey);
}
}
//main method
public class guessgame {
public static void main(String[] args) {
//generating object for the class
game g=new game();
System.out.println("\nStart guessing");
//calls the method of the class using objects
g.guess();
}
}
Get Answers For Free
Most questions answered within 1 hours.