Need help converting this to linked list instead of using an array and the drive
ID: 3685987 • Letter: N
Question
Need help converting this to linked list instead of using an array and the driver class
public class Card
{
/********* Instance Data **************/
private String stringFaceValue; //string face of the card
private String stringSuitValue; //string suit of the card
private int numFaceValue; //integer face of the card
//ace is 1 and Jack-King is 11-13
private int numSuitValue; //integer suit of the card
private int uniqueID; //a unique ID of each card
/********** Contructors **************/
public Card(int inSuit, int inFaceVal)
{
numFaceValue = inFaceVal;
numSuitValue = inSuit;
stringFaceValue = "";
uniqueID = 0;
if(inSuit == 1)
stringSuitValue = "Hearts";
else if(inSuit == 2)
stringSuitValue = "Clubs";
else if(inSuit == 3)
stringSuitValue = "Diamonds";
else
stringSuitValue = "Spades";
}
/************ Methods ***************/
public String toString()
{
//local constants
//local variables
String format; //formatted string to be returned
/********** begin toSTring ***************/
//check which face value to return
switch(numFaceValue)
{
case 1:
stringFaceValue = "Ace";
break;
case 2:
stringFaceValue = "Two";
break;
case 3:
stringFaceValue = "Three";
break;
case 4:
stringFaceValue = "Four";
break;
case 5:
stringFaceValue = "Five";
break;
case 6:
stringFaceValue = "Six";
break;
case 7:
stringFaceValue = "Seven";
break;
case 8:
stringFaceValue = "Eight";
break;
case 9:
stringFaceValue = "Nine";
break;
case 10:
stringFaceValue = "Ten";
break;
case 11:
stringFaceValue = "Jack";
break;
case 12:
stringFaceValue = "Queen";
break;
case 13:
stringFaceValue = "King";
break;
}//end switch
//format the string
format = stringFaceValue + " of " + stringSuitValue;
//return the string
return format;
}
public int getNumber()
{
return numFaceValue;
}
} // end class Card'
---------------------------------------------
import java.lang.*;
import java.text.*;
import java.util.*;
public class DeckofCards
{
private static Card[] cards;//the array of cards
private static int numCards;//the number of cards in the deck
public DeckofCards(boolean shuffle)
{
numCards = 52;
cards = new Card[numCards];
int cardIndex = 0;
//for each suit of cards
for(int suit = 0; suit < 4; suit++)
{
for(int faceVal = 1; faceVal <= 13; faceVal ++)
{
//add a new card to deck
cards[cardIndex] = new Card(suit, faceVal);
cardIndex++;
}
}
//if the user wants to shuff
if(shuffle == true)
{
DeckofCards.shuffle();
}
}//end constructor
public static void shuffle()
{
//initialize random number generator
Random shuffleDeck = new Random();
//temporary card value
Card temp;
int tempIndex;
for(int i = 0; i < numCards; i++)
{
//get a random card to swap
tempIndex = shuffleDeck.nextInt(numCards);
//swap the cards
temp = cards[i];
cards[i] = cards[tempIndex];
cards[tempIndex] = temp;
}
}//end shuffle
public Card dealNextCard()
{
//get the top card
Card top = cards[0];
for(int c = 1; c < numCards; c++)
{
cards[c - 1] = cards[c];
}
cards[numCards - 1] = null;
numCards--;
return top;
}//end dealNextCard
public void printDeck()
{
for(int c = 0; c < numCards; c++)
{
System.out.println(cards[c]);
}
}//end printDeck
}
------------------------
import java.lang.*;
import java.text.*;
import java.util.*;
public class DeckofCards
{
private static Card[] cards;//the array of cards
private static int numCards;//the number of cards in the deck
public DeckofCards(boolean shuffle)
{
numCards = 52;
cards = new Card[numCards];
int cardIndex = 0;
//for each suit of cards
for(int suit = 0; suit < 4; suit++)
{
for(int faceVal = 1; faceVal <= 13; faceVal ++)
{
//add a new card to deck
cards[cardIndex] = new Card(suit, faceVal);
cardIndex++;
}
}
//if the user wants to shuff
if(shuffle == true)
{
DeckofCards.shuffle();
}
}//end constructor
public static void shuffle()
{
//initialize random number generator
Random shuffleDeck = new Random();
//temporary card value
Card temp;
int tempIndex;
for(int i = 0; i < numCards; i++)
{
//get a random card to swap
tempIndex = shuffleDeck.nextInt(numCards);
//swap the cards
temp = cards[i];
cards[i] = cards[tempIndex];
cards[tempIndex] = temp;
}
}//end shuffle
public Card dealNextCard()
{
//get the top card
Card top = cards[0];
for(int c = 1; c < numCards; c++)
{
cards[c - 1] = cards[c];
}
cards[numCards - 1] = null;
numCards--;
return top;
}//end dealNextCard
public void printDeck()
{
for(int c = 0; c < numCards; c++)
{
System.out.println(cards[c]);
}
}//end printDeck
}
Explanation / Answer
Please find the Linked List implementation below :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.