0% found this document useful (0 votes)
73 views

Sharpen Solutions: Part One

The document discusses code examples for declaring variables and calling methods. It also presents partial code examples and asks the reader to identify legal statements, fill in missing code, and suggest ways to improve testing. The examples cover topics like classes, objects, methods, and inheritance.

Uploaded by

Kshitij Banerjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Sharpen Solutions: Part One

The document discusses code examples for declaring variables and calling methods. It also presents partial code examples and asks the reader to identify legal statements, fill in missing code, and suggest ways to improve testing. The examples cover topics like classes, objects, methods, and inheritance.

Uploaded by

Kshitij Banerjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1 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.)

this is a new chapter 1


chapter one

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 11 Given the output:


% java Test
DooBeeDooBeeDo

Fill in the missing code:

public class DooBee {


public static void main (String[] args) {
int x = 1;
3 ){
while (x < _____
print
System.out._________(“Doo”);
print
System.out._________(“Bee”);
x = x + 1;
}
3 ){
if (x == ______
System.out.print(“Do”);
}
}
}

2 Sharpen Your Pencil - Part One


chapter two Head First Java Sharpen Solutions

Page 32
Television
int channel
int volume
boolean power

setChannel()
setVolume()
setPower()
skipCommercials()
searchForSimpsons()

Page 35

MOVIE title Gone with the Stock


title
genre object 1 genre Tragic
rating rating -2
playIt()

title Lost in Cubicle Space


object 2 genre Comedy
rating 5

title Byte Club


object 3 genre Tragic but ultimately uplifting
rating 127

you are here 4 3


chapter three

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;

10. short n = 12;

11. v = n;

12. byte k = 128;

13. int p = 3 * g + y;

4 Sharpen Your Pencil - Part One


chapter four Head First Java Sharpen Solutions

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

you are here 4 5


chapter five

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:

Make a fake user guess that is a MISS instead of a hit


Try all three hits
Try out a range of guesses
Try duplicate guesses
(these are just a few...)

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?

Stay tuned for the next chapter, where we answer


these questions and more...
And in the meantime, see if you can come up with
ideas for what went wrong and how to fix it.

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.

6 Sharpen Your Pencil - Part One


chapter six Head First Java Sharpen Solutions

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 ]

you are here 4 7


chapter seven

Page 165

How many instance variables does


Surgeon have? 1

How many instance variables does


FamilyDoctor have? 2

How many methods does Doctor have? 1


How many methods does Surgeon have? 2
How many methods does FamilyDoctor
have? 2

Can a FamilyDoctor do treatPatient()? Yes


Can a FamilyDoctor do makeIncision()? No

Page 175
Page 172

Fan Musician Put a check next to the relationships that


make sense.

Oven extends Kitchen


Guitar extends Instrument

concert Bass Player Person extends Employee


Rock Star
Pianist Ferrari extends Engine
FriedEgg extends Food
What if I want
h at if y ou have ao Beagle extends Pet Beagle to extend
p r o b le m here... w? A rock star wh Dog, but not all
see a star rock Container extends Jar dogs are pets?
I think Iyer who IS a rocllk bass players arlae yers, so
bass pla s player? Not a stars are bass p Metal extends Titanium (chapter 8)
IS a bas nd not all rock
stars, a you do? here GratefulDead extends Band
at d o fo r c h apter 8 w
wh wait Blonde extends Smart
e r : y o u ’l l have toaces...]
[answ about interf Beverage extends Martini
we talk
Hint: apply the IS-A test

8 Sharpen Your Pencil - Part One


chapter eight Head First Java Sharpen Solutions

Page 201

Concrete Sample class Abstract

golf course simulation Tree tree nursery application

monopoly game
____________________ House architect application

satellite photo application Town Atlas/map application


_____________________

video game
____________________ Football Player coaching application

banquet seating app home floorplan


design app
____________________ Chair _____________________

business modeling app


____________________ Customer
technical
support app
_____________________

employee training app


____________________ Sales Order Store inventory system
_____________________

home inventory program


____________________ Book online book store
_____________________

Mall store directory


kiosk
____________________ Store Warehouse distribution system
_____________________

Simple business accounting


____________________ Supplier Just-in-time inventory system
_____________________

simple golf game


____________________ Golf Club Pro shop POS system
_____________________

Parts Inventory app


____________________ Carburetor Engine design software
_____________________

Home / architectural design


____________________ Oven Gourmet restaurant app
_____________________

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

You might also like