Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE HELP! Assignment 5 Shuffle: Main Class Ok, so I have the Deck class submi

ID: 3808937 • Letter: P

Question

PLEASE HELP! Assignment 5 Shuffle: Main Class

Ok, so I have the Deck class submitted and I have a 100% on that. But for my Main class, I have a 10/14 with four errors. They all regard adding the points together and printing them out. This is my error statement:


Your code has been evaluated against a set of test data.
You had 10 out of 14 tests pass correctly.
Your score is 71%.

The tests that failed were:
Hand 1:
   Incorrect: Total points
Hand 2:
   Incorrect: Total points
Incorrect Number of Cards Dealt:
   Incorrect: More than five cards
dealt to either hand
Game Results
   Incorrect: Final score

These were the original instructions:

Assignment 5 - Shuffle

In this lab you will simulate the playing of a simple card game. Start by downloading two starter files: Card.java and Deck.java. The Card class should not be changed. You will add one method to the Deck class.

This assignment should be submitted in two classes, each with a separate code runner box. The first class, Deck, will be a modification of the class we have provided. You will need to implement the shuffleDeck method in order for this class to be accepted.

The second class, Main, will use the Card class and your modified Deck class to create a shuffled Deck object and deal the two hands. The hands should be dealt in alternating order, starting with the first hand. Each hand should have five cards. As the cards are dealt into each hand they should be removed from the deck.

For example, each hand is shown for the following Deck.

Seven of spades <- Index 0, top card

Queen of spades <- Index 1, etc.

Ten    of spades

Eight of spades

Three of spades

King   of hearts

Queen of hearts

Jack   of clubs

Four   of clubs

Eight of clubs

King   of diamonds

Seven of hearts

Hand 1:

Hand 2:

Seven of spades

Queen of spades

Ten of spades

Eight of spades

Three of spades

King of hearts

Queen of hearts

Jack of clubs

Four of clubs

Eight of clubs

Also, all of these cards should be removed from the deck.

After dealing the hand, Main should use the point value of each card to calculate the total point value of each hand. The hand with the highest point value wins. In the case of a draw, the second hand wins. In this game ace = 1, jack = 11, queen = 12, and king = 13. In the deck the card in the first position (index 0) is the top of the deck.

Lastly, Main will declare the winning hand. See the following sample run of Main for the exact format that will be expected by the code runner.

Sample Run of Main:

Hand 1 (total points 22)

Three of clubs (point value = 3)

Two of clubs (point value = 2)

Six of hearts (point value = 6)

Ten of hearts (point value = 10)

Ace of spades (point value = 1)

Hand 2 (total points 27)

Four of spades (point value = 4)

Ten of clubs (point value = 10)

Three of diamonds (point value = 3)

Eight of diamonds (point value = 8)

Two of hearts (point value = 2)

Hand 2 wins!

And this is my code (which is passing 10/14 tests) that I just need to add the point value parts to:

import java.util.ArrayList;

public class Main {

  private static ArrayList hand1 = new ArrayList();

  private static ArrayList hand2 = new ArrayList();

  private static Deck deck = new Deck();

  public static void main(String[] args){

      for (int i = 0; i<5; i++){

          hand1.add(deck.getTopCard());

          hand2.add(deck.getTopCard());

      }

      int hand1Value = 0;

      for (int i = 0; i

          Card cC = hand1.get(i);

          hand1Value += cC.pointValue();

      }

      int hand2Value = 0;

      for (int i = 0; i

          Card cC = hand2.get(i);

          hand2Value += cC.pointValue();

      }

      System.out.println("Hand 1: total points " + hand1Value);

      for (int i = 0; i

          System.out.println(hand1.get(i));

      }

      System.out.println(" Hand 2: total points " + hand2Value);

      for (int i = 0; i

          System.out.println(hand2.get(i));

      }

     if(hand1Value

          System.out.println("Hand 2 wins");

      }else if(hand2Value

          System.out.println("Hand 1 wins");

      }else

          System.out.println("Tie");

  }


}

Please help me. Thanks!

Below are the two starter files: Card and Deck

Card:

*
* AP CS MOOC
* Term 2 - Assignment 5: Shuffle
* A class which represents a single playing card, Card. Use this class to test the methods in your Deck and Main classes.
*/

public class Card {

/**
* String value that holds the suit of the card
*/
private String suit;

/**
* String value that holds the rank of the card
*/
private String rank;

/**
* int value that holds the point value.
*/
private int pointValue;


/**
* Creates a new <code>Card</code> instance.
*
* @param cardRank a <code>String</code> value
* containing the rank of the card
* @param cardSuit a <code>String</code> value
* containing the suit of the card
* @param cardPointValue an <code>int</code> value
* containing the point value of the card
*/
public Card(String cardRank, String cardSuit, int cardPointValue) {
//initializes a new Card with the given rank, suit, and point value
rank = cardRank;
suit = cardSuit;
pointValue = cardPointValue;
  
}


/**
* Accesses this <code>Card's</code> suit.
* @return this <code>Card's</code> suit.
*/
public String suit() {
return suit;
}

/**
* Accesses this <code>Card's</code> rank.
* @return this <code>Card's</code> rank.
*/
public String rank() {
return rank;
}

/**
* Accesses this <code>Card's</code> point value.
* @return this <code>Card's</code> point value.
*/
public int pointValue() {
return pointValue;
}

/** Compare this card with the argument.
* @param otherCard the other card to compare to this
* @return true if the rank, suit, and point value of this card
* are equal to those of the argument;
* false otherwise.
*/
public boolean matches(Card otherCard) {
return otherCard.suit().equals(this.suit())
&& otherCard.rank().equals(this.rank())
&& otherCard.pointValue() == this.pointValue();
}

/**
* Converts the rank, suit, and point value into a string in the format
* "[Rank] of [Suit] (point value = [PointValue])".
* This provides a useful way of printing the contents
* of a <code>Deck</code> in an easily readable format or performing
* other similar functions.
*
* @return a <code>String</code> containing the rank, suit,
* and point value of the card.
*/
@Override
public String toString() {
return rank + " of " + suit + " (point value = " + pointValue + ")";
  
}
}

Deck:

/*
* AP CS MOOC
* Term 2 - Assignment 5: Shuffle
* A class which represents a Deck of cards. For this assignment, you will need to implement the method shuffleDeck, which appears at the bottom of this class.
*/

import java.util.ArrayList;

public class Deck
{
private ArrayList<Card> deck;


public Deck ()
{
deck = new ArrayList <Card>();
deck = initDeck();
deck = shuffleDeck();
System.out.println(deck);

}

public String toString ()
{
String temp = "";
  
for (Card c: deck)
{
temp += c.toString() + " ";
}
  
return temp;
}

public Card getTopCard ()
{
Card c = deck.get(0);
deck.remove(0);


return c;
}

public static ArrayList <Card> initDeck ()
{
ArrayList <String> ranks = new ArrayList <String> ();

ranks.add ("Ace");

ranks.add ("Two");
ranks.add ("Three");
ranks.add ("Four");
ranks.add ("Five");
ranks.add ("Six");
ranks.add ("Seven");
ranks.add ("Eight");
ranks.add ("Nine");
ranks.add ("Ten");
ranks.add ("Jack");
ranks.add ("Queen");
ranks.add ("King");

ArrayList <String> suites = new ArrayList <String> ();
suites.add("clubs");
suites.add("diamonds");
suites.add("hearts");
suites.add("spades");

ArrayList <Card> deck = new ArrayList <Card> ();
for (String s : suites)
{
int p = 1;
for (String r: ranks)
{
Card c = new Card (r, s, p);
p++;
deck.add(c);
}
}
return deck;
}

//SHUFFLE ****************************


public ArrayList <Card> shuffleDeck ()
{
  
ArrayList <Card> t = new ArrayList <Card> ();
  
//your code here
return t;
  
  
  
  
}

}

Here is also my version of Deck which I had to update and submit:

import java.util.ArrayList;

public class Deck

{

  private ArrayList<Card> deck;

  public Deck ()

  {

      deck = new ArrayList <Card>();

      deck = initialDeck();

      deck = shuffleDeck();

      System.out.println(deck);

  }

  public String toString ()

  {

      String temp = "";

      for (Card c: deck)

      {

          temp += c.toString() + " ";

      }

      return temp;

  }

  public Card getTopCard ()

  {

      Card c = deck.get(0);

      deck.remove(0);

      return c;

  }

  public static ArrayList <Card> initialDeck ()

  {

      ArrayList <String> cardRank = new ArrayList <String> ();

      cardRank.add ("ace");

      cardRank.add ("two");

      cardRank.add ("three");

      cardRank.add ("four");

      cardRank.add ("five");

      cardRank.add ("six");

      cardRank.add ("seven");

      cardRank.add ("eight");

      cardRank.add ("nine");

      cardRank.add ("ten");

      cardRank.add ("jack");

      cardRank.add ("queen");

      cardRank.add ("king");

      ArrayList <String> cardSuit = new ArrayList <String> ();

      cardSuit.add("clubs");

      cardSuit.add("diamonds");

      cardSuit.add("hearts");

      cardSuit.add("spades");

      ArrayList <Card> deck = new ArrayList <Card> ();

      for (String s : cardSuit)

      {

          int p = 1;

          for (String r: cardRank)

          {

              Card c = new Card (r, s, p);

              p++;

              deck.add(c);

          }

      }

      return deck;

  }

  public ArrayList <Card> shuffleDeck ()

  {

      ArrayList <Card> t = new ArrayList <Card> ();

      int[] ind = new int[deck.size()];

      for (int i = 0; i< ind.length; i++){

          ind[i] = i;

      }

      for (int i = 0; i<deck.size(); i++){

          int r_I = 0;

          boolean isValid = false;

          while(!isValid){

              r_I = (int)(Math.random()*deck.size());

              for(int n = 0; n<ind.length; n++){

                  if(ind[n] == r_I)

                      isValid = true;

              }

          }

          t.add(i, deck.get(r_I));

          ind[r_I] = -1;

      }

      return t;

  }

}

Hand 1:

Hand 2:

Seven of spades

Queen of spades

Ten of spades

Eight of spades

Three of spades

King of hearts

Queen of hearts

Jack of clubs

Four of clubs

Eight of clubs

Explanation / Answer

CODE:

Deck.java
import java.util.ArrayList;

public class Deck
{
private ArrayList<Card> deck;


public Deck ()
{
deck = new ArrayList <Card>();
deck = initDeck();
deck = shuffleDeck();
System.out.println(deck);

}

public String toString ()
{
String temp = "";
  
for (Card c: deck)
{
temp += c.toString() + " ";
}
  
return temp;
}

public Card getTopCard ()
{
Card c = deck.get(0);
deck.remove(0);


return c;
}

public static ArrayList <Card> initDeck ()
{
ArrayList <String> ranks = new ArrayList <String> ();

ranks.add ("ace");

ranks.add ("two");
ranks.add ("three");
ranks.add ("four");
ranks.add ("five");
ranks.add ("six");
ranks.add ("seven");
ranks.add ("eight");
ranks.add ("nine");
ranks.add ("ten");
ranks.add ("jack");
ranks.add ("queen");
ranks.add ("king");

ArrayList <String> suites = new ArrayList <String> ();
suites.add("clubs");
suites.add("diamonds");
suites.add("hearts");
suites.add("spades");

ArrayList <Card> deck = new ArrayList <Card> ();
for (String s : suites)
{
int p = 1;
for (String r: ranks)
{
Card c = new Card (r, s, p);
p++;
deck.add(c);
}
}
return deck;
}
public ArrayList <Card> shuffleDeck ()
{
  
   ArrayList <Card> t = new ArrayList <Card> ();
        int[] indexes = new int[deck.size()];
        for (int i = 0; i< indexes.length; i++){
            indexes[i] = i;
        }
        for (int i = 0; i<deck.size(); i++){
            int randIndex = 0;
            boolean isvalid = false;
            while(!isvalid){
                randIndex = (int)(Math.random()*deck.size());

                for(int n = 0; n<indexes.length; n++){
                    if(indexes[n] == randIndex)
                        isvalid = true;
                }
            }
            t.add(i, deck.get(randIndex));
            indexes[randIndex] = -1;
        }

        return t;
}
}

Card.java
public class Card {

/**
* String value that holds the suit of the card
*/
private String suit;

/**
* String value that holds the rank of the card
*/
private String rank;

/**
* int value that holds the point value.
*/
private int pointValue;


/**
* Creates a new <code>Card</code> instance.
*
* @param cardRank a <code>String</code> value
* containing the rank of the card
* @param cardSuit a <code>String</code> value
* containing the suit of the card
* @param cardPointValue an <code>int</code> value
* containing the point value of the card
*/
public Card(String cardRank, String cardSuit, int cardPointValue) {
//initializes a new Card with the given rank, suit, and point value
rank = cardRank;
suit = cardSuit;
pointValue = cardPointValue;
  
}


/**
* Accesses this <code>Card's</code> suit.
* @return this <code>Card's</code> suit.
*/
public String suit() {
return suit;
}

/**
* Accesses this <code>Card's</code> rank.
* @return this <code>Card's</code> rank.
*/
public String rank() {
return rank;
}

/**
* Accesses this <code>Card's</code> point value.
* @return this <code>Card's</code> point value.
*/
public int pointValue() {
return pointValue;
}

/** Compare this card with the argument.
* @param otherCard the other card to compare to this
* @return true if the rank, suit, and point value of this card
* are equal to those of the argument;
* false otherwise.
*/
public boolean matches(Card otherCard) {
return otherCard.suit().equals(this.suit())
&& otherCard.rank().equals(this.rank())
&& otherCard.pointValue() == this.pointValue();
}

/**
* Converts the rank, suit, and point value into a string in the format
* "[Rank] of [Suit] (point value = [PointValue])".
* This provides a useful way of printing the contents
* of a <code>Deck</code> in an easily readable format or performing
* other similar functions.
*
* @return a <code>String</code> containing the rank, suit,
* and point value of the card.
*/
@Override
public String toString() {
return rank + " of " + suit + " (point value = " + pointValue + ")";
  
}
}

Main.java
import java.util.ArrayList;
public class Main {

   /**
   * @param args
*/
   private static ArrayList <Card> ArrayList<Card>();
   private static ArrayList <Card> two = new ArrayList<Card>();
   private static Deck deck = new Deck();
   public static void main(String[] args) {
       deck.shuffleDeck();
       one.add(deck.getTopCard());
       two.add(deck.getTopCard());
       one.add(deck.getTopCard());
       two.add(deck.getTopCard());
       one.add(deck.getTopCard());
       two.add(deck.getTopCard());
       one.add(deck.getTopCard());
       two.add(deck.getTopCard());
       one.add(deck.getTopCard());
       two.add(deck.getTopCard());
      
       int hand1value = 0;
       for (int i = 0; i < one.size(); i++)
       {
           Card current = one.get(i);
           hand1value += current.pointValue();
       }
       int hand2value = 0;
       for (int i = 0; i < two.size(); i++)
       {
           Card current = two.get(i);
           hand2value += current.pointValue();
       }
      
       System.out.println("Hand 1: total points " + hand1value);
       System.out.println(one.get(0));
       System.out.println(one.get(1));
       System.out.println(one.get(2));
       System.out.println(one.get(3));
       System.out.println(one.get(4));
       System.out.println(" Hand 2: total points " + hand2value);
       System.out.println(two.get(0));
       System.out.println(two.get(1));
       System.out.println(two.get(2));
       System.out.println(two.get(3));
       System.out.println(two.get(4));
      
       if (hand1value > hand2value)
           System.out.println(" Hand 1 wins");
       else if (hand2value > hand1value)
           System.out.println(" Hand 2 wins");
       else
           System.out.println(" Tie");      
}
}

OUTPUT:
[jack of clubs (point value = 11), jack of diamonds (point value = 11), ten of spades (point value = 10), seven of spades (point value = 7),
ace of clubs (point value = 1), nine of clubs (point value = 9), queen of hearts (point value = 12), four of hearts (point value = 4),
jack of hearts (point value = 11), two of clubs (point value = 2), king of diamonds (point value = 13), ace of hearts (point value = 1),
six of spades (point value = 6), ten of clubs (point value = 10), eight of hearts (point value = 8), queen of diamonds (point value = 12),
eight of diamonds (point value = 8), queen of spades (point value = 12), six of diamonds (point value = 6), nine of diamonds (point value = 9),
five of clubs (point value = 5), five of diamonds (point value = 5), nine of hearts (point value = 9), seven of hearts (point value = 7),
queen of clubs (point value = 12), nine of spades (point value = 9), king of hearts (point value = 13), king of spades (point value = 13),
two of diamonds (point value = 2), eight of spades (point value = 8), three of hearts (point value = 3), two of hearts (point value = 2),
six of hearts (point value = 6), king of clubs (point value = 13), jack of spades (point value = 11), five of spades (point value = 5),
ace of spades (point value = 1), four of diamonds (point value = 4), six of clubs (point value = 6), ten of hearts (point value = 10),
ten of diamonds (point value = 10), eight of clubs (point value = 8), seven of clubs (point value = 7), three of diamonds (point value = 3),
seven of diamonds (point value = 7), four of spades (point value = 4), four of clubs (point value = 4), three of clubs (point value = 3),
five of hearts (point value = 5), ace of diamonds (point value = 1), three of spades (point value = 3), two of spades (point value = 2)]
Hand 1: total points 45
jack of clubs (point value = 11)
ten of spades (point value = 10)
ace of clubs (point value = 1)
queen of hearts (point value = 12)
jack of hearts (point value = 11)
Hand 2: total points 33
jack of diamonds (point value = 11)
seven of spades (point value = 7)
nine of clubs (point value = 9)
four of hearts (point value = 4)
two of clubs (point value = 2)
Hand 1 win

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote