Question

Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....

Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do.

TournamentAdmin class code:

public class RankAdmin

{

   /**

    * Constructor for objects of class RankAdmin

    */

   public RankAdmin()

   {

   }

}

Player class code:

public class Player

{

   private String name;

   private String rank;

   private int won;

   private int drew;

   private int lost;

   /**

    * Constructor for objects of class Player

    */

   public Player(String aName, String aRank)

   {

      name = aName;

      rank = aRank;

      // no need to set won, drew and lost to 0

   }

  

    /**

    * getter for attribute points

    */

   public int getPoints()

   {

      return won + drew;

   }

  

   /**

    * getter for name

    */

   public String getName()

   {

      return name;

   }

  

   /**

    * getter for rank

    */

   public String getRank()

   {

      return rank;

   }

   /**

    * getter for won

    */

   public int getWon()

   {

      return won;

   }  

  

   /**

    * getter for drew

    */

   public int getDrew()

   {

      return drew;

   }

  

   /**

    * getter for lost

    */

   public int getLost()

   {

      return lost;

   }  

  

   /**

    * increments the number of games won

    */  

   public void incWon()

   {

      won = won + 1;

   }

   /**

    * increments the number of games drawn

    */   

   public void incDrew()

   {

      drew = drew + 1;

   }

  

   /**

    * increments the number of games lost

    */   

   public void incLost()

   {

      lost = lost + 1;

   }

   /**

    * setter for rank

    */

   public void setRank(String aRank)

   {

      rank = aRank;

   }

  

   public String toString()

   {

      return ("Player " + name + ", rank: " + rank + " stats: Won: " + won

       + ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());

   }

}

A.

In this part of the question you will amend the TournamentAdmin class.

i.

An instance of TournamentAdmin is used to hold data about a number of players in a number of different ranks. For example:

Key

Value

"Master"

List of Player objects in the Master rank

" Intermediate"

List of Player objects in the Intermediate rank

" Beginner"

List of Player objects in the Beginner rank

...

Declare a private instance variable players in your TournamentAdmin class, capable of referencing a map where the key is a String and the value is a List of Player objects as in the example above.

ii.

Next amend the provided zero-argument constructor in TournamentAdmin so that players is assigned a suitable empty map object when a new instance of TournamentAdmin is created.

iii.

Write a public instance method for the TournamentAdmin class called addPlayer(), which takes two arguments and returns no value as in the following header:

public void addPlayer(String rank, Player player)

The method should check to see if a list for that rank already exists in the Player map.

If the list exists, then the new Player should be added to the existing list of players for that rank.

If the list does not exist, then a new empty list of Player should be created and the new Player should be added to it, then a new key-value pair should be created in players with Tournament as the key and the new list as the value.

B.

Write a public instance method for the TournamentAdmin class called recordResult() which takes five arguments and returns no value as below:

public void recordResult(String rank, String playerA, String playerB, int playerAScore, int playerBScore)

rank indicates which rank the result is for, playerA and playerB indicate which players faced each other. playerAScore and playerBScore are the number of points each player scored in the game.

You should begin by declaring a local variable of a type suitable for referencing a list of Players. Then you should get the list of players for that tournament from the players map, using rank as the map key, and assign it to your local variable.

If playerA scored more points than playerB, then increment the number of wins for playerA , and increment the number of losses for playerB. Similarly, if playerB scored more points than playerA, then increment the number of wins for playerB , and increment the number of losses for playerA. If playerA and playerB both scored the same number of points, then increment the number of draws for both playerA and playerB.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

2 public class TestPlayer i RankAcnin ra-ne Rkdin) Player plsyer2 new PlayerDriteyaster; rs.ad Playeraster playeri)j Disolay-----------------------------------------------

Player.java

public class Player

{

   private String name;

   private String rank;

   private int won;

   private int drew;

   private int lost;

   /**

    * Constructor for objects of class Player

    */

   public Player(String aName, String aRank)

   {

      name = aName;

      rank = aRank;

      // no need to set won, drew and lost to 0

   }

    /**

    * getter for attribute points

    */

   public int getPoints()

   {

      return won + drew;

   }

   /**

    * getter for name

    */

   public String getName()

   {

      return name;

   }

   /**

    * getter for rank

    */

   public String getRank()

   {

      return rank;

   }

   /**

    * getter for won

    */

   public int getWon()

   {

      return won;

   }

   /**

    * getter for drew

    */

   public int getDrew()

   {

      return drew;

   }

   /**

    * getter for lost

    */

   public int getLost()

   {

      return lost;

   }

   /**

    * increments the number of games won

    */

   public void incWon()

   {

      won = won + 1;

   }

   /**

    * increments the number of games drawn

    */

   public void incDrew()

   {

      drew = drew + 1;

   }

   /**

    * increments the number of games lost

    */

   public void incLost()

   {

      lost = lost + 1;

   }

   /**

    * setter for rank

    */

   public void setRank(String aRank)

   {

      rank = aRank;

   }

   public String toString()

   {

      return ("Player " + name + ", rank: " + rank + " stats: Won: " + won

       + ", drew: " + drew + ", lost: " + lost + ", points: " + getPoints());

   }

RankAdmin.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RankAdmin {
   //Instance variable
   ArrayList<Player> players;
   private Map<String,ArrayList<Player>> playersMap;
   /**

        * Constructor for objects of class RankAdmin

        */

       public RankAdmin()

       {
         playersMap=new HashMap<String,ArrayList<Player>>();
       }
       /*
        * check to see if a list for that rank already exists in the Player map
        * If the list exists, then the new Player should be added to the existing list of players for that rank.
        * Else new Player should be added to it,
        * then a new key-value pair should be created in players with Tournament as the key and the new list as the value.
        */
       public void addPlayer(String rank, Player player) {
          if( playersMap.containsKey(rank)) {
           players=playersMap.get(rank);
           players.add(player);
           playersMap.put(rank, players);
       }
          else {
              players=new ArrayList<Player>();
              players.add(player);
              playersMap.put(rank, players);
          }
       }
       /*
        *
        */
       public void recordResult(String rank, String playerA, String playerB, int playerAScore, int playerBScore) {
           players=new ArrayList<Player>();
           players=playersMap.get(rank);
           int index1 = 0,index2 = 0;
           for(int i=0;i<players.size();i++) {
               if(players.get(i).getName().equals(playerA)) {
                   index1=i;
               }
               else if(players.get(i).getName().equals(playerB)) {
                   index2=i;
               }

           }
         
           if(playerAScore>playerBScore) {
               players.get(index1).incWon();
               players.get(index2).incLost();
           }
           else if(playerAScore<playerBScore) {
               players.get(index1).incLost();
               players.get(index2).incWon();
           }
           if(playerAScore==playerBScore) {
               players.get(index1).incDrew();
               players.get(index2).incDrew();
           }
       }
}

TestPlayer.java

public class TestPlayer {

   public static void main(String[] args) {
       //Create object of rankAdmin
       RankAdmin ra=new RankAdmin();
       //Create 2 players
       Player player1=new Player("Michael","Master");
       Player player2=new Player("Britany","Master");
       //Add into map
       ra.addPlayer("Master",player1);
       ra.addPlayer("Master",player2);
       //Display intial status
       System.out.println("Initial status:");
       System.out.println(player1);
       System.out.println(player2);
       System.out.println("\nAfter play status:");
       ra.recordResult("Master", player1.getName(),player2.getName(), 10, 12);
       System.out.println(player1);
       System.out.println(player2);

   }

}

-----------------------------------------------

Output

Initial status:
Player Michael, rank: Master stats: Won: 0, drew: 0, lost: 0, points: 0
Player Britany, rank: Master stats: Won: 0, drew: 0, lost: 0, points: 0

After play status:
Player Michael, rank: Master stats: Won: 0, drew: 0, lost: 1, points: 0
Player Britany, rank: Master stats: Won: 1, drew: 0, lost: 0, points: 1

Add a comment
Know the answer?
Add Answer to:
Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...

    The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java public class FoodItem{ private String name; private int calories;    public FoodItem(String iName, int iCals){ name = iName; calories = iCals; }    public void setName(String newName){ name = newName; }    public void setCalories(int newCals){ calories = newCals; }    public int getCalories(){ return calories;...

  • public class Player { private String name; private int health; public Player(String name) { this.name =...

    public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;...

    Write Junit test for the following class below: public class Player {       public int turnScore;    public int roundScore;    public int lastTurnScore;    public String name;    public int chipPile;       public Player (String name) {        this.name = name;        this.turnScore = 0;        this.chipPile = 50;    }          public int getChip() {        return chipPile;    }       public void addChip(int chips) {        chipPile += chips;    }       public int getRoundScore() {        return roundScore;    }       public void setRoundScore(int points) {        roundScore += points;    }       public int getTurnScore() {        return turnScore;   ...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major...

    Step 1 Develop the following class: Class Name: CollegeDegree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String [ ] Name: courseCreditArray Access modifier: private Data type: int [ ] Static variables Name: maximumNumberOfCourses Access modifier: private Data type: int Initial Value: 40 Constructor Name: CollegeDegree Access modifier: public Parameters: none (default constructor) Task: sets major to the empty string...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT