Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: To refresh your Java programming skills and to emphasize the object-oriented programming approach used in Java. Specifically, you will work with control structures, class-building,

Purpose: To refresh your Java programming skills and to emphasize the object-oriented programming approach used in Java. Specifically, you will work with control structures, class-building, interfaces and generics to create and utilize a simple array-based data structure.

Goal 1: To design and implement a simple class MultiDS that will act as a simple data structure for accessing Java Objects. Your MultiDS class will primarily implement 2 interfaces PrimQ and Reorder. The details of these interfaces are explained in the files PrimQ.java and Reorder.java. Read these files over very carefully before implementing your MultiDS class.

Goal 2: To utilize your MultiDS class by implementing a simple version of the card game "Snap". In this case your program will be a client using MultiDS and the details of the MultiDS implementation will be abstracted out.

DETAILS

Details 1: For the details on the functionality of your MultiDS class, carefully read over the files PrimQ.java, Reorder.java and Assig1A.java provided on the CourseWeb folder where you find this assignment description. You must use these files as specified and cannot remove/alter any of the code that is already written in them. There are different ways of implementing the PrimQ and Reorder interface methods, some of which are more efficient than others. Try to think of the best way of implementing these methods in this assignment, but the most important thing at this point is getting them to work. A lot of pencil and paper work is recommended before actually starting to write your code. Later we will discuss the relative merits of different implementations. Your MultiDS class header should be:

public class MultiDS implements PrimQ, Reorder

Important Note: The primary data within your MultiDS class must be an array. You may not use any predefined Java collection class (e.g., ArrayList) for your MultiDS data.

After you have finished your coding of MultiDS, the Assig1A.java file provided for you should compile and run correctly, and should give output identical to the output shown in the sample executions (except for the segments where the data is shuffled, since it will be pseudo-random in that case).

Details 2: Snap is a simple card game that has many variations. You will implement a simple version as described below:

This is a multi-player game. The number of players is between 2 and 7, inclusive. Lets use the symbol n to denote the number of players.

Initially shuffle a 52-card standard deck of cards.

Deal the cards out completely to the players, alternating cards to each player. If 52%n != 0, assign each player 52/n cards and move the remaining cards (52%n) to the snap-pool.

Each player puts his cards into his face-down card pile and has his face-up pile initially empty.

A snap-pool pile (at the center of the table) is initially empty as well.

In total, we have 2n+1 card piles, n+1 piles with face-up cards (each players face-up pile and the snap-pool and n face-down piles.

Do the following until the first player runs out of cards or the number of iterations is equal to the specified number of rounds (whichever happens first should stop the loop):

1. Each player moves the top card of his face-down pile into his face-up pile.

2. If two cards at the top of any of the face-up piles have the same rank (suits don't matter), a match is declared.

3. Each player has a probability of shouting Snap! when a match occurs and another probability of (incorrectly) shouting Snap! when no match occurs. These two probabilities are 0.4 and 0.01, respectively, for all players.

4. The player who correctly shouts Snap! when no other player shouts wins the round. She then moves the matching piles into the bottom of her own face-down pile after shuffling them. The winning player in the round gets the face-up pile of the other player and the snap-pool pile into his face-down pile.

5. Any player (can be more than one player) who incorrectly shouts Snap! moves his face-up cards into the bottom of the snap-pool pile after shuffling.

The following rules also apply to the game:

If at any point in the game a player's face-down pile is empty, the player should move the cards in his face-up pile into his face-down pile after shuffling.

If at any point in the game a player has no cards remaining in both his piles, he/she is out of the game.

If more than one player have cards remaining after the set number of rounds, the player with the most cards (face-up + face-down) is the winner. In this case, it is possible for a tie between p players to occur, with each player having 52/p cards.

IMPLEMENTATION REQUIREMENTS

Your initial card deck, the players' face-up and face-down piles, and the snap-pool pile must all be stored in MultiDS objects.

The Card class must be used as provided and cannot be changed.

The number of players and the maximum number of rounds for the game must be read in from the command line.

To allow better testing of your program, you must output when correct and incorrect snaps occur and when piles are reshuffled. See the various sample output files for required output information.

Given Files not meant to be altered in any way:

Carefully read the specifications for each of the operations and implement them correctly in your MultiDS class.

The overall logic of the PrimQ is the following:

Data is logically "added" in the same order that it is "removed". However, there is no requirement for the physical storage of the actual data. Your only requirement for the MultiDS class is that all of the methods work as specified and that your MultiDS class have an array as its primary data. You MAY NOT use ArrayList, Vector or any predefined collection class for your MultiDS data. However, you may want to use some additional instance variables to keep track of the data effectively.

Note: Later in the term we will discuss how to implement a queue in an efficient way*/

public interface PrimQ

{

// Add a new Object to the PrimQ in the next available location. If

// all goes well, return true. If there is no room in the PrimQ for

// the new item, return false (you should NOT resize it)

public boolean addItem(T item);

// Remove and return the "oldest" item in the PrimQ. If the PrimQ

// is empty, return null.

public T removeItem();

// Returns the "oldest" item in the PrimQ. If the PrimQ

// is empty, return null.

public T top();

// Return true if the PrimQ is full, and false otherwise

public boolean full();

// Return true if the PrimQ is empty, and false otherwise

public boolean empty();

// Return the number of items currently in the PrimQ

public int size();

// Reset the PrimQ to empty status by reinitializing the variables

// appropriately

public void clear();

}

- Assig1A driver program. This program must work as is with your MultiDS class. Look carefully at all of the method calls so that you create your MultiDS methods correctly. For example, note the constructor calls and the toString() method call. The output should be identical to my sample output, with the exception of the result of the shuffle() methods -- since this should be random yours should not match mine */

public class Assig1A

{

public static void main(String [] args)

{

// Testing constructor and PrimQ interface

PrimQ theQ = new MultiDS(5);

// Testing addItem

for (int i = 0; i < 6; i++)

{

Integer newItem = 2 * i;

if (!(theQ.full()))

{

theQ.addItem(newItem);

System.out.println(newItem + " added to Q");

}

else

{

System.out.println("No room for " + newItem);

}

}

// Testing removeItem

while (!(theQ.empty()))

{

Integer oldItem = theQ.removeItem();

System.out.println(oldItem + " retrieved from Q");

}

Integer noItem = theQ.removeItem();

if (noItem == null)

System.out.println("Nothing in the Q");

// Testing array management

int count = 1;

PrimQ theQ2 = new MultiDS(5);

String theItem = new String("Item " + count);

System.out.println("Adding " + theItem);

theQ2.addItem(theItem);

for (int i = 0; i < 8; i++)

{

count++;

theItem = new String("Item " + count);

System.out.println("Adding " + theItem);

theQ2.addItem(theItem);

theItem = theQ2.removeItem();

System.out.println("Removing " + theItem);

}

int sz = theQ2.size();

System.out.println("There are " + sz + " items in the buffer");

// This code will test the toString() method and the Reorder

// interface.

System.out.println(" About to test Reorder methods");

MultiDS newDS = new MultiDS(15);

for (int i = 0; i < 8; i++)

{

newDS.addItem(i);

}

System.out.println(newDS.toString());

System.out.println("Reversing");

newDS.reverse();

System.out.println(newDS.toString());

System.out.println("Removing 3 items then adding 1");

newDS.removeItem();

newDS.removeItem();

newDS.removeItem();

newDS.addItem(8);

System.out.println(newDS.toString());

System.out.println("Reversing");

newDS.reverse();

System.out.println(newDS.toString());

System.out.println("Shifting right");

newDS.shiftRight();

System.out.println(newDS.toString());

System.out.println("Shifting left twice");

newDS.shiftLeft();

newDS.shiftLeft();

System.out.println(newDS.toString());

System.out.println(" About to test shuffle...");

newDS.clear();

for (int i = 0; i < 10; i++)

{

newDS.addItem(i);

}

System.out.println(newDS.toString());

System.out.println("Shuffling...");

newDS.shuffle();

System.out.println(newDS.toString());

System.out.println("Removing 2 and adding 1");

newDS.removeItem();

newDS.removeItem();

newDS.addItem(22);

System.out.println(newDS.toString());

System.out.println("Shuffling again");

newDS.shuffle();

System.out.println(newDS.toString());

}

}

- Card class for Assigment 1. You must use this class as given in the implementation of your Snap card game.*/

public class Card implements Comparable

{

// These are enum types that make the suits and ranks of the cards more readable. They

// are also used for comparison of cards, as shown in equals and compareTo below. You

// can access the type values directly using the class, which you will need to do to

// initialize your deck of cards. See A1Help.java for a demo of this.

public static enum Suits {Spades, Clubs, Diamonds, Hearts}

public static enum Ranks {Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace}

private Suits suit;

private Ranks rank;

public Card(Suits s, Ranks r)

{

suit = s;

rank = r;

}

// Equals is only true if both the suit and the rank of the cards match

public boolean equals(Object rhs)

{

if(rhs instanceof Card) {

Card rside = (Card) rhs;

if (suit == rside.suit && rank == rside.rank)

return true;

else

return false;

} else {

return false;

}

}

public String toString()

{

return (rank + "-of-" + suit);

}

// Compare this object to another Card. Cards of the same rank are

// considered equal, regardless of the suit. Note that this differs

// from the equals method. Java enum types are automatically Comparable,

// based on the order in which the values are defined (smallest to largest).

public int compareTo(Card rhs)

{

return rank.compareTo(rhs.rank);

}

}

- Carefully read the specifications for the methods below and

implement them in your MultiDS class. As with the PrimQ

interface, don't worry as much about efficiency here as you do

about correctness.

*/

public interface Reorder

{

// Logically reverse the data in the Reorder object so that the item

// that was logically first will now be logically last and vice

// versa. The physical implementation of this can be done in

// many different ways, depending upon how you actually implemented

// your physical MultiDS class

public void reverse();

// Remove the logical last item of the DS and put it at the

// front. As with reverse(), this can be done physically in

// different ways depending on the underlying implementation.

public void shiftRight();

// Remove the logical first item of the DS and put it at the

// end. As above, this can be done in different ways.

public void shiftLeft();

public void shuffle();

// Reorganize the items in the object in a pseudo-random way. The exact

// way is up to you but it should utilize a Random object (see Random in

// the Java API). Thus, after this operation the "oldest" item in the

// DS could be arbitrary.

}

You must create two files named MultiDS.java and Snap.java

HINTS / NOTES 1. See program A1Help.java for some help with the Card class and using the MultiDS class with Card objects. 2. See file A1Out.txt to see how your output for Assig1A should look. As noted, your output when running Assig1A.java should be identical to this with the exception of the order of the values after being shuffled. 3. See files SnapOut1.txt, SnapOut2.txt, SnapOut3.txt, and SnapOut4.txt for example runs of my Snap.java program. Your Snap program output does not have to look exactly like this but the functionality should be the same. 4. Your MultiDS class will need to allow for a variable number of objects, up to some maximum number (set by the initial size). You can implement this by having an integer instance variable (e.g., "count") that indicates how many slots in the MultiDS are filled. For example, you could have a MultiDS with a capacity of 52 cards for a pile with anywhere from 0 up to 52 Cards in it. In this case, the array length (i.e., physical size) would be 52 but the "count" (i.e., logical size) would be some value between 0 and 52. When implementing MultiDS be careful to use the "count" value rather than the array length when doing operations such as the toString() method. 5. In order for the Assig1A.java output to show correctly, you must override the toString() method in the MultiDS class. You should be familiar with the toString() method from CS 0401. 6. You can easily generate the "deck" of cards for your Snap game with nested for loops. You should NOT have 52 individual statements to do this! See A1Help.java for some hints about how to generate the deck of cards. 7. Note that for the most part, all of your methods (in both interfaces) in your MultiDS class should act on the logical data structure -- accessing only the locations in the array that actually are storing data (See note 4 above). 8. You can use an object of the MultiDS class to determine winning and incorrect Snaps conditions each round. 9. MultiDS can be the type of the elements stored in your data structure. That is, you can define an object of type MultiDS>. You may add the below static method to the MultiDS class. It retrieves the item with logical order index inside a MultiDS object --- without affecting the order. It does that by calling shiftLeft(), top(), and shiftRight(). Of course, this way is not efficient at all. Some data structures perform poorly if you ask them to do an operation they are not designed for! You may be able to do slightly better without the getItem method. We will see data structures that more efficiently gets an item using an index later in the course. static public T getItem(MultiDS container, Integer index){ for(int i=0; i container.shiftLeft(); } T result = container.top(); for(int i=0; i container.shiftRight(); } return result; 5 } You can call that static method from elsewhere in the code as Mul

Some hints/help for Assignment 1.

Note: Parts of this program (identified below) will not work until you have completed the MultiDS implementation. To see the rest of it execute simply comment out that section.*/

public class A1Help

{

public static void main(String [] args)

{

// Demo of Card class

Card c1 = new Card(Card.Suits.Diamonds, Card.Ranks.Seven);

Card c2 = new Card(Card.Suits.Hearts, Card.Ranks.Queen);

Card c3 = new Card(Card.Suits.Spades, Card.Ranks.Queen);

Card c4 = new Card(Card.Suits.Spades, Card.Ranks.Queen);

System.out.println("Card 1 is " + c1.toString());

System.out.println("Card 2 is " + c2.toString());

System.out.println("Card 3 is " + c3.toString());

System.out.println("Card 4 is " + c4.toString());

compareCards(c1, c2);

compareCards(c1, c3);

compareCards(c2, c3);

compareCards(c4, c1);

compareCards(c4, c3);

for (int rankOfCards = 0; rankOfCards <= Card.Ranks.Ace.ordinal(); rankOfCards++) {

for (Card.Suits suitOfCards : Card.Suits.values()) {

System.out.println(rankOfCards + " " + suitOfCards);

switch (suitOfCards) {

case Spades:

default:

break;

}

}

}

System.out.println("Here are all of the suits:");

for (Card.Suits s: Card.Suits.values())

System.out.print(s + " ");

System.out.println(" ");

System.out.println("Here are all of the ranks:");

for (Card.Ranks r: Card.Ranks.values())

System.out.print(r + " ");

System.out.println(" ");

// Below this line requires the MultiDS class. Comment this out if you

// have not yet finished your MultiDS class

// First let's make two MultiDS of Cards

MultiDS myCards = new MultiDS(10);

MultiDS otherCards = new MultiDS(10);

// Now put some cards into one

myCards.addItem(c1);

myCards.addItem(c2);

myCards.addItem(c3);

// Print it out

System.out.println("myCards " + myCards.toString());

// Move two into the other one

otherCards.addItem(myCards.removeItem());

otherCards.addItem(myCards.removeItem());

System.out.println("myCards " + myCards.toString());

System.out.println("otherCards " + otherCards.toString());

}

public static void compareCards(Card x, Card y)

{

int result = x.compareTo(y);

if (result > 0)

System.out.println(x + " beats " + y);

else if (result < 0)

System.out.println(x + " loses to " + y);

else

System.out.println(x + " ties " + y);

if (x.equals(y))

System.out.println(x + " and " + y + " are equal ");

else

System.out.println(x + " and " + y + " are not equal ");

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

What is job rotation ?

Answered: 1 week ago