Princeton University
Computer Science Dept.

Computer Science 441
Programming Languages
Fall 1998

Assignment 7
Due 11/24/98


  1. a. Suppose class B extends class A in Java by adding at least one new method m. Suppose we include the following code in a method of some other class C:
       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.

  2. Java programming:

    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 returned 
    
    Of 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.

    Naming Conventions

    Classes requested for assignments should always be named as in the assignment. This is necessary for our testing of your work. Also, Java programmers tend to follow certain naming conventions. Constants are all upper-case, classes begin with upper-case, variable and method names begin with lower case, using upper-case for multi-word names. You should also follow these conventions.

    What to hand in

    We would like to you to turn in an electronic as well as paper copy of your final program (with the Deck of OtherCard). Again, the 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.

    Sample Program

    Here is a sample applet which demonstrates what each method should do. (It will display and execute only if your browser is Java 1.1 savvy. The netscape-4.5 browser on the CS dept suns does work! Otherwise you will simply get an error message.) Of course your program will be an application, rather than an applet, and need not use windows or buttons unless you are feeling especially ambitious!