Question

In: Computer Science

Add the following method below to the CardDeck class, and create a test driver to show...

Add the following method below to the CardDeck class, and create a test driver to show that they work correctly.

int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck.

Complete in Java programming language.

// Models a deck of cards. Includes shuffling and dealing.

//----------------------------------------------------------------------

package Homework4;

import java.util.Random;

import java.util.Iterator;

import javax.swing.ImageIcon;

public class CardDeck {

public static final int NUMCARDS = 52;

protected ABList<Card> deck;

protected Iterator<Card> deal;

public CardDeck() {

deck = new ABList<Card>(NUMCARDS);

ImageIcon image;

for (Card.Suit suit : Card.Suit.values())

for (Card.Rank rank : Card.Rank.values()) {

image = new ImageIcon("./src/Homework4/cards/" + suit + "_"

+ rank + "_RA.gif");

deck.add(new Card(rank, suit, image));

}

deal = deck.iterator();

}

public void shuffle()

// Randomizes the order of the cards in the deck.

// Resets the current deal.

{

Random rand = new Random(); // to generate random numbers

int randLoc; // random location in card deck

Card temp; // for swap of cards

for (int i = (NUMCARDS - 1); i > 0; i--) {

randLoc = rand.nextInt(i); // random integer between 0 and i - 1

temp = deck.get(randLoc);

deck.set(randLoc, deck.get(i));

deck.set(i, temp);

}

deal = deck.iterator();

}

public boolean hasNextCard()

// Returns true if there are still cards left to be dealt;

// otherwise, returns false.

{

return (deal.hasNext());

}

public Card nextCard()

// Precondition: this.hasNextCard() == true

//

// Returns the next card for the current 'deal'.

{

return deal.next();

}

}

Solutions

Expert Solution

package Homework4;

import java.util.Random;

import java.util.Iterator;

import javax.swing.ImageIcon;

public class CardDeck {

public static final int NUMCARDS = 52;

int count=0,res=0;

protected ABList<Card> deck;

protected Iterator<Card> deal;

public CardDeck() {

deck = new ABList<Card>(NUMCARDS);

ImageIcon image;

for (Card.Suit suit : Card.Suit.values())

for (Card.Rank rank : Card.Rank.values()) {

image = new ImageIcon("./src/Homework4/cards/" + suit + "_"

+ rank + "_RA.gif");

deck.add(new Card(rank, suit, image));

}

deal = deck.iterator();

}

public void shuffle()

// Randomizes the order of the cards in the deck.

// Resets the current deal.

{

Random rand = new Random(); // to generate random numbers

int randLoc; // random location in card deck

Card temp; // for swap of cards

for (int i = (NUMCARDS - 1); i > 0; i--) {

randLoc = rand.nextInt(i); // random integer between 0 and i - 1

temp = deck.get(randLoc);

deck.set(randLoc, deck.get(i));

deck.set(i, temp);

count=count+1;

}

deal = deck.iterator();

}

public boolean hasNextCard()

// Returns true if there are still cards left to be dealt;

// otherwise, returns false.

{

return (deal.hasNext());

}

public Card nextCard()

// Precondition: this.hasNextCard() == true

//

// Returns the next card for the current 'deal'.

{

return deal.next();

}

int cardsRemaining()

{

// veriable " res" is used to count the number of card remaining

if(this.hasNextCard() == true)

res=NUMCARDS-count;

return res;

}

}


Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method...
Please add the following method as a part of the UnorderedList class definition: •print_list: the method prints the elements of the list using the same format as a Python list (square brackets and commas as separators). In the main function of the modified UnorderedList.py, please test the new method to demonstrate that it works as expected. Please leave comments in the code to show what is done UnorderList.py file # Implementation of an Unordered List ADT as a linked list....
Complete the following Customer class below. Also, add the equals method that would compare each field....
Complete the following Customer class below. Also, add the equals method that would compare each field. Please pay attention to the format of the equals method as it needs to have the same format as shown below. public class Customer {       private int id; //customer id       private String name; //customer's name       private int discount; //discount rate in percent             //construct a new customer       public Customer(int id, String name, int discount) {                    }      ...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT