Answers to Practice Final Exam -- Spring 18 Part A: Multiple Choice 1. b 2. c 3. a 4. b 5. c 6. d 7. b 8. c 9. c 10. d 11. c 12. b 13. d 14. a 15. d Part B: Find the Errors Corrected source code: # Source code file bridgehand.py from card import Card from deck import Deck class BridgeHand(Deck): def __init__(self, the_cards): self._cards = [ ] for card in the_cards: self._cards.append(card) # To value a bridge hand, add points for each # honor card, counting each Ace (rank 14) as 4 points, # each King (rank 13) as 3 points, each Queen (rank 12) # as 2 points, and each Jack (rank 11) as 1 point. # Use the dictionary d to do this. def point_count( ): total_point_count = 0 d = {14: 4, 13: 3, 12: 2, 11: 1} for card in self._cards: if card in d: total_point_count += d[card.rank] return total_point_count Part C: Predict the output. The definition of the Book class is missing from the Practice Final. Here it is: class Book: def __init__(self, the_author, the_title): self.author = the_author self.title = the_title def __str__(self): return f"{self.author}\n{self.title}\n" Here is the output; War and Peace Tolstoy Catcher in the Rye Salinger 813.54 False True False Part D: Construct Unit Test File Unit test file: from book import Book from librarybook import LibraryBook import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): b1 = Book("Tolstoy", "War and Peace") self.assertEqual("Tolstoy\nWar and Peace\n", str(b1)) self.assertEqual("Tolstoy", b1.author) self.assertEqual("War and Peace", b1.title) def test_2(self): b2 = LibraryBook("Salinger", "Catcher in the Rye", 813.54) self.assertEqual( "Salinger\nCatcher in the Rye\n813.54\nFalse\n", str(b2)) self.assertEqual(813.54, b2.catalog_number) self.assertEqual(False, b2.checked_out( )) b2.check_out( ) self.assertEqual(True, b2.checked_out( )) b2.check_in( ) self.assertEqual(False, b2.checked_out( )) if __name__ == '__main__': unittest.main( ) Part E: Correct the errors There are the corrected files: public class Card { public int rank; public String suit; public Card(int theRank, String theSuit) { rank = theRank; suit = theSuit; } public String color( ) { // Use equals method to compare String // objects instead of ==. || means or in Java. if (suit.equals("C") || suit.equals("S")) { return "black"; } else { return "red"; } } // Represent card as a string. // The symbols list entries "0" and "1" are dummy entries. public String toString( ) { String[ ] symbols = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; return symbols[rank] + suit; } } public class TestCard { public static void main(String[ ] args) { Card c1 = new Card(12, "S"); System.out.println(c1.toString( )); System.out.println(c1.rank + " " + c1.suit + " " + c1.color( )); System.out.println( ); Card c2 = new Card(14, "H"); System.out.println(c2); System.out.println(c2.rank + " " + c2.suit + " " + c2.color( )); } }