Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. Each deck is given a list of cards from the professor's test harness. I'm having trouble with a drawAll method:
  2.  
  3. drawAll documentation:
  4. [QUOTE]Removes all Cards from this Deck and returns them as a new Deck. If this deck is empty, then this method returns an empty new Deck.[/QUOTE]
  5.  
  6. I puzzled through it with a friend, and the closest we got was
  7. [QUOTE]public Deck drawAll() {
  8. Deck newDeck = new DeckImp();
  9.  
  10. if (self.isEmpty() == false) {
  11. for(i=0; i<self.size; i++) {
  12. newDeck.add(self[i]);
  13. }
  14. self.removeAll();
  15. }
  16. return newDeck;
  17. }[/QUOTE]
  18.  
  19. Which didn't work. So the professor gave me some hints.
  20.  
  21. 1. Construct a new empty deck
  22. 2. Pass elements from the list the professor created into the new deck
  23. 3. Call clear() to remove cards from the first deck, not removeAll()
  24.  
  25. As far as two goes, he first said to use a loop to get and remove each item, then said sublists would be easier (I don't think we've ever gone over sublists, so it's fun research time).
  26.  
  27. Specific test that is failing:
  28. public void testDeck9() {
  29. final List<Card> cardList = new ArrayList<>();
  30. final Card c1 = new Card(Suit.SPADE, Rank.TWO);
  31. final Card c2 = new Card(Suit.SPADE, Rank.THREE);
  32. final Card c3 = new Card(Suit.SPADE, Rank.SIX);
  33. final Card c4 = new Card(Suit.SPADE, Rank.SEVEN);
  34. final Card c5 = new Card(Suit.SPADE, Rank.KING);
  35. cardList.add(c1);
  36. cardList.add(c2);
  37. cardList.add(c3);
  38. cardList.add(c4);
  39. cardList.add(c5);
  40.  
  41. Deck deck1 = factory.createDeck(cardList);
  42. Deck deck2 = deck1.drawAll(); //this is failing
  43.  
  44. assert deck1.size() == 0;
  45. assert deck2.size() == 5;
  46. assert deck2.peekTop().equals(c1);
  47. assert deck2.peekBottom().equals(c5);
  48.  
  49. System.out.println("testDeck9 success.");
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement