Question

In: Computer Science

Write a java program with the following classes: Class Player Method Explanation: play : will use...

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.)

Solutions

Expert Solution

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);

    }

}

Related Solutions

Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments)...
write the program in java. Develop a class RentCabin that does the following: (use JavaDoc comments) // declare the constructor that sets the type and rate based in the sqft parm        // set values based on sqft <1000 is small with $100 per night, // sqft between 1000 and 2000, mid-sized $200 per night, and // over 2000 as a large cabin with $300 per night        //declare getRate        //declare getType        //declare setRate with int rate parm...
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class...
This is in JAVA Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is...
Write a java program StudentDriver class to test the methods of the Student class. Use data:...
Write a java program StudentDriver class to test the methods of the Student class. Use data: Mary Brown 1234 John Jones 5678 Maty Jones 1234 Note: we are including Mary Jones twice so we can test the "equals" method. We can test "compareTo", by test student1 and student2, then student2 and student3. Just submit your driver class as text file. Make sure you also test addQuiz, getTotalScore, getAverage, and toString. Don't worry about testing all of the sets and gets......
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have...
JAVA: USE SWITCH METHOD Write a Temperature class using the Demo below. The class will have three conversion methods: toCelcius(), toKelvin and toFahrenheit(). These methods will return a Temperature in those three scales equal to this temperature. Note that the value of this is not changed int these coversions. In addition to these conversion methods the class will have add(Temperature), subtract(Temperature), multiply(Temperature) and divide(Temperature). These four methods all return a temperature equalled to the respective operation. Note that the this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT