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....
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...
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...
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. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use...
This is in JAVA Bank Accounts 01: Child Classes Copy the following SimpleBankAccount class and use it as a base class: /** * Simple representation of a bank account * * @author Jo Belle * @version 0.5 (10/12/2020) */ import java.text.NumberFormat; public class SimpleBankAccount{ // fields (instance variables) private double balance; private String accountId; /** * Constructor for objects of class SimpleBankAccount */ public SimpleBankAccount(){ balance = 0.0; accountId = ""; } /** * Constructor for objects of class SimpleBankAccount...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT