A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ElleCullen

  • one year ago

Homework help asap please. Elevens lab, activity 5 buggy 5. more details in comments

  • This Question is Open
  1. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Now, examine the Buggy5 folder. This folder contains a Deck.java file with multiple errors. Use DeckTester to help you find the errors. Correct each error until the Deck class has passed all of its tests. Note that you may receive a runtime error other than AssertionError when running DeckTester. If so, you may find it helpful to switch the order of 1-card deck and 2-card deck tests as follows: public static void main(String[] args) { test2CardDeck(); // order swapped test1CardDeck(); // order swapped testShuffle(); System.out.println("All tests passed!"); }

  2. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I need help with this part: Correct each error until the Deck class has passed all of its tests. Here is the code for DeckTester: /** * This is a class that tests the Deck class using assert statements. */ public class DeckTester { /** * The main method in this class checks the Deck operations for consistency * using assert statements. * @param args is not used. */ public static void main(String[] args) { test1CardDeck(); test2CardDeck(); testShuffle(); System.out.println("All tests passed!"); } /** * Create a 1-card Deck and test the Deck methods with it. */ private static void test1CardDeck() { String[] r1 = {"ace"}; String[] s1 = {"spades"}; int[] v1 = {1}; Deck d = new Deck(r1, s1, v1); testOneCard(d, new Card("ace", "spades", 1)); testEmpty(d); d.shuffle(); testOneCard(d, new Card("ace", "spades", 1)); testEmpty(d); } /** * Create a 2-card Deck and test the Deck methods with it. */ private static void test2CardDeck() { String[] r2 = {"ace", "2"}; String[] s2 = {"hearts"}; int[] v2 = {1, 2}; Deck d = new Deck(r2, s2, v2); assert d.size() == 2 : "Initial size is " + d.size() + ". It should be 2."; assert !d.isEmpty() : "Initial deck is empty."; boolean aceIsFirst, twoIsFirst; Card c = d.deal(); assert c != null : "1st card dealt is null."; aceIsFirst = c.matches(new Card("ace", "hearts", 1)); twoIsFirst = c.matches(new Card("2", "hearts", 2)); assert (aceIsFirst || twoIsFirst) : "1st card dealt is " + c + ". It is not the one of the two correct cards."; assert d.size() == 1 : "Size after one deal is " + d.size() + ". It should be 1."; assert !d.isEmpty() : "Deck is empty after one deal."; c = d.deal(); assert c != null : "2nd card dealt is null."; if (aceIsFirst) { assert c.matches(new Card("2", "hearts", 2)) : "2nd card dealt is " + c + ". It is not one of the two correct cards."; } else { assert c.matches(new Card("ace", "hearts", 1)) : "2nd card dealt is " + c + ". It is not one of the two correct cards."; } testEmpty(d); d.shuffle(); c = d.deal(); assert c != null : "1st card dealt after shuffle is null."; aceIsFirst = c.matches(new Card("ace", "hearts", 1)); twoIsFirst = c.matches(new Card("2", "hearts", 2)); assert (aceIsFirst || twoIsFirst) : "1st card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; assert d.size() == 1 : "Size is " + d.size() + "after shuffle and one deal. It should be 1."; assert !d.isEmpty() : "Deck is empty after shuffle and one deal."; c = d.deal(); assert c != null : "2nd card dealt after shuffle is null."; if (aceIsFirst) { assert c.matches(new Card("2", "hearts", 2)) : "2nd card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; } else { assert c.matches(new Card("ace", "hearts", 1)) : "2nd card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; } testEmpty(d); } /** * Check that the given deck is empty. * @param d is a deck that should be empty. */ private static void testEmpty(Deck d) { assert d.size() == 0 : "Size for an empty deck is " + d.size() + ". It should be 0."; assert d.isEmpty() : "isEmpty is false for an empty deck."; Card c = d.deal(); assert c == null : "Dealt card is " + c + ". It should be null for an empty deck."; } /** * Check that the given deck contains the given card and nothing else. * @param d is a deck that should contain a single card. * @param intended is the card that the one-card deck should contain. */ private static void testOneCard(Deck d, Card intended) { assert d.size() == 1 : "Size is " + d.size() + ". It should be 1 for a 1-card deck."; assert !d.isEmpty() : "isEmpty true for a 1-card deck."; Card c = d.deal(); assert c != null : "1st dealt card is null for a 1-card deck."; assert c.matches(intended) : "1st card dealt for a 1-card deck is " + c + ". It should be " + intended + "."; } /** * Check that two decks constructed in succession aren't the same. */ private static void testShuffle() { String[] r = {"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"}; String[] s = {"spades", "hearts", "diamonds", "clubs"}; int[] v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; Deck d1 = new Deck(r, s, v); Deck d2 = new Deck(r, s, v); assert d1 != d2 : "Decks d1 and d2 are the same physical object."; assert d1.size() == d2.size() : "Deck d1 size of " + d1.size() + " does not match deck d2 size of " + d2.size() + "."; boolean allMatch = true; while (!d1.isEmpty()) { Card c1 = d1.deal(); Card c2 = d2.deal(); if (!c1.matches(c2)) { allMatch = false; } } assert !allMatch : "The sequence of cards in d1 and d2 are identical."; } }

  3. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Code for Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including * initialize, shuffle, deal, and check if empty. */ public class Deck { /** * cards contains all the cards in the deck. */ private List<Card> cards; /** * size is the number of not-yet-dealt cards. * Cards are dealt from the top (highest index) down. * The next card to be dealt is at size - 1. */ private int size; /** * Creates a new <code>Deck</code> instance.<BR> * It pairs each element of ranks with each element of suits, * and produces one of the corresponding card. * @param ranks is an array containing all of the card ranks. * @param suits is an array containing all of the card suits. * @param values is an array containing all of the card point values. */ public Deck(String[] ranks, String[] suits, int[] values) { cards = new ArrayList<Card>(); for (int j = 0; j < ranks.length; j++) { for (String suitString : suits) { cards.add(new Card(ranks[j], suitString, values[j])); } } size = cards.size(); shuffle(); } /** * Determines if this deck is empty (no undealt cards). * @return true if this deck is empty, false otherwise. */ public boolean isEmpty() { return size == 0; } /** * Accesses the number of undealt cards in this deck. * @return the number of undealt cards in this deck. */ public int size() { return size; } /** * Randomly permute the given collection of cards * and reset the size to represent the entire deck. */ public void shuffle() { /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */ } /** * Deals a card from this deck. * @return the card just dealt, or null if all the cards have been * previously dealt. */ public Card deal() { if (isEmpty()) { return null; } size--; Card c = cards.get(size); return c; } /** * Generates and returns a string representation of this deck. * @return a string representation of this deck. */ @Override public String toString() { String rtn = "size = " + size + "\nUndealt cards: \n"; for (int k = size - 1; k >= 0; k--) { rtn = rtn + cards.get(k); if (k != 0) { rtn = rtn + ", "; } if ((size - k) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + "\n"; } } rtn = rtn + "\nDealt cards: \n"; for (int k = cards.size() - 1; k >= size; k--) { rtn = rtn + cards.get(k); if (k != size) { rtn = rtn + ", "; } if ((k - cards.size()) % 2 == 0) { // Insert carriage returns so entire deck is visible on console. rtn = rtn + "\n"; } } rtn = rtn + "\n"; return rtn; } }

  4. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I altered the DeckTester a little: /** * This is a class that tests the Deck class using assert statements. */ public class DeckTester { /** * The main method in this class checks the Deck operations for consistency * using assert statements. * @param args is not used. */ public static void main(String[] args) { test2CardDeck(); test1CardDeck(); testShuffle(); System.out.println("All tests passed!"); } /** * Create a 1-card Deck and test the Deck methods with it. */ private static void test1CardDeck() { String[] r1 = {"ace"}; String[] s1 = {"spades"}; int[] v1 = {1}; Deck d = new Deck(r1, s1, v1); testOneCard(d, new Card("ace", "spades", 1)); testEmpty(d); d.shuffle(); testOneCard(d, new Card("ace", "spades", 1)); testEmpty(d); } /** * Create a 2-card Deck and test the Deck methods with it. */ private static void test2CardDeck() { String[] r2 = {"ace", "2"}; String[] s2 = {"hearts"}; int[] v2 = {1, 2}; Deck d = new Deck(r2, s2, v2); assert d.size() == 2 : "Initial size is " + d.size() + ". It should be 2."; assert !d.isEmpty() : "Initial deck is empty."; boolean aceIsFirst, twoIsFirst; Card c = d.deal(); assert c != null : "1st card dealt is null."; aceIsFirst = c.matches(new Card("ace", "hearts", 1)); twoIsFirst = c.matches(new Card("2", "hearts", 2)); assert (aceIsFirst || twoIsFirst) : "1st card dealt is " + c + ". It is not the one of the two correct cards."; assert d.size() == 1 : "Size after one deal is " + d.size() + ". It should be 1."; assert !d.isEmpty() : "Deck is empty after one deal."; c = d.deal(); assert c != null : "2nd card dealt is null."; if (aceIsFirst) { assert c.matches(new Card("2", "hearts", 2)) : "2nd card dealt is " + c + ". It is not one of the two correct cards."; } else { assert c.matches(new Card("ace", "hearts", 1)) : "2nd card dealt is " + c + ". It is not one of the two correct cards."; } testEmpty(d); d.shuffle(); c = d.deal(); assert c != null : "1st card dealt after shuffle is null."; aceIsFirst = c.matches(new Card("ace", "hearts", 1)); twoIsFirst = c.matches(new Card("2", "hearts", 2)); assert (aceIsFirst || twoIsFirst) : "1st card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; assert d.size() == 1 : "Size is " + d.size() + "after shuffle and one deal. It should be 1."; assert !d.isEmpty() : "Deck is empty after shuffle and one deal."; c = d.deal(); assert c != null : "2nd card dealt after shuffle is null."; if (aceIsFirst) { assert c.matches(new Card("2", "hearts", 2)) : "2nd card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; } else { assert c.matches(new Card("ace", "hearts", 1)) : "2nd card dealt after shuffle is " + c + ". It is not the one of the two correct cards."; } testEmpty(d); } /** * Check that the given deck is empty. * @param d is a deck that should be empty. */ private static void testEmpty(Deck d) { assert d.size() == 0 : "Size for an empty deck is " + d.size() + ". It should be 0."; assert d.isEmpty() : "isEmpty is false for an empty deck."; Card c = d.deal(); assert c == null : "Dealt card is " + c + ". It should be null for an empty deck."; } /** * Check that the given deck contains the given card and nothing else. * @param d is a deck that should contain a single card. * @param intended is the card that the one-card deck should contain. */ private static void testOneCard(Deck d, Card intended) { assert d.size() == 1 : "Size is " + d.size() + ". It should be 1 for a 1-card deck."; assert !d.isEmpty() : "isEmpty true for a 1-card deck."; Card c = d.deal(); assert c != null : "1st dealt card is null for a 1-card deck."; assert c.matches(intended) : "1st card dealt for a 1-card deck is " + c + ". It should be " + intended + "."; } /** * Check that two decks constructed in succession aren't the same. */ private static void testShuffle() { String[] r = {"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"}; String[] s = {"spades", "hearts", "diamonds", "clubs"}; int[] v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; Deck d1 = new Deck(r, s, v); Deck d2 = new Deck(r, s, v); assert d1 != d2 : "Decks d1 and d2 are the same physical object."; assert d1.size() == d2.size() : "Deck d1 size of " + d1.size() + " does not match deck d2 size of " + d2.size() + "."; boolean allMatch = true; while (!d1.isEmpty()) { Card c1 = d1.deal(); Card c2 = d2.deal(); if (!c1.matches(c2)) { allMatch = false; } } assert !allMatch : "The sequence of cards in d1 and d2 are identical."; } } I keep getting an error when I run this. Error: java.lang.AssertionError: 1st card dealt after shuffle is null.

  5. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @Compassionate

  6. ElleCullen
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @Luigi0210

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.