Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 Card instance. * * @param cardRank a String value * containing the rank of the card * @param cardSuit a String value * containing the suit of the card * @param cardPointValue an int 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 Card's suit. * @return this Card's suit. */ public String suit() { return suit; }

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

/** * Accesses this Card's point value. * @return this Card's 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 Deck in an easily readable format or performing * other similar functions. * * @return a String 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 deck;

public Deck () { deck = new ArrayList (); 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 initDeck () { ArrayList ranks = new ArrayList (); 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 suites = new ArrayList (); suites.add("clubs"); suites.add("diamonds"); suites.add("hearts"); suites.add("spades"); ArrayList deck = new ArrayList (); 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 shuffleDeck () { ArrayList t = new ArrayList (); //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 deck;

public Deck ()

{

deck = new ArrayList ();

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 initialDeck ()

{

ArrayList cardRank = new ArrayList ();

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 cardSuit = new ArrayList ();

cardSuit.add("clubs");

cardSuit.add("diamonds");

cardSuit.add("hearts");

cardSuit.add("spades");

ArrayList deck = new ArrayList ();

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 shuffleDeck ()

{

ArrayList t = new ArrayList ();

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

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

ind[i] = i;

}

for (int i = 0; i

int r_I = 0;

boolean isValid = false;

while(!isValid){

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

for(int n = 0; n

if(ind[n] == r_I)

isValid = true;

}

}

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

ind[r_I] = -1;

}

return t;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL 10 High Performance Expert Techniques For Query Optimization High Availability And Efficient Database Maintenance

Authors: Ibrar Ahmed ,Gregory Smith ,Enrico Pirozzi

3rd Edition

1788474481, 978-1788474481

More Books

Students also viewed these Databases questions