![]() Princeton University |
Computer Science 441 |
public void errormethod(){ A a1 = new A(); A a2 = new A(); B b = new B(); a1 = b; b = a2; }Which of these assignments is always guaranteed to be type-safe? Give an example of code which would break if the other assignment were allowed. (Hint: Consider sending messages. )
b. Suppose Java allowed call-by-reference as a parameter passing mode. Suppose we have classes A and B as above in which B extends A. Give an example in which it could break the type system if an actual parameter of class B were passed in for a formal call-by-reference parameter of class A.
In this problem you are to create classes to implement a deck of playing cards. The assignment has two parts. In part 1, you implement a Deck class using the CardInterface interface and Card class provided. The Deck class should provide the following methods
public void shuffle(); // Post: Randomly shuffles the cards in the deck public void reInit(); // Post: Refill the deck in order: 2 of clubs .. ace of spades public CardInterface[] deal(int n); // Pre: There are still n cards in the deck // Post: The "top" n cards are removed from deck and returnedOf course, there should also be a constructor and any other necessary methods, including a
main()
method which tests your Deck
class.
The deck should be implemented as an array of CardInterface
.
The second part of the assignment is to provide a new implementation of the Card
class,
and then use it in your Deck
class.
The new card class, called OtherCard
, should implement a card as a number
between 0 and 51, where 0-12 represents the clubs (2..Ace),
13-25 represent the diamonds, and so on.
This will require rewriting of the bodies of several of the methods in the
Card class.
The easiest way to do this part of the assignment is to copy my Card.java
file and edit it. Using OtherCard
in place of Card
should require only minor
modifications to the Deck
class if you've been using CardInterface for the types
of variable and parameters.
Deck
class should include a main method which tests your Deck
thoroughly.
For example, shuffle, deal some hands, re-initialize, shuffle again, etc.,
printing the deck contents after each operation.