Page 1 of 1

I need this Java code "translated" into JOptionPane I have a few sepearate text documents that make up a program that I

Posted: Fri Jul 01, 2022 5:34 am
by answerhappygod
I need this Java code "translated" into JOptionPane
I have a few sepearate text documents that make up a programthat I need to translate into JOptionPane
1. FirstToOneGame.java
import java.util.Scanner;
/** This program demonstrates a solution to the First to One Game programming challenge. */
public class FirstToOneGame{ public static void main(String[] args) { final int NUM_SIDES = 6; // Number of sides on the die final int STARTING_POINTS = 50; //Number of starting points String player1Name; // First player's name String player2Name; // Second player's name
// Create a Scanner object for keyboardinput. Scanner keyboard = new Scanner(System.in);
// Create the die. Die die = new Die(NUM_SIDES);
// Get the player's names. System.out.print("Enter the first player'sname: "); player1Name = keyboard.nextLine(); System.out.print("Enter the second player'sname: "); player2Name = keyboard.nextLine();
// Create the two players. Player player1 = new Player(player1Name,STARTING_POINTS); Player player2 = new Player(player2Name,STARTING_POINTS);
// Play the game until one of the player'swins. while (gameInPlay(player1, player2)) { // Player 1 rolls the die andtakes the first turn. takeTurn(player1, die);
// If the game is still beingplayed, // Player 2 rolls the die andtakes a turn. if (gameInPlay(player1,player2)) { takeTurn(player2,die); } } // Display the name of the winningplayer. determineWinner(player1, player2); }
/** The gameInPlay method determines if thegame is still in play. The method returnstrue if the game is still being played, or falseif one of the players has won the game. @param p1 A reference to the Playerobject for player 1. @param p2 A reference to the Playerobject for player 2. @return True if the game is still beingplayed. */
public static boolean gameInPlay(Player p1, Playerp2) { boolean status = false; // Set the flag tofalse.
// Determine if the game is still beingplayed. if (p1.getPoints() != 1 &&p2.getPoints() != 1) { status = true; // Set the flag totrue. }
// Return the status. return status; }
/** The takeTurn method simulates a player'sturn. @param p A reference to a Player object. @param d A reference to a Die object. */
public static void takeTurn(Player p, Die d) { // Display information about the player'sturn. System.out.println("----------------------------"); System.out.println(p.getName() + "'sturn.");
d.roll(); // Roll the die.
// Display the value of the die. System.out.println(p.getName() + " rolled a "+ d.getValue() + ".");
// Determine if the player's points areless than the // number of points needed to win. if (p.getPoints() - d.getValue() < 1) { // If so, add the value to theplayer's points. p.setPoints(p.getPoints() +d.getValue()); } else { // Subtract the value from theplayer's points. p.setPoints(p.getPoints() -d.getValue()); }
// Display the player's points after theroll. System.out.println(p.getName() + " has "+ p.getPoints() + " point(s)."); }
/** The determineWinner method displays thewinner of the game. @param p1 A reference to the Playerobject for player 1. @param p2 A reference to the Playerobject for player 2. */
public static void determineWinner(Player p1,Player p2) { System.out.println("----------------------------");
// Determine if player 1 has won thegame. if (p1.getPoints() == 1) { // If so, display a messagedeclaring player 1 // as the winner. System.out.println(p1.getName() +" is the winner!"); } else { // Display a message declaringplayer 2 as the winner. System.out.println(p2.getName() +" is the winner!"); } System.out.println("----------------------------"); }}
2. Die.Java
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 aninitial roll of the die. The number ofsides for the die is passed as anargument. @param numSides The number of sides forthe die. */
public Die(int numSides) { sides = numSides; roll(); }
/** The roll method simlates the rollingof 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; }
/** The getSides method returns the number of sides for the die. @return The number of sides for thedie. */
public int getSides() { return sides; }
/** The getValue method returns the value of the die. @return The value of the die. */
public int getValue() { return value; }}
3.Player.Java
/** The Player class simulates a player in the First to One Game. */public class Player{ // Fields private String name; // The player's name private int points; // The player's points /** The constructor accepts two argumentsfor the player's name and points. @param n The player's name. @param p The player's points. */ Player (String n, int p) { name = n; points = p; } /** The getName method returns the name of the player. @return The name of the player. */ public String getName() { return name; } /** The getPoints method returns the number of points. @return The player's points. */ public int getPoints() { return points; } /** The setName method accepts an argument for the name of the player. @param n The player's name. */ public void setName(String n) { name = n; } /** The setPoints method accepts an argument for the player's points. @param p The player's points. */ public void setPoints(int p) { points = p; }}
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 1
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 1 (46.69 KiB) Viewed 34 times
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 2
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 2 (35.82 KiB) Viewed 34 times
This is an example of how the program should operate oncetranslated
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 3
I Need This Java Code Translated Into Joptionpane I Have A Few Sepearate Text Documents That Make Up A Program That I 3 (21.91 KiB) Viewed 34 times
- name: - points int String Player + Player(n : String, p : int) + getName(): String int + getPoints(): + setName(n: + setPoints(p: String): void int) : void
- sides : int - value : int Die + Die(numSides: int) + roll(): void + getSides(): int + getValue(): int
Write a program that simulates the game being played by two players. Use the Die class that was presented in Chapter 6 to simulate the dice. Write a Player class to simulate the player. Enter the player's names and display the die rolls and the totals after each round. I will attach the books code that must be converted to import javax.swing.JOptionPane;. Example: Message i Round 1: James rolled a 4 Sally rolled a 2 James: 46 Sally: 48 OK X