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:
main()
class is run, and answer the questions below.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 |
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) |
(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:
rankNum
method for ranking suits. Card.java | |
---|---|
1 | public 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 |
String msg
as a parameter—that string should be passed through to the parent constructor.1 | public 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 | } |
BadSuitException
were a checked exception instead of an unchecked (5 pts) exception.BadSuitException
were a checked exception instead of an unchecked exception. 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.
Total points: ?
Book.java | |
---|---|
1 | // ### code for Midterm 2 exam question |
2 |
|
3 | public 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 | } |
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