In: Computer Science
Write a java program with the following classes:
Class Player
Method Explanation:
play : will use a loop to generate a
series of random numbers and add them to a total, which will be
assigned to the variable score.
decideRank: will set the instance
variable rank to “Level 1”, “Level 2”, “Level 3”, “Level
4” based on the value of score, and return that
string.
getScore : will return score.
toString: will return a string of
name, score and rank.
Must have all the accessors and mutators.
Class: Game:
The instance variable is an array of Player.
Method Explanation:
constructor Game (Player player[]): it
will instantiate the instance variable with the passed
Player array.
start: will use a loop to go through each
Player of the instance variable player (the array
player[]) and have each element (which is a Player object)
call the play method.
end: print out “Game Ended…”
decideWinner: use the
score method of Player to get the score
of each Player object in the instance variable player. Compare and
decide the winner. Return the Player
object.
Must have all the accessors and mutators.
GameDriver: main method will declare and instantiate an array of Player and create a Game object with it. Then it will start the game (call the start method), end the game (call the end method). It will show the rank of each Player object and show the winner (you can call decideWinner in the print statement since this method returns the winning Player object). Here is a summary: in this main method, you are to call
( start(), end(), and decideWinner() through the Game object you declared at the beginning. All three methods are from the Game class.)
Thanks for the question, here are the 3 classes - Player, Game and GameDriver.
Let me know for any question or for any further help!
thank you & please do upvote : )
===============================================================================================
import java.util.Random;
public class Player {
private static int
PLAY_COUNT = 10; // generate 10 times the random
number
private int
score;
private String
playerName;
public Player(String
playerName) {
this.playerName =
playerName;
}
public void play() {
int
total = 0;
Random random =
new Random();
for
(int i = 1; i <= PLAY_COUNT; i++)
{
total += random.nextInt(100) + 1;
}
score =
total;
}
public String decideRank()
{
if
(score <= 100) return
"Level 1";
else if
(score <= 200) return
"Level 2";
else if
(score <= 300) return
"Level 3";
else
return "Level 4";
}
public int getScore() {
return
score;
}
public String getPlayerName()
{
return
playerName;
}
@Override
public String toString()
{
return
"Name: " + getPlayerName() + ", Score:
" + getScore() + ", Rank: " +
decideRank();
}
}
public class Game { private Player[] players; public Game(Player[] players) { this.players = players; } public void start() { System.out.println("Game Started..."); for (int id = 0; id < players.length; id++) { players[id].play(); } } public void end() { System.out.println("Game Ended..."); } public Player decideWinner() { Player winner = players[0]; for (Player player : players) { if (player.getScore() > winner.getScore()) winner = player; } return winner; } public Player[] getPlayers() { return players; } }
public class GameDriver { public static void main(String[] args) { Player[] players = new Player[5]; players[0] = new Player("Kim Jong-Un"); players[1] = new Player("Donald Trump"); players[2] = new Player("Xi Jinping"); players[3] = new Player("Queen Elizabeth"); players[4] = new Player("King Salman"); Game game = new Game(players); game.start(); game.end(); Player winner = game.decideWinner(); System.out.println("Winner of the game is : "+winner); } }