logo资料库

head first java shapensone 习题解答.pdf

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
1 Part One h g Sharpen Solutions g You didn’t think we’d just leave you hanging, did you? No, we thought we’d be all nice and helpful with this fi rst 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; String name = “Fido”; Dog myDog = new Dog(name, size); x = size - 5; if (x < 15) myDog.bark(8); while (x > 3) { myDog.play(); declare an integer variable named ‘size’ and give it the value 27 declare a String variable named ‘name’ and give it the String value “Fido” declare a Dog variable named ‘myDog’ and give it a new Dog (that has a name and a size) subtract 5 from the current value of the variable ‘size’, assign the result to the variable ‘x’ if the value of x is less than 15, then tell myDog to bark 8 times as long as the value of x is greater than 3, tell myDog to 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 print System.out._________(“Doo”); System.out._________(“Bee”); x = x + 1; } if (x == ______ ) { 3 System.out.print(“Do”); } } } 2 Sharpen Your Pencil - Part One
Head First Java Sharpen Solutions chapter two Page 32 Television int channel int volume boolean power setChannel() setVolume() setPower() skipCommercials() searchForSimpsons() Page 35 MOVIE title genre rating playIt() Gone with the Stock Tragic -2 Lost in Cubicle Space Comedy 5 Byte Club Tragic but ultimately uplifting 127 you are here 4 3 object1object2object3titlegenreratingtitlegenreratingtitlegenrerating
Page 50 What is the current value of pets[2]? ___________ null What code would make pets[3] refer to one of the two existing Dog objects? _______________________ pets[3] = pets[0] chapter three Page 50 Circle the legal statements from the following list: 1. int x = 34.5; 2. boolean boo = x; 3. int g = 17; 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 Page 85 What’s legal? Given the method below, which of the method calls listed on the right are legal? Put a checkmark next to the ones that are legal. (Some statements are there to assign values used in the method calls). int calcArea(int height, int width) { return height * width; int f = calcArea(t,17); int g = calcArea(); calcArea(); need two args byte h = calcArea(4,20); } int j = calcArea(2,3,5); Head First Java Sharpen Solutions int a = calcArea(7, 12); short c = 7; calcArea(c,15); int d = calcArea(57); need two args calcArea(2,3); long t = 42; ‘t’ is a long (too big for the int parameter ‘height’) need two args calcArea returns an int, not a byte 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 Page 130 Turn to page 132 in your book for the answer. Page 141 Head First Java Sharpen Solutions 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). [ K at hy , t h i s l o o ks p re t t y w e a k h e re fo r c h a p t e r s i x . i k e w e ’ re g i v i n g t h e m A N Y T H I N G ! D o n ’ t y o u D o e s n ’ t l o o k l fe e l g u i l t y a b o u t t h at ? — B e r t ] [ 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? How many methods does Surgeon have? 1 2 How many methods does FamilyDoctor have? 2 Can a FamilyDoctor do treatPatient()? Can a FamilyDoctor do makeIncision()? Yes No Page 172 Fan Musician Rock Star concert Pianist Bass Player i f o r s r e s a b a o a r o r e v w h k c s , s r e 8 w h e r o e o u s t e r p l a e t y a k p c s a h a y r I b s a e e I k w h r e y p l a y p l a s s a b o n n d a s , u o y d o t o y r : s w e b a l k t a n h i n t s s a a I S a s t w h [ r a a w e c o r b l e m o p r a I S o t N o ? r e r a ll t ? d o u ’ll t u o a h i n v t e e h k o t a e ... w h A ? r a s t y p l a s s a b e r a a s r r e k c a ll s t w a i t s ... ] e c o t r f a c r h f o 8 Sharpen Your Pencil - Part One Page 175 Put a check next to the relationships that make sense. Oven extends Kitchen Guitar extends Instrument Person extends Employee Ferrari extends Engine FriedEgg extends Food Beagle extends Pet Container extends Jar Metal extends Titanium What if I want Beagle to extend Dog, but not all dogs are pets? (chapter 8) GratefulDead extends Band Blonde extends Smart Beverage extends Martini Hint: apply the IS-A test
分享到:
收藏