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:
System.out.println("c = " + c);
when c
is of type Coin.
"25 cents"
, a nickel should return "5 cents"
, and a penny should return "1 cent"
. "2 cents"
, "20 cents"
or "42 cents"
. A dollar coin should return "100 cents"
. Only the value "1 cent"
should be treated as a special case (no s
.)There is more room on the next page for your answer if you run out of room here.
Extra space for your answer to question 1
(30 pts) Assume that we have a class Book
with attributes title
and year
(representing copyright year). We have a two-argument constructor that takes title
and year
, and default constructor that gives title
the value ""
(empty string) and year the value
0.
Assume that we have overridden the equals() method as follows:
public boolean equals(Object o) { if (! o instanceof Book) { return false;} Book b = (Book) o; return (b.title.equals(this.title)) && (b.year == this.year); }
Assume then that we have the following code inside a main method. The spot marked _____
is a blank where we will fill in an expression.
Book t = new Book(); Book u = new Book(); Book v = u; Book w = new Book("Moby Dick",1851); Book x = new Book("Moby Dick",1851); Book y = w; System.out.println("result =" + ________ ); // we will fill in the blank!
As we demonstrated in class, if we fill in the blank with a boolean expression, when that expression is concatenated with a String
, Java will automatically convert it to either the String literal "true"
or "false"
.
A table of boolean expressions appears below, each one involving the Book
objects t,u,v,w,x
, and y
we've constructed above. For each one, indicate if the boolean expression would evaluate to true
or false
(i.e. whether true
or false
would be printed after result =
)
t == u |
Circle one: true false |
t.equals(u) |
Circle one: true false |
t.equals(v) |
Circle one: true false |
u.equals(v) |
Circle one: true false |
w.equals(w) |
Circle one: true false |
y == w |
Circle one: true false |
w.equals(y) |
Circle one: true false |
w.equals(x) |
Circle one: true false |
y == x |
Circle one: true false |
y.equals(u) |
Circle one: true false |
x
and y
, each of which has a primitive type.
a
and b
, each of which ends up being a reference.
Questions 7-10 are on the back page—dont forget them!
boolean
?
Total points: ?