CS56—Midterm Exam 2
E02, W11, Phill Conrad, UC Santa Barbara
Monday, 02/28/2011

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Circle one:       5pm           6pm     



Please write your name only on this page.
That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 75 minutes to complete the exam.

A hint for allocating your time—on your first pass through the exam:

If you do that, after you complete your first pass through the exam in 50 minutes, you'll still have 25 minutes to:


  1. (18 pts) READ THESE INSTRUCTIONS CAREFULLY—they apply to both question 1, and question 2 (on the next page)

    On a handout along with this exam, there is the contents of a file Book.java. Consider what will happen when this file is compiled and its main() class is run, and answer the questions below.

    Consider what will be true in memory at the point in time when the letter "A" is printed by this program.

    With that moment in mind, fill in the answer column for each row in the table with the name of a variable from the program.

    ONLY when the variable you write under "Variable Name" is an instance variable, in the right most column, also indicate that name of a variable in the program that points to an object in which this instance variable is contained, at the moment in the program when the letter "A" is printed.

    Two special cases:
    • If the situation described can NEVER HAPPEN in Java, write "IMPOSSIBLE"
    • If the situation CAN happen in Java, but there are no such variables in memory at the point in time indicated, write "NONE".


      Description Answer
    Variable Name
    Fill in this column ONLY
    if the variable in left answer column
    is an instance variable:

    name of a variable that points to the object in which this instance variable is contained.
    1 primitive variable
    on the stack
                                                   
    2 primitive variable
    on the heap
    3 primitive variable
    that is also an instance variable
    4 primitive variable
    that is a parameter to a method
       
    5 local primitive variable on stack    
    6 local primitive variable on heap    
  2. (18 pts) The instructions for this problem are the same as for problem 1, except all of these questions pertain to reference variables.


      Description Answer
    Variable Name
    Fill in this column ONLY
    if the variable in left answer column
    is an instance variable:

    name of a variable that points to the object in which this instance variable is contained.
    1 reference variable
    on the stack
    (the reference is on the stack,
    not necessarily the object referred to)
    2 reference variable
    on the heap
    (the reference is on the heap,
    not necessarily the object referred to)
    3 reference variable
    that is also an instance variable
       
    4 reference variable
    that is a parameter to a method
       
    5 reference variable
    pointing to object on the stack
    (i.e. the object pointed to is on the stack)
       
    6 reference variable
    pointing to object on the heap
    (i.e. the object pointed to is on the heap)
       
  3. Briefly answer the following two questions about "inner classes" in Java.


    1. (4 pts) What is an inner class, and how do you define one?











    2. (4 pts) Based on our reading in the textbook, and our programming experiences in lab07—describe a case where inner classes are used in practice.














  4. (5 pts) The Math class has only static methods, and is not meant to be instantiated. The designers of that class enforced this by making its constructor private. Another way to make Math be a class that you can't make an instance of would be to make it an abstract class (see Chapter 8). Although that would achieve the goal of making it so that Math can't be instantiated, there's something not quite right about that approach—what is it that doesn't quite make sense about making Math an abstract class?










  5. (12pts) Suppose there is a Java class that can sort items, but only if they implement an interface called Orderable. There is a separate handout that has information about the Orderable interface—you will need that information to answer this question.

    In lab05, you were asked to implement a class for ordinary playing cards.

    Below (and continued on the next page) is an example of that class with plenty of space for additional code to be added.

    Modify the class so that it implements the Comparable Orderable interface.


    Although the lines with spaces have line numbers, you do not need to line up your code with those numbers—use the space in whatever way you see fit.

    Your implementation of this interface should order cards from low rank to high rank within each suit, and the suits should be ordered in alphabetical order, with Clubs being the lowest, Diamonds next, Hearts, third, and Spades last.

    For example, if you have the cards 2S, TH, QS, 3H, JC, 7D, 7H, AS, the correct order is JC, 7D, 3H, 7H, TH, 2S, QS, AS.

    Hints:

    • It may help to define an additional helper method similar to the rankNum method for ranking suits.
    • Though it is not the only way to solve the problem, it may help to think of a card as a number in base 13 or base 16 where the rank is the ones place, and the suit is the 13s or 16s place. That leads to a straightforward conversion from cards to ordered integers—you could define a method for this conversion. That leads to a straightforward solution.
      But if that hint only confuses you, forget about it, and solve the problem with your own approach.
    Card.java
    1public class Card
    2{
    3 private char suit;
    4 private char rank;
    5
    6 /**
    7 Constructor
    8 @param suit C,H,D or S, for Clubs, Hearts, Diamonds, Spades
    9 @param rank one of A for ace, 2-9, or TJQK for ten, jack, queen, king
    10 */
    11
    12 public Card(char suit, char rank) {
    13 this.suit = suit;
    14 this.rank = rank;
    15 }
    16
    17 public char getSuit() { return suit; }
    18 public char getRank() { return rank; }
    19
    20 /**
    21 returns card formatted as string; e.g.
    22 "5H" for five of hearts, "3C" for "three of clubs" or "AS" for "Ace of Spades
    23 @return "rs" where r is rank and s is suit
    24 */
    25 public String toString() { return "" + rank + "" + suit; }

    Card.java listing continued from previous page

    26
    27 /**
    28 Return rank as an int; useful for orderings. Treats ace as high,
    29 so rank(T)=10, rank(J)=11, rank(Q)=12, rank(K)=13, rank(A)=14.
    30 @return rank of card as an int
    31 */
    32 public int rankNum() {
    33 switch (this.rank) {
    34 case '2': case '3': case '4': case '5':
    35 case '6': case '7': case '8': case '9':
    36 return Integer.parseInt(Character.toString(this.rank));
    37 case 'T': return 10;
    38 case 'J': return 11;
    39 case 'Q': return 12;
    40 case 'K': return 13;
    41 case 'A': return 14;
    42 }
    43 return 0;
    44 }
    45
    46 // insert extra methods here as needed ##
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76} // public class Card
  6. Suppose we want to create a user defined exception called BadSuitException. It should be an unchecked exception that comes up anytime a character is supposed to be one of 'H', 'C', 'S', 'D', but instead it is some other value.

    1. (8 pts) Write the code to define this new exception. For full credit, be sure there is a constructor that can take a String msg as a parameter—that string should be passed through to the parent constructor.







    2. (8 pts) Here's the constructor from the Card class, and a utility method called badSuit.

      Modify the code for the constructor so that the BadSuitException exception comes up if the suite has a bad value.

      You ONLY need to rewrite the constructor in the space below the code—nothing else.
      1public class Card
      2{
      3 private char suit; private char rank;
      4
      5 public Card(char suit, char rank) {
      6 this.suit = suit;
      7 this.rank = rank;
      8 }
      9 public static boolean badSuit(char c) {
      10 switch (c) { case 'H': case 'S': case 'D': case 'C': return false;}
      11 return true;
      12 }
      13}

       

       

    3. (4 pts) Briefly explain what would have to be different about your answer to "a" if BadSuitException were a checked exception instead of an unchecked (5 pts) exception.



    4. (4 pts) Briefly explain what would have to be different about your answer to "b" if BadSuitException were a checked exception instead of an unchecked exception.


  7. (10 pts) A few reminders about Swing GUIs:
    • Components can be nested.
    • At each level, a differnet layout managers (such as BorderLayout, FlowLayout, BoxLayout and GridLayout) can be applied.
    • Widgets placed within a given component use the Layout manager selected.

    Draw a picture of a Gui for some application (real or imagined) that illustrates at least two different layouts from the list above—that is, an outer component should use layout manager "A" and the inner component should use layout manager "B", where both A and B come from the list of choices (BorderLayout, FlowLayout, BoxLayout, GridLayout) and A≠ B.

    Then label your diagram, showing which layout manager the outer component is using, and which the inner component is using.

    Your Gui can have buttons, labels, components, text areas, or whatever you like.

    The Gui could be one of the TicTacToe programs we wrote in lab06, or something else of your choosing.

    You do NOT have to write any Java code for this problem—just draw a picture, and label it properly.

    The important thing for full credit:
    your picture should illustrate clearly that you understand how the two layout managers you selected work to layout widgets.













     


End of Exam

Total points: ?

Handout for E02, CS56, W11

Book.java

Book.java
1// ### code for Midterm 2 exam question
2
3public class Book {
4 private int year;
5 private String name;
6
7 public Book(String theName, int theYear) {
8 this.name = theName; this.year = theYear;
9 }
10
11 public static void main(String [] args) {
12 int mYear = 2011;
13 String mTitle = "Moby Dick";
14 Book t = new Book(mTitle,mYear);
15 System.out.println("A");
16 t = new Book("The Grapes of Wrath",1931);
17 System.out.println("B");
18 }
19}

Orderable


public interface Orderable {

  /** compare "this" object to Object o.  
  
      When this method is implemented,
      you can assume that o is an object of the type of the class
      you are working with, and cast it to that class (e.g. Book other = (Book) o; ).
      
      Then compare object this to object other (for example).
      
      if this object is "less than" other, return any negative integer
      if this object is equal to other, return 0
      if this object is "greater" than other, return any positive integer 
 
      @return a value less than, equal to, or greater than 0, as defined above.
      
      */
      
  public int compareTo(Object o);


}

See other side for Book.java