public class Card implements Comparable
{
// other methods and data fields not shown
/**
* Return a value according to whether
* this Card is greater, equal to, or less than
* the card passed in as parameter o. The value
* returned is consistent with the specifications
* of the Comparable interface's compareTo method.
*/
public int compareTo(Object o)
{
// implementation not shown
}
}
The card game to be simulated is a two-player game called
"Compete" that is played with cards. Cards can be compared
to see which card is greater in value, or if the cards are
equal. The object of the game is to win all of the cards.
Each player has a pile of cards and both piles start with the same number of cards. Play consists of a sequence of "rounds". Play continues until at the end of a round at least one player is out of cards. A discard pile is created and used during the rounds and is empty at the beginning of each round.
A round is played by executing the following steps until the round ends.
The class Compete is used to simulate the card game. Write private method appendQueue, which is described as follows. Method appendQueue should remove all the cards from parameter source and add them to parameter destination in the same order.
Complete method appendQueue below.
/**
* Remove all objects from source and put them
* in destination in the order in which they're removed.
*/
private void appendQueue(Queue destination, Queue source)
{
}
The constructor and data field myDiscards for the class Compete are shown below. In writing oneRound you should use myDiscards as the discard pile. You may call method appendQueue from part (a). Assume appendQueue works as specified, regardless of what you wrote in part (a). Assume that the class Card correctly implements the Comparable interface for comparing cards to see whether one card is less than, equal to, or greater than another card.
public class Compete
{
private Queue myDiscards;
/**
* Construct with empty discard pile
*/
public Compete()
{
// implementation not shown
}
/**
* Remove all objects from source and put them
* in destination in the order in which they're removed.
*/
private void appendQueue(Queue destination, Queue source)
{
// implementation not shown
}
/**
* precondition: myDiscards is empty
* Simulate one round of Compete.
*/
public void oneRound(Queue pile1, Queue pile2)
{
// write this method
}