Using Linked Lists, design a Deck of Cards with the following criteria. Define a
ID: 3680229 • Letter: U
Question
Using Linked Lists, design a Deck of Cards with the following criteria. Define a Class Card that will store Ac face value of the Card and the Sim Value 1. Include five fields: Numcfic Face Value - fof processing purposes Siring lace Value - tor printing purposes Niencnc Suit Value - foe pruceimg purposes String Suit Value - for priming purposes Unique II) Card number of 1 to 52 (or 0 to 51) Next field to link to the next cud. Have the constructor initialize the next field to null. No data will be set wnhin Ac constructor Include a id and a get method for each of the following; Stnng Facc Value String Suit Value. Numeric Face Value. Numcnc Sint Value. ID number of card Next field of card (pointer to the next catd in deck). Create a class for Ac Deck of Cards containing a set of operations that do the following-1. Include the following fields. Head node - points to the firat card in Ac dcck. First node a temporary pointer that points to the first card in Ac deck. Current node - a node that poems to the current card when traveling A rough the deck of canls (the linked list) Previous node a node that point), to the previous card vitslcd when tnvclinj; through the deck of cards (linked list) Next node - a node that points to next card in Ac dcck. Toil node potato to the laaUunl in Ac dcck. Number of cards bow many cards air currently A the dcck? Suit value - contains the suit of the current card Face value contains the lace value of the cutreo: card. Unique ID - con-jim? the unKjue ID number of Ac current card You may not need all oflhete - it depends upoa your logic. You may need more or you may need lew. Initializes a Linked List that contains all 52 cords in order. The mean you arc populating your linked lot with 52 cards. Checks U> see if the dcck is empty or out. Display the entire deck (this will be fur debuggmg pioposes is you test this class). a Driver program that contains a menu of the following You roust use Referenced-based Linked List for A is program. Create a new deck Print the dcckExplanation / Answer
Node Class
public class Node {
private Comparable data;
private Node next;
public Node(){
next = null;
}
public Node(Comparable c){
data = c;
next = null;
}
public Node(Comparable c, Node n){
data = c;
next = n;
}
public Comparable getData(){
return data;
}
public void setData(Comparable c){
data = c;
}
public Node getNext(){
return next;
}
public void setNext(Node n){
next = n;
}
}
LinkedList class
public class LinkedList {
private Node first = null;
private Node current = null;
private Node pre = null;
public boolean isEmpty(){
return true;
}
public boolean contains(Comparable item){
current = first;
pre = null;
while ((current != null)&&(current.getData().compareTo(item) < 0)){
pre = current;
current = current.getNext();
}
return ((current != null) && (current.getData().compareTo(item) == 0));
}
public int size(){
int count = 0;
current = first;
pre = null;
while (current != null){
pre = current;
current = current.getNext();
count++;
}
return count;
}
public void add(Comparable c){
Node temp = new Node(c);
if (pre == null){
first = temp;
}else{
pre.setNext(temp);
}
temp.setNext(current);
current = temp;
}
public void remove(Comparable c){
if (pre == null){
first = first.getNext();
}else{
current = current.getNext();
if (pre == null){
first = current;
}else{
pre.setNext(current);
}
}
}
public void clear(){
first = null;
}
public void print(){
Node current = first;
while (current != null){
System.out.println(current.getData());
current = current.getNext();
}
}
}
Card class
public class Card implements Comparable<Card> {
private int rank;
private int suit;
public Card(int suit, int rank){
this.rank = rank;
this.suit = suit;
}
public int getRank(){
return rank;
}
public int getSuit(){
return suit;
}
public String toString(){
switch(suit){
case 1:
switch(rank)
{
case 11: return "Jack of Hearts";
case 12: return "Queen of Hearts";
case 13: return "King of Hearts";
case 14: return "Ace of Hearts";
default: return rank + " of Hearts";
}
case 2:
switch(rank)
{
case 11: return "Jack of Diamonds";
case 12: return "Queen of Diamonds";
case 13: return "King of Diamonds";
case 14: return "Ace of Diamonds";
default: return rank + " of Diamonds";
}
case 3:
switch(rank)
{
case 11: return "Jack of Clubs";
case 12: return "Queen of Clubs";
case 13: return "King of Clubs";
case 14: return "Ace of Clubs";
default: return rank + " of Clubs";
}
case 4:
switch(rank)
{
case 11: return "Jack of Spades";
case 12: return "Queen of Spades";
case 13: return "King of Spades";
case 14: return "Ace of Spades";
default: return rank + " of Spades";
}
}//end Switch Statement
return null;
}
public int compareTo(Card a) {
if (this.rank < a.rank){
return -1;
}
if (this.rank > a.rank){
return 1;
}
if (this.rank == a.rank){
if (this.suit < a.suit){
return -1;
}
if (this.suit > a.suit){
return 1;
}
}
return 0;
}
}
CardDeck class - a linked list of card
import java.util.Random;
public class CardDeck {
private LinkedList cards=new LinkedList();
private int numCards;
public void Deck(){
for (int a = 1; a <= 4; a++){
for (int b = 1; b <= 14; b++){
cards.add(new Card(a,B)/>);
}
}
public void drawFromDeck(){
Random rand = new Random();
int index = rand.nextInt(cards.size());
cards.remove(index);
numCards--;
}
public int getTotalCard(){
return cards.size();
}
}
Main class
public class Main {
public static void main(String[] args){
LinkedList myList = new LinkedList();
CardDeck myCards = new CardDeck();
myCards.Deck();
myList.print();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.