import java.util.Scanner; public class ChoHan { /** * Main(0 tests classes devel
ID: 3680001 • Letter: I
Question
import java.util.Scanner; public class ChoHan { /** * Main(0 tests classes developed in ChoHan game application, and * plays the game with two players for five rounds determining winner(s). * @param args the command line arguments */ public static void main(String[] args) { final int MAX_ROUNDS = 5; // Number of rounds String player1Name; // First player's name String player2Name; // Second player's name // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get the player's names. System.out.print("Enter the first player's name: "); player1Name = keyboard.nextLine(); System.out.print("Enter the second player's name: "); player2Name = keyboard.nextLine(); // Create the dealer. Dealer dealer = new Dealer(); // Create the two players. Player player1 = new Player(player1Name); Player player2 = new Player(player2Name); // Play the rounds. for (int round = 0; round < MAX_ROUNDS; round++) { System.out.println("----------------------------"); System.out.printf("Now playing round %d. ", round + 1); // Roll the dice. dealer.rollDice(); // The players make their guesses. player1.makeGuess(); player2.makeGuess(); // Determine the winner of this round. roundResults(dealer, player1, player2); } // Display the grand winner. displayGrandWinner(player1, player2); } /** The roundResults method determines the results of the current round @param dealer The Dealer object @param player1 Player #1 object @param player2 Player #2 object */ public static void roundResults(Dealer dealer, Player player1, Player player2) { // Show the dice values. System.out.printf("The dealer rolled %d and %d. ", dealer.getDie1Value(), dealer.getDie2Value()); System.out.printf("Result: %s ", dealer.getChoOrHan()); // Check each player's guess and award points. checkGuess(player1, dealer); checkGuess(player2, dealer); } /** The checkGuess method checks a player's guess against the dealer's result. @param player The Player object to check. @param dealer The Dealer object. */ public static void checkGuess(Player player, Dealer dealer) { final int POINTS_TO_ADD = 1; // Points to award winner String guess = player.getGuess(); // Player's guess String choHanResult = dealer.getChoOrHan(); // Cho or Han // Display the player's guess. System.out.printf("The player %s guessed %s. ", player.getName(), player.getGuess()); // Award points if the player guessed correctly. if (guess.equalsIgnoreCase(choHanResult)) { player.addPoints(POINTS_TO_ADD); System.out.printf("Awarding %d point(s) to %s. ", POINTS_TO_ADD, player.getName()); } } /** The displayGrandWinner method displays the game's grand winner. @param player1 Player #1 @param player2 Player #2 */ public static void displayGrandWinner(Player player1, Player player2) { System.out.println("----------------------------"); System.out.println("Game over. Here are the results:"); System.out.printf("%s: %d points. ", player1.getName(), player1.getPoints()); System.out.printf("%s: %d points. ", player2.getName(), player2.getPoints()); // fill in the code to determine the winner and print his/her name } }
This has four errors
Explanation / Answer
First you need to make an empty java project then add these 4 java files in it--->
1. Die.java
package chohan;
import java.util.Random;
/**
The Die class simulates a six-sided die.
*/
public class Die
{
private int sides; // Number of sides
private int value; // The die's value
/**
The constructor performs an initial
roll of the die.
@param numSides The number of sides for this die.
*/
public Die(int numSides)
{
sides = numSides;
roll();
}
/**
The roll method simlates the rolling of
the die.
*/
public void roll()
{
// Create a Random object.
Random rand = new Random();
// Get a random value for the die.
value = rand.nextInt(sides) + 1;
}
/**
getSides method
@return The number of sides for this die.
*/
public int getSides()
{
return sides;
}
/**
getValue method
@return The value of the die.
*/
public int getValue()
{
return value;
}
}
2. Dealer.java
package chohan;
/**
Dealer class for the game of Cho-Han
*/
public class Dealer
{
private int die1Value; // The value of die #1
private int die2Value; // The value of die #2
/**
Constructor
*/
public Dealer()
{
die1Value = 0;
die2Value = 0;
}
/**
The rollDice method rolls the dice and saves
their values.
*/
public void rollDice()
{
final int SIDES = 6; // Number of sides for the dice
// Create the two dice. (This also rolls them.)
Die die1 = new Die(SIDES);
Die die2 = new Die(SIDES);
// Record their values.
die1Value = die1.getValue();
die2Value = die2.getValue();
}
/**
The getChoOrHan method returns the result of
the dice roll, Cho or Han.
@return Either "Cho (even)" or "Han (odd)"
*/
public String getChoOrHan()
{
String result; // To hold the result
// Get the sum of the dice.
int sum = die1Value + die2Value;
// Determine even or odd.
if (sum % 2 == 0)
result = "Cho (even)";
else
result = "Han (odd)";
// Return the result.
return result;
}
/**
The getDie1Value method returns the value of
die #1.
@return The die1Value field
*/
public int getDie1Value()
{
return die1Value;
}
/**
The getDie2Value method returns the value of
die #2.
@return The die2Value field
*/
public int getDie2Value()
{
return die2Value;
}
}
3. Player.java
//———Player class———
package chohan;
import java.util.Random;
/**
Player class for the game of Cho-Han
*/
public class Player
{
private String name; // The player's name
private String guess; // The player's guess
private int points; // The player's points
/**
Constructor
@param playerName The player's name.
*/
public Player(String playerName)
{
name = playerName;
guess = "";
points = 0;
}
/**
The makeGuess method causes the player to guess
either "Cho (even)" or "Han (odd)".
*/
public void makeGuess()
{
// Create a Random object.
Random rand = new Random();
// Get a random number, either 0 or 1.
int guessNumber = rand.nextInt(2);
// Convert the random number to a guess of
// either "Cho (even)" or "Han (odd)".
if (guessNumber == 0)
guess = "Cho (even)";
else
guess = "Han (odd)";
}
/**
The addPoints method adds a specified number of
points to the player's current balance.
@newPoints The points to add.
*/
public void addPoints(int newPoints)
{
points += newPoints;
}
/**
The getName method returns the player's name.
@return The value of the name field.
*/
public String getName()
{
return name;
}
/**
The getGuess method returns the player's guess.
@return The value of the guess field.
*/
public String getGuess()
{
return guess;
}
/**
The getPoints method returns the player's points
@return The value of the points field.
*/
public int getPoints()
{
return points;
}
}
4. ChoHan.java
package chohan;
import java.util.Scanner;
/**
*
* @author akg (modified)
*/
public class ChoHan {
/**
* Main(0 tests classes developed in ChoHan game application, and
* plays the game with two players for five rounds determining winner(s).
* @param args the command line arguments
*/
public static void main(String[] args)
{
final int MAX_ROUNDS = 5; // Number of rounds
String player1Name; // First player's name
String player2Name; // Second player's name
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the player's names.
System.out.print("Enter the first player's name: ");
player1Name = keyboard.nextLine();
System.out.print("Enter the second player's name: ");
player2Name = keyboard.nextLine();
// Create the dealer.
Dealer dealer = new Dealer();
// Create the two players.
Player player1 = new Player(player1Name);
Player player2 = new Player(player2Name);
// Play the rounds.
for (int round = 0; round < MAX_ROUNDS; round++)
{
System.out.println("----------------------------");
System.out.printf("Now playing round %d. ", round + 1);
// Roll the dice.
dealer.rollDice();
// The players make their guesses.
player1.makeGuess();
player2.makeGuess();
// Determine the winner of this round.
roundResults(dealer, player1, player2);
}
// Display the grand winner.
displayGrandWinner(player1, player2);
}
/**
The roundResults method determines the results of
the current round
@param dealer The Dealer object
@param player1 Player #1 object
@param player2 Player #2 object
*/
public static void roundResults(Dealer dealer, Player player1,
Player player2)
{
// Show the dice values.
System.out.printf("The dealer rolled %d and %d. ",
dealer.getDie1Value(), dealer.getDie2Value());
System.out.printf("Result: %s ", dealer.getChoOrHan());
// Check each player's guess and award points.
checkGuess(player1, dealer);
checkGuess(player2, dealer);
}
/**
The checkGuess method checks a player's guess against
the dealer's result.
@param player The Player object to check.
@param dealer The Dealer object.
*/
public static void checkGuess(Player player, Dealer dealer)
{
final int POINTS_TO_ADD = 1; // Points to award winner
String guess = player.getGuess(); // Player's guess
String choHanResult = dealer.getChoOrHan(); // Cho or Han
// Display the player's guess.
System.out.printf("The player %s guessed %s. ",
player.getName(), player.getGuess());
// Award points if the player guessed correctly.
if (guess.equalsIgnoreCase(choHanResult))
{
player.addPoints(POINTS_TO_ADD);
System.out.printf("Awarding %d point(s) to %s. ",
POINTS_TO_ADD, player.getName());
}
}
/**
The displayGrandWinner method displays the game's grand winner.
@param player1 Player #1
@param player2 Player #2
*/
public static void displayGrandWinner(Player player1, Player player2)
{
System.out.println("----------------------------");
System.out.println("Game over. Here are the results:");
System.out.printf("%s: %d points. ", player1.getName(),
player1.getPoints());
System.out.printf("%s: %d points. ", player2.getName(),
player2.getPoints());
// fill in the code to determine the winner and print his/her name
}
}
Output :
Enter the first player's name: Sania
Enter the second player's name: shasha
----------------------------
Now playing round 1.
The dealer rolled 2 and 5.
Result: Han (odd)
The player Sania guessed Cho (even).
The player shasha guessed Cho (even).
----------------------------
Now playing round 2.
The dealer rolled 1 and 2.
Result: Han (odd)
The player Sania guessed Han (odd).
Awarding 1 point(s) to Sania.
The player shasha guessed Han (odd).
Awarding 1 point(s) to shasha.
----------------------------
Now playing round 3.
The dealer rolled 3 and 4.
Result: Han (odd)
The player Sania guessed Cho (even).
The player shasha guessed Han (odd).
Awarding 1 point(s) to shasha.
----------------------------
Now playing round 4.
The dealer rolled 1 and 3.
Result: Cho (even)
The player Sania guessed Han (odd).
The player shasha guessed Cho (even).
Awarding 1 point(s) to shasha.
----------------------------
Now playing round 5.
The dealer rolled 1 and 2.
Result: Han (odd)
The player Sania guessed Han (odd).
Awarding 1 point(s) to Sania.
The player shasha guessed Han (odd).
Awarding 1 point(s) to shasha.
----------------------------
Game over. Here are the results:
Sania: 2 points.
shasha: 4 points.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.