Question

In: Computer Science

Download the following zip file Bag and complete the program. You will need to complete files...

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

  • set appropriate default values for select class variables in the constructor
  • in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set the appropriate rank of the playing card so you can keep track of the total value of the hand.
    • Jack, Queen, and King should be valued at 10 and and ace(1) should be valued at 11.
  • your print statement should return BLACKJACK if the value of the hand is equal to 21, BUST if the values of the hand is over 21, or just print out the value of the hand as well as each card in the hand.

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");
       }
   }

}

Solutions

Expert Solution

// 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:


Related Solutions

you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that...
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that contains counts for characters in a text (see submission folder on iCollege) and put it into the root of related Eclipse project folder. To view your project folder through system file explorer, right-click on ‘src’ folder in the Eclipse project explorer and choose ‘Show In->System Explorer’. Consider the following Java code, that reads a .csv file: BufferedReader csvReader = new BufferedReader(new FileReader("letter_count.csv")); String currentRow...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files...
In this programming assignment, you will implement a SimpleWebGet program for non- interactive download of files from the Internet. This program is very similar to wget utility in Unix/Linux environment.The synopsis of SimpleWebGet is: java SimpleWebGet URL. The URL could be either a valid link on the Internet, e.g., www.asu.edu/index.html, or gaia.cs.umass.edu/wireshark-labs/alice.txt or an invalid link, e.g., www.asu.edu/inde.html. ww.asu.edu/inde.html. The output of SimpleWebGet for valid links should be the same as wget utility in Linux, except the progress line highlighted...
Since you will be using zip or compressed files; Explain how to zip (compress) and unzip...
Since you will be using zip or compressed files; Explain how to zip (compress) and unzip (uncompress/extract) files. How to extract or uncompress files into a folder. Why do you need to uncompress/extract the files? The importance of knowing about zip/compressed files in digital forensics.
Download and review the PrintNumbers.java program provided in the homework files. This program contains a method...
Download and review the PrintNumbers.java program provided in the homework files. This program contains a method which is designed to read in a number from the user and print it to the screen. The parameter to the method specifies whether the number might contain decimals. There are two helper methods- one that reads in an integer and one that reads in a double. Run the program. As written, it works as long as the user enters what they are supposed...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are...
Go to the Files section and download the AFE_Test file from the Datasets folder. We are interested in a one­tail test described in the following fashion: Ho: u < or = to 200 CFM; H1: u > 200 CFM. At 5% significance level, we can reject the null hypothesis given the sample information in AFE_Test1. we can reject the null hypothesis given the sample information in AFE_Test2. we cannot reject the null hypothesis. we can reject the null hypothesis given...
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp"...
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp" file, and a "main.cpp" file. C++ Please! This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings...
C++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h>...
C++ and Java Zoo File Manager I need to complete these files: #include <iostream> #include <jni.h> using namespace std; void GenerateData() //DO NOT TOUCH CODE IN THIS METHOD { JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine) JNIEnv *env; // Pointer to native interface //================== prepare loading of Java VM ============================ JavaVMInitArgs vm_args; // Initialization arguments JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options options[0].optionString = (char*) "-Djava.class.path="; // where to find java .class vm_args.version = JNI_VERSION_1_6;...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files...
This assignment uses a combination of classes and arrays. Instructions: 1) download the 3 program files into a new C++ project.    NOTE: if your IDE does not allow you to create projects - or you are not sure how to do it - then you may combine the two cpp files into a single file. 2) Complete the program by adding code the the class methods. You may want to study the existing code first to get a feel...
You will need the information in this file to complete the two lab sequence This is...
You will need the information in this file to complete the two lab sequence This is the file that contains the report page for Lab 13-Taste, part 1 There are two things for you to turn in Enter your taste data on the following page. You should all be able to edit and save the page. Lab #13 - Taste Data W18 Taste Background Background Introduction Taste is the least understood of all the senses. (PBS, 2000) Taste has been...
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT