In: Computer Science
Download the following zip file Bag and complete the program. You will need to complete files MyBag and .
MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class.
In your BagHand class you should
The given driver should run without change.
Classes:
Bag-
package bag;
public interface Bag<T> extends Iterable<T> {
public boolean isEmpty();
public int size();
public void add(T item);
}
MyBag-
package bag;
import java.util.ArrayList;
import java.util.Iterator;
public class MyBag<T> implements Bag<T> {
private int count;
private ArrayList<T> list = new
ArrayList<T>(count);
public Iterator<T> iterator() {
return null;
}
public boolean isEmpty() {
// for students to complete
return false;
}
public int size() {
// for students to complete
return 0;
}
public void add(T item) {
// for students to complete
}
public void print() {
// for students to complete
}
}
MyBagHand-
package cards;
import bag.MyBag;
public class BagHand implements HandOfCards {
private MyBag myHand = new MyBag();
private int value;
public BagHand() {
// for students to complete
}
public void add(PlayingCard c) {
// for students to complete
}
// returns the added value of a players dealt
cards
public int getValue() {
// for students to complete
return 0;
}
public void print() {
System.out.print("\tValue:");
if (value == 21)
System.out.print("BLACKJACK!\t");
else
System.out.print("\t" + value + " \t\t");
myHand.print();
}
}
Deck-
package cards;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<PlayingCard> cards;
private Random rand;
public Deck(int n) {
super();
rand = new Random();
cards = new
ArrayList<PlayingCard>(n);
for (int suite = 1; suite <= 4;
suite ++)
for (int rank =
1; rank <= 13; rank ++) {
if ((suite - 1) * 13 + rank <= n) {
cards.add(new PlayingCard(rank, suite));
}
}
}
public PlayingCard dealOne() {
return
cards.get(rand.nextInt(52));
}
public String toString(){
String ret = "";
for (PlayingCard card :
cards){
ret += card +
"\n";
}
return ret;
}
}
HandOfCards-
package cards;
import cards.PlayingCard;
public interface HandOfCards {
public void add(PlayingCard c);
public int getValue();
public void print();
}
PlayingCards-
package cards;
// Card class
// Constructor creates a joker, deal() method sets a rank &
suit.
import java.util.Random;
public class PlayingCard {
public final static String suits[] = {"None",
"Hearts", "Spades", "Diamonds", "Clubs"};
public final static String ranks[] = {"Joker", "Ace",
"2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen",
"King"};
private int rank;
private int suit;
/**
* Default constructor sets the card to a random
value
*/
public PlayingCard ()
{
// Set each data member to a random
number between 1 and length-1
Random randNum = new
Random();
this.setRank(randNum.nextInt(ranks.length-1) + 1);
this.setSuit(randNum.nextInt(suits.length-1) + 1);
}
/**
* Constructor with String values
*
* @param rank the rank name
* @param suit the suit name
*/
public PlayingCard (String rank, String suit)
{
this.rank = find_entry(rank,
ranks);
this.suit = find_entry(suit,
suits);
}
/**
* Find the index of an entry in an array
*
* @param value the entry to search for
* @param values array to search
* @return index of the value in values (0
default)
*/
private static int find_entry(String value, String
values[])
{
int ret = 0;
for (int
ii=0;ii<values.length;ii++)
{
if
(value.toUpperCase() == values[ii].toUpperCase())
ret = ii;
}
return ret;
}
/**
* Constructor with int values
*
* @param rank the rank index
* @param suit the suit index
*/
public PlayingCard (int rank, int suit)
{
// Use setters instead of directly
assigning so that any logic
// validating the values can reside
in one place.
this.setRank(rank);
this.setSuit(suit);
}
// Getters & Setters
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank %
ranks.length;
}
public void setRank(String rank) {
this.rank = find_entry(rank,
ranks);
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit %
suits.length;
}
public void setSuit(String suit) {
this.suit = find_entry(suit,
suits);
}
/**
* Return string representation of the Card
*
* @return string in the form "rank of suit"
*/
public String toString ()
{
return (ranks[rank] + " of " +
suits[suit]);
}
}
bagDriver-
import cards.BagHand;
import cards.Deck;
public class bagDriver {
public static void main(String[] args) {
for(int i = 1; i <= 4; i++) { //
playing two rounds
System.out.println("Game : " + i);
Deck d = new
Deck(52);
for(int j = 1; j
<= 3; j++) { // three players
BagHand hand = new BagHand();
hand.add(d.dealOne());
hand.add(d.dealOne());
System.out.print("Hand : " + j);
hand.print();
}
System.out.print("\n\n");
}
}
}
// Bag.java
package bag;
public interface Bag<T> extends Iterable<T> {
public boolean isEmpty();
public int size();
public void add(T item);
}
// end of Bag.java
// PlayingCard.java
package cards;
// Card class
// Constructor creates a joker, deal() method sets a rank &
suit.
import java.util.Random;
public class PlayingCard {
public final static String suits[] = {"None", "Hearts",
"Spades", "Diamonds", "Clubs"};
public final static String ranks[] = {"Joker", "Ace", "2", "3",
"4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
private int rank;
private int suit;
/**
* Default constructor sets the card to a random value
*/
public PlayingCard ()
{
// Set each data member to a random number between 1 and
length-1
Random randNum = new Random();
this.setRank(randNum.nextInt(ranks.length-1) + 1);
this.setSuit(randNum.nextInt(suits.length-1) + 1);
}
/**
* Constructor with String values
*
* @param rank the rank name
* @param suit the suit name
*/
public PlayingCard (String rank, String suit)
{
this.rank = find_entry(rank, ranks);
this.suit = find_entry(suit, suits);
}
/**
* Find the index of an entry in an array
*
* @param value the entry to search for
* @param values array to search
* @return index of the value in values (0 default)
*/
private static int find_entry(String value, String values[])
{
int ret = 0;
for (int ii=0;ii<values.length;ii++)
{
if (value.toUpperCase() == values[ii].toUpperCase())
ret = ii;
}
return ret;
}
/**
* Constructor with int values
*
* @param rank the rank index
* @param suit the suit index
*/
public PlayingCard (int rank, int suit)
{
// Use setters instead of directly assigning so that any
logic
// validating the values can reside in one place.
this.setRank(rank);
this.setSuit(suit);
}
// Getters & Setters
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank % ranks.length;
}
public void setRank(String rank) {
this.rank = find_entry(rank, ranks);
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit % suits.length;
}
public void setSuit(String suit) {
this.suit = find_entry(suit, suits);
}
/**
* Return string representation of the Card
*
* @return string in the form "rank of suit"
*/
public String toString ()
{
return (ranks[rank] + " of " + suits[suit]);
}
}
//end of PlayingCard.java
// HandOfCards.java
package cards;
import cards.PlayingCard;
public interface HandOfCards {
public void add(PlayingCard c);
public int getValue();
public void print();
}
// end of HandOfCards.java
// Deck.java
package cards;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<PlayingCard> cards;
private Random rand;
public Deck(int n) {
super();
rand = new Random();
cards = new ArrayList<PlayingCard>(n);
for (int suite = 1; suite <= 4; suite ++)
for (int rank = 1; rank <= 13; rank ++) {
if ((suite - 1) * 13 + rank <= n) {
cards.add(new PlayingCard(rank, suite));
}
}
}
public PlayingCard dealOne() {
return cards.get(rand.nextInt(52));
}
public String toString(){
String ret = "";
for (PlayingCard card : cards){
ret += card + "\n";
}
return ret;
}
}
// end of Deck.java
// MyBag.java
package bag;
import java.util.ArrayList;
import java.util.Iterator;
public class MyBag<T> implements Bag<T> {
private int count;
private ArrayList<T> list = new
ArrayList<T>(count);
// returns an iterator to the Bag
public Iterator<T> iterator() {
return list.iterator();
}
// returns true if Bag is empty else return
false
public boolean isEmpty() {
return list.isEmpty();
}
// returns number of elements in the Bag
public int size() {
return list.size();
}
// inserts item at the end of Bag
public void add(T item) {
list.add(item);
}
public void print() {
// loop over the Bag, displaying the elements
separated by a comma
for(int i=0;i<list.size();i++)
{
System.out.print(list.get(i));
if(i <
list.size()-1) // this is not the last element
System.out.print(", ");
}
System.out.println(); // display a
new line at the end
}
}
//end of MyBag.java
// BagHand.java
package cards;
import bag.MyBag;
public class BagHand implements HandOfCards {
private MyBag myHand = new MyBag();
private int value;
// initialize an empty Bag
public BagHand() {
value = 0;
}
public void add(PlayingCard c) {
// add c to the bag
myHand.add(c);
// based on the rank of the card,
add points to values, Ace - 11, Jack, Queen, King - 10, and all
other have same as rank values
if(c.getRank() == 1)
value +=
11;
else if(c.getRank() <= 10)
value +=
c.getRank();
else
value +=
10;
}
// returns the added value of a players dealt cards
public int getValue() {
return value
}
public void print() {
System.out.print("\tValue:");
if (value == 21)
System.out.print("BLACKJACK!\t");
else
System.out.print("\t" + value + " \t\t");
myHand.print();
}
}
//end of BagHand.java
// bagDriver.java
import cards.BagHand;
import cards.Deck;
public class bagDriver {
public static void main(String[] args) {
for(int i = 1; i <= 4; i++) { // playing two rounds
System.out.println("Game : " + i);
Deck d = new Deck(52);
for(int j = 1; j <= 3; j++) { // three players
BagHand hand = new BagHand();
hand.add(d.dealOne());
hand.add(d.dealOne());
System.out.print("Hand : " + j);
hand.print();
}
System.out.print("\n\n");
}
}
}
//end of bagDriver.java
Output: