Sharpen Solutions: Part One
Sharpen Solutions: Part One
h
g
g Sharpen Solutions
You didn’t think we’d
just leave you hanging, did you?
No, we thought we’d be all nice
and helpful with this first book,
to get you hooked, and then
slam you in the next one...
Sometimes there’s more than one right answer. And sometimes the
answer is whatever you want it to be. If you don’t like our answers, argue with us. If we’re
just plain wrong, we’ll change it and give you credit on the web site. If you’re wrong, we’ll
publicly humiliate you, using a very large font.
Just kidding.
Please share your ideas and solutions with us, and we’ll add them with your name (unless
you want to be anonymous, and who could blame you.)
Page 4
int size = 27; declare an integer variable named ‘size’ and give it the value 27
String name = “Fido”; declare a String variable named ‘name’ and give it the String value “Fido”
Dog myDog = declare a Dog variable named ‘myDog’ and give it a new Dog (that has a name and a size)
new Dog(name, size);
x = size - 5; subtract 5 from the current value of the variable ‘size’, assign the result to the variable ‘x’
if (x < 15) myDog.bark(8); if the value of x is less than 15, then tell myDog to bark 8 times
while (x > 3) { as long as the value of x is greater than 3, tell myDog to play
myDog.play();
Page 32
Television
int channel
int volume
boolean power
setChannel()
setVolume()
setPower()
skipCommercials()
searchForSimpsons()
Page 35
Page 50 Page 50
Circle the legal statements
What is the current value of
from the following list:
null
pets[2]? ___________
1. int x = 34.5;
What code would make
2. boolean boo = x; pets[3] refer to one of the
two existing Dog objects?
3. int g = 17; pets[3] = pets[0]
_______________________
4. int y = g;
5. y = y + 10;
6. short s;
7. s = y;
8. byte b = 3;
9. byte v = b;
11. v = n;
13. int p = 3 * g + y;
Page 85
int a = calcArea(7, 12);
What’s legal?
short c = 7;
Given the method below, which
calcArea(c,15);
of the method calls listed on the
right are legal? int d = calcArea(57); need two args
Put a checkmark next to the calcArea(2,3);
ones that are legal. (Some
statements are there to assign long t = 42;
(too big for the int
‘t’ is a long ‘he
values used in the method calls). int f = calcArea(t,17); parameter ight’)
int g = calcArea(); need two args
calcArea(); need two args
calcArea returns an int,
int calcArea(int height, int width) {
return height * width; byte h = calcArea(4,20); not a byte
} int j = calcArea(2,3,5);
need two args
Page 101
In the next couple of pages we implement the SimpleDotCom class,
and then later we return to the test class. Looking at our test code
above, what else should be added? What are we not testing in this
code, that we should be testing for? Write your ideas (or lines of
code) below:
Page 105
Turn the to the next page in your book (106) for the answer. But then,
you obviously know that already. We just put this in for completeness.
Didn’t want you thinking we just skipped it. Although we actually are
skipping it. Here in the solutions document, anyway, not in the real
book. You know what we mean.
Page 111
It’s a cliff-hanger!
Will we find the bug?
Will we fix the bug?
Will Ben marry J-Lo?
The current version of the game cares only about the NUMBER
of hits, not what the actual hits really are. So entering the
same number (as long as it’s a hit) three times is the same as
entering the three correct, but different, numbers corresponding
to the three hit locations.
So, we need to somehow keep track of each hit, and maybe
“cross it off the list” to show that it’s been hit and is no longer
a valid entry.
Page 130
Turn to page 132 in your book for the answer.
Page 141
We didn’t do an answer for this one, but nobody around here can
remember why. Must have been some excuse about how that makes
it more of a “learning opportunity” for you. If you’ve got an answer
you want to “share” with others (for that warm fuzzy feeling and good
karma points), let us know and we’ll include it (with your name).
six.
k h ere for chapGter ou
at hy , t h is lo o ks p re t t y w ea
t h em A N Y T HIN on’t y
! D
[K ke we’re giving
Doesn’t look bliout that? —Bert ]
feel guilty a
[ No. —Kathy ]
Page 165
Page 175
Page 172
Page 201
monopoly game
____________________ House architect application
video game
____________________ Football Player coaching application
Note: this is a little confusing and a little subjective, but here’s a tip for this exercise -- the abstract
category is for applications where the class in the center column would need to be SUBCLASSED. The
concrete category is for applications where the class in the center can be concrete, and the only thing dif-
ferentiating one instance from another is instance variable values. For example, in a home inventory system,
you probably don’t need to distinguish between different classes of books, you simply need an instance vari-
able with the title, so that each instance of Book represents a book in your house. But a bookstore might
*care* about different TYPES of books like FictionBook, TradePaperback, Children, etc. since each type
might have different policies for stocking, discounts, and advertising.
you are here 4 9