Question
Programming Project: This assignment requires you to download the following files through Canvas: Card.java(below) and DeckMethods.java(below) . Please look them over, they are self-explanatory: Card
Programming Project:
This assignment requires you to download the following files through Canvas: Card.java(below) and DeckMethods.java(below). Please look them over, they are self-explanatory: Card represents a playing card and DeckMethods.java provides methods to manipulate an array of instances of the class Card.java.
This assignment requires that you write a program (name it DemoCard.java) that will create an array of cards, populate that array with instances of the class card (a standard deck), shuffle that array, create two additional arrays of cards of size 5 and place 5 cards from the original shuffled array into each of the two new arrays, alternating between the two new arrays. The methods in the class DeckMethods provide all that you need to do this - use those methods. In plain English you are creating a deck of cards, shuffling that deck, and then dealing 5 cards to two different players. You are then to sort the two arrays that were dealt by the int stored in the instance variable value (you will have to write your own code for this - the methods in the Arrays class will not help you, and there is no Sort method in the DeckMethods class.
You are to use the methods of the class DeckMethods as much as possible.
Here is an outline of the steps you could take:
//declare and create an array of 52 cards - name it deck
//populate deck with instances of the class card - making a complete deck of cards
//print the array deck
//shuffle the array deck
//print the array deck (which is now shuffled)
//declare and create two arrays of Cards each of length 5 name one array player1 and the other player2
//deal (by using the method pop) alternating cards from deck, adding them to the arrays player1 and player2 - you are dealing 5 cards to each player //alternating between the players
//print the arrays player 1 and player2
//sort the arrays player1 and player2 based on the int stored in the instance variable - you will have to create your own method for this
//print the arrays player1 and player2
Card.java:
/*
* Author: Prof Brown-Sederberg
* Description: This class represents a playing card
* Date Last Revision: 6.15.2018
*/
public class Card
{
//instance variables
private String suit;
private String face;
private int value;
private boolean shuffled;
private boolean dealt;
//constructors
public Card()
{
suit = "";
face = "";
value = 0;
shuffled = false;
dealt = false;
}//end default constructor
public Card(String suitPassed, String facePassed,
int valuePassed, boolean shuffledPassed,
boolean dealtPassed)
{
suit = suitPassed;
face = facePassed;
value = valuePassed;
shuffled = shuffledPassed;
dealt = dealtPassed;
}//end constructor method
//getters
public String getSuit()
{
return suit;
}//end getSuit
public String getFace()
{
return face;
}//end getFace
public int getValue()
{
return value;
}//end getValue
public boolean getShuffled()
{
return shuffled;
}//end getShuffled
public boolean getDealt()
{
return dealt;
}//end getDealt
//setter methods
public void setSuit(String suitPassed)
{
suit = suitPassed;
}//end setSuit
public void setFace(String facePassed)
{
face = facePassed;
}//end setFace
public void setValue(int valuePassed)
{
value = valuePassed;
}//end setValue
public void setShuffled(boolean shuffledPassed)
{
shuffled = shuffledPassed;
}//end setShuffled
public void setDealt(boolean dealtPassed)
{
dealt = dealtPassed;
}//end setDealt
public String toString()
{
return "Suit: " + suit + " Face: " + face + " Value: " + value + " Shuffled: " + shuffled + " Dealt: " + dealt;
}
}//end class card
DeckMethods.java:
/*
* Author: Prof Brown-Sederberg
* Description: This class provides methods to manipulate object of type Card (Card.java)
* Date Last Revision: 6.15.2018
*/
public class DeckMethods
{
/*
* appends an instance of the class Card to th an array of instances of the class card
* if there is room in hat array
*/
public static void appendCard(Card[] deckOfCardsPassed,
Card cardToAppend)
{
deckOfCardsPassed[countElements(deckOfCardsPassed)] = cardToAppend;
}//end method appendCard
/*
* returns the number of elements in an array of Cards
* i.e. the number of spaces filled with instances of the class Card
* in the array
*/
public static int countElements(Card[] deckOfCardsPassed)
{
int count = 0;
for(int i = 0; i < deckOfCardsPassed.length; ++i)
{
if(deckOfCardsPassed[i] != null)
++count;
}//end loop
return count;
}//end method countElements
/*
* returns thefirst card (in position [0] of an aray of Cards
* moves the remaining Cards forward in the array
*/
public static Card popCard(Card[] deckOfCardsPassed)
{
Card cardToBeReturned = deckOfCardsPassed[0];
for(int i = 1; i < deckOfCardsPassed.length; ++i)
{
deckOfCardsPassed[i - 1] = deckOfCardsPassed[i];
}//end for loop
deckOfCardsPassed[deckOfCardsPassed.length - 1] = null;
return cardToBeReturned;
}//end popCard
/*
* populates and array with nulls
*/
public static void populateNull(Object[] arrayPassed)
{
for(int i = 0; i < arrayPassed.length; ++i)
{
arrayPassed[i] = null;
}//end for loop
}//end method populateNull
/*
* Divides an array of instances of class Card into two arrays of equal size
*/
public static void cutTheDeck(Card[] deckOfCardsPassed,
Card[] playerDeckPassed, Card[]computerDeckPassed)
{
for(int i = 0; i < deckOfCardsPassed.length/2; ++i)
{
playerDeckPassed[i] = deckOfCardsPassed[i];
computerDeckPassed[i] =
deckOfCardsPassed[i + deckOfCardsPassed.length/2];
}//end for loop
}//end method cutTheDeck
/*
* Prints the values of the valiables of instances of the class Card
* in an array of Cards
*/
public static void printDeck(Card[] deckPassed)
{
for(int i = 0; i < deckPassed.length; ++i)
{
if(deckPassed[i] != null)
System.out.println(deckPassed[i].toString());
}//end loop
}//end method printDeck
/*
* Randomizes the instances of the class Card within an
* array of instaces of the class Card
*/
public static void shuffleDeck(Card[] deckPassed)
{
for(int i = 0; i < deckPassed.length; ++i)
{
int j = (int)(Math.random() * deckPassed.length);
Card temp = deckPassed[i];
deckPassed[i] = deckPassed[j];
deckPassed[j] = temp;
deckPassed[i].setShuffled(true);
deckPassed[j].setShuffled(true);
}//end for loop
}//end method shuffleDeck
/*
* Creates instances of the class Card and populates an array of instances of the class Card
* with those instances, the array therefore holds 52 instances of the class Card representing an
* ushuffeled deck of cards
*/
public static void populateDeck(Card[] deckPassed)
{
int position = 0;
String[] suit = {"Hearts", "Clubs", "Diamonds", "Spades"};
String[] face = {"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int[] value ={1,2,3,4,5,6,7,8,9,10,10,10,10};
for(int i = 0; i < suit.length; ++i)
{
for(int j = 0; j < face.length; ++j)
{
deckPassed[position] = new Card(suit[i], face[j], value[j],
false, false);
position++;
}//end inner loop
}//end outer for loop
}//end method shuffleDeck
}//end class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started