Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Create a child class that extends Person called Players.java. Create private instance variables within the Players class for the player's current amount of money. Create

Create a child class that extends Person called Players.java.

Create private instance variables within the Players class for the player's current amount of money.

Create public getters and setters for amount of money.

The constructor should initialize the money field to 1000.

Override the toString method of Players so that it will display the player's name and their current amount of money.

In the Numbers.java class, change the randomNum field to static so that it will be the same when instantiated from different classes.

Next, create a child class that extends Person called Hosts.java.

Create a public method called randomizeNum() that instantiates the Numbers.java class and generates a random number.

Create a class, called Turn.java. This class will handle the activities for each player's turn. Create a public method in Turn.java called takeTurn. Move the prompt for the user guess from GamePlay.java into this method.

ACCEPTS PARAMETERS: Players object, Hosts object

RETURNS: true if guess is correct, false if not

Have this method simulate the host(by name) prompting the player(by name) to enter a guess for the random number between 0 and 100, then use the Numbers.java class to check the guess.

If the player correctly guesses the numToGuess, the method should increase the players amount of money by your chosen amount for winning, print a congratulatory message declaring this player the winner, use the overridden toString to display the player's name and money, and then return true.

If the player guess incorrectly, the method should decrease the players amount of money by your chosen amount for incorrect guesses and use the overridden toString to display the player's name and their money, then also display whether their guess was too high or too low and then return false.

Edit GamePlay.java.

In the main method, before prompting the player user, create an instance of the Host class, using any name you like for the Host. Then, generate a new random number for this host.

Instead of creating a Person in your GamePlay.java, you should now create an instance of the Players.java class.

Remove the instantiation of Numbers.java and the generateNumber() method call from your GamePlay.java class. You are now taking care of this in the Hosts.class.

Create an instance of Turn.java

Move the prompt and compare for the guess from your loop in this class and instead put it inside the takeTurn method in Turn.java

Create a loop that calls to takeTurn() until takeTurn() returns true, meaning the player guessed correctly.

Add another loop around the loop in #4 that allows a player to choose whether to stop or keep playing. Remember to change the random number for the next game. There is more than one way that you can do this. Choose the way you think is best.

EXAMPLE OUTPUT:

What is your first name? Jane Would you like to enter a last name? (Y/n) : y

Enter last name: Doe Bob Barker says "Jane Doe, enter your guess for my random number between 0 and 100" 50 I'm sorry. That guess was too high. You lose $200.00 Jane Doe: $800.00 Bob Barker says "Jane Doe, enter your guess for my random number between 0 and 100" 25 I'm sorry. That guess was too high. You lose $200.00 Jane Doe: $600.00 Bob Barker says "Jane Doe, enter your guess for my random number between 0 and 100" 12 I'm sorry. That guess was too low. You lose $200.00 Jane Doe: $400.00 Congratulations, you guessed the number! You win $1,000.00 Jane Doe: $800.00 Play another game? (y or n) n

Process finished with exit code 0

Here is the code I have now but it is not working at all.

//Import Scanner library for input/outputs import java.util.Scanner; //Random Library import java.util.Random //Start of GamePlay public class GamePlay { private Players player; private Hosts host;

public static void main(String[] args) { Scanner sc = new Scanner(System.in); GamePlay game = new GamePlay();

game.host = new Hosts(); game.host.randomizeNum();

System.out.print("What is your first name? "); String firstName = sc.nextLine(); // Prompt for the last name //if user enters y then get the last name. System.out.print("Would you like to enter a last name? (y/n) "); String lastNameResponse = sc.nextLine(); if (lastNameResponse.equalsIgnoreCase("y")) { System.out.print("What is your last name? "); String lastName = sc.nextLine(); game.player = new Players(firstName, lastName, 0.0); } else { game.player = new Players(firstName, 0.0); } Turn turn = new Turn();

// loop that allows the player to keep playing or stop boolean keepPlaying = true; while (keepPlaying) { boolean correctGuess = false; while (!correctGuess) { correctGuess = turn.takeTurn(game.player); } System.out.print("Do you want to keep playing? (y/n) "); String response = sc.nextLine(); if (response.equalsIgnoreCase("n")) { keepPlaying = false; } else { game.host.randomizeNum(); } }//end of game loop }//end of void main

}//GamePlay class ends

//Beg. Turn class public class Turn { public boolean takeTurn(Person player) { Numbers number = new Numbers(); number.generateNumber(); Scanner input = new Scanner(System.in); System.out.println(player.getFirstName() + ", please enter a number to guess my number between 0 - 100: "); int guess = input.nextInt(); boolean correctGuess = number.compareNumber(guess); int winAmount = 500; int loseAmount = 200; // if (correctGuess) { player.increaseMoney(winAmount); System.out.println("Congratulations " + player.getFirstName() + "! You won " + winAmount + " dollars."); } else { player.decreaseMoney(loseAmount); System.out.println("Sorry " + player.getFirstName() + ", your guess was incorrect. You lost " + loseAmount + " dollars."); if (guess > number.getNumToGuess()) { System.out.println("Your guess was too high."); } else { System.out.println("Your guess was too low."); } } System.out.println(player.toString()); return correctGuess; } }//End of Turn Class

//Beg. of Players class public class Players extends Person { private double money; public Players(String firstName, double money) { super(firstName); this.money = money; } public Players(String firstName, String lastName, double money) { super(firstName, lastName); this.money = money; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public void increaseMoney(int amount) { this.money += amount; } public void decreaseMoney(int amount) { this.money -= amount; } @Override public String toString() { return "Bat Man says " + super.getFirstName() + " " + super.getLastName() + ", Money: $" + money; } }//end of Players class //Beg. of Person class

public class Person { // private variables private String firstName; private String lastName; //Create two overloaded constructors for the Person class // First constructor public Person(String firstName) { this.firstName = firstName; this.lastName = ""; }

// Second constructor public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } // Getters and Setters for the names public String getFirstName() { return firstName; }

public void setFirstName(String firstName) { this.firstName = firstName; }

public String getLastName() { return lastName; }

public void setLastName(String lastName) { this.lastName = lastName; } public void increaseMoney(int amount) { } public void decreaseMoney(int amount) { }

}//end of Person class

//Beg. of Numbers class public class Numbers { private static int randomNum;

public void generateNumber() { Random rand = new Random(); randomNum = rand.nextInt(101); }

public boolean compareNumber(int guess) { if (randomNum == guess) { System.out.println("Congratulations, you guessed the number!"); return true; } else if (randomNum < guess) { System.out.println("I'm sorry. That guess was too high."); return false; } else { System.out.println("I'm sorry, That guess was too low."); return false; } }

public static int getRandomNum() { return randomNum; }

public static void setRandomNum(int randomNum) { Numbers.randomNum = randomNum; }

public int getNumToGuess() { return randomNum; }

public int getRandomNumber() { return 0; } }//end of Numbers class

//Beg. of Hosts class public class Hosts { private String name; private Numbers numbers; private int numToGuess; public void randomizeNum() { numToGuess = (int) (Math.random() * 10 + 1); } public Hosts(String name) { this.name = name; this.numbers = new Numbers(); }

public void generateNumber() { this.numbers.generateNumber(); } public int getRandomNumber() { return this.numbers.getRandomNumber(); } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Hosts() { // constructor left empty on purpose to resolve unknown issue.. otherwise it won't run :( } }//end of Hosts class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions