Background

Solitaire is a card game that you play against yourself. When you play solitaire you either win or you lose. Solitaire players are very organised. As a result the Australian Solitaire Players Federation is also very organised. In this exam you are going to write code to help keep them organised by answering the three questions below.

Question 1 - a Person class:

Define a Java class to represent a person in a file called Person.java. A Person has the following attributes:

  • a name represented as a single string e.g. "Jessica Long"
  • an age represented as an integer e.g. 35

Your Person class should have the following methods:

  • a constructor with no parameters that sets the name to be "anon" and the age to be 0.
  • a constructor with parameters representing the person's name and age that sets the name and age of the person.
  • an accessor method called getName() that returns the name of the person.
  • an accessor method called getAge() that returns the age of the person.
  • a void method called setName() which takes a String parameter representing this person's name and sets this person's name.
  • a void method called setAge() which takes an integer representing this person's age and sets this person's age.
  • a public toString() method which returns a String with the contents: "Person: " followed by the person's name followed by " is age: " followed by the person's age.

When you compile and run the driver file you should see the output:

Person: anon is age: -1
Person: Jane Austen is age: 23
Person: Spiro Agnew is age: 76
Person: Sally Smith is age: 18
Person: Jane Austen is age: 23
Person: Spiro Agnew is age: 77
18
Jane Austen
77

Note that having your driver file work in the desired way does not guarantee you will pass all the tests - we will run more tests.

Question 2 - a Player class:

The Australian Solitaire Players Federation keeps track of all registered solitaire players in Australia. This is so they can help organise clubs and work out player rankings. Write a Java class to represent a solitaire Player. This class should extend the Person class from question 1 and have at least the following additional attributes:

  • an integer id representing the unique identifier for this player. When a new Player object is created they will get the next available id. To keep track of the next available id you might need a static variable in this class. Note that the very first player id is 1.
  • an integer numWins that represents the number of wins this player has had in their career.
  • an integer numPlayed that represents the number of times a player has played Solitaire in their career.

Note that due to a quirk in the scoring of Solitaire it is possible for players to count more wins than games played! (it's due to a weird historical rule - nobody remembers why).

Of course because Player extends Person, Players also automatically have the name and age attributes of Person. Your Player class should have the following methods:

  • a constructor that takes parameters representing the player's name, the players age, the number of wins the player has had and the number of times the player has played. This constructor should then initialise the name and age (in the super class) and also initialise numWins and numPlayed and the player id. Note: you might also need to update the static variable.
  • a void method called win() that takes no parameters and increments the number of wins and the number of games played by this player.
  • a void method called lose() that takes no parameters and just increments the number of games played.
  • a method called getRanking() that takes no parameters and returns an integer value representing this player's ranking score. This score is calculated as: numPlayed times ( numWins divided-by numPlayed)
  • Note that all the calculations above are done with integers. This means that (due to rounding) the ranking is not always the same as numWins. Note that this scoring calculation is unconventional (its related to the quirk described above) but the calculation above faithfully implements the conventions of the Solitaire Federation. Note also that if a player has played zero times getRanking should just return zero.
  • a method called getId() that just returns the integer id of this player.
  • a public toString() method which returns the String representing the person's attributes concatenated with "Id: " followed by the player's id and then " Ranking: " followed by the player's ranking.

When you compile and run the driver file you should see the output:

Person: Sally Smith is age: 18 Id: 1 Ranking: 0
Person: Jane Austen is age: 23 Id: 2 Ranking: 6
Person: Spiro Agnew is age: 76 Id: 3 Ranking: 8
Person: Sarah Smith is age: 18 Id: 1 Ranking: 0
Person: Jane Austen is age: 23 Id: 2 Ranking: 4
Person: Spiro Agnew is age: 77 Id: 3 Ranking: 9
18
Jane Austen
77
9
0
2

Note how the id's increase automatically as the players are constructed and how the id's start at 1. As before, note that having your driver file work in the desired way does not guarantee you will pass all the tests - we will run more tests.

Question 3 - a Club class

Last but not least, write a Java class file called Club.java that represents a solitaire club. A club keeps track of its members. Members can belong to more than one club (the Solitaire Players Federation is organised but not jealous!). Your class should have the following attribute:

  • a collection to contain Players called members

It should have the following methods:

  • addMember a void method that takes a member as a parameter and adds the member to its collection of members.
  • a boolean method called removeMemberById which takes an integer parameter representing the id of a Player and tries to remove a player with that id from its collection of members. If the removal is successful this method should return true. If the player can't be found then this method should return false.
  • a method called getHighestRankedPlayer that returns the Player in the club that has the highest ranking. Note that if more than one player has the equal highest ranking the player who was added to the club most recently will be the one returned. If the club is empty this method should return null.
  • a method called printMembers() that calls toString on each Player in the club to print out all the members in the order in which they were added.

When you compile and run the driver file you should see the output:

Club 1
Person: Sally Smith is age: 18 Id: 1 Ranking: 0
Person: Jane Austen is age: 23 Id: 2 Ranking: 6
Person: Yasi Jones is age: 53 Id: 5 Ranking: 2
Club 2
Person: Sally Smith is age: 18 Id: 1 Ranking: 0
Person: Spiro Agnew is age: 76 Id: 3 Ranking: 8
Person: Jenny Lee is age: 33 Id: 4 Ranking: 8
Club 1
Person: Sally Smith is age: 18 Id: 1 Ranking: 1
Person: Jane Austen is age: 23 Id: 2 Ranking: 4
Person: Yasi Jones is age: 53 Id: 5 Ranking: 4
Club 2
Person: Sally Smith is age: 18 Id: 1 Ranking: 1
Person: Spiro Agnew is age: 76 Id: 3 Ranking: 10
Person: Jenny Lee is age: 33 Id: 4 Ranking: 9
true
false
Club 1
Person: Jane Austen is age: 23 Id: 2 Ranking: 4
Person: Yasi Jones is age: 53 Id: 5 Ranking: 4
Club 2
Person: Sally Smith is age: 18 Id: 1 Ranking: 1
Person: Spiro Agnew is age: 76 Id: 3 Ranking: 10
Person: Jenny Lee is age: 33 Id: 4 Ranking: 9
Highest Ranked Players
Person: Yasi Jones is age: 53 Id: 5 Ranking: 4
Person: Spiro Agnew is age: 76 Id: 3 Ranking: 10

As before note that having your driver file work in the desired way does not guarantee you will pass all the tests - we will run more tests.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.