Page 377 - Introduction to Programming with Java: A Problem Solving Approach
P. 377
Review Question Solutions 343 TestRuntime that asks the user for a desired number of iterations, num. Then have it measure the runtime
for a loop of num iterations that executes the single statement: Math.cos(0.01 * i);
The variable i is the loop count variable. Review Question Solutions
1. False. Readability is an important attribute of good computer code. To save printer paper, print on both sides of the page and/or use smaller font.
2. The seven items to include in a file prolog are: • lineofasterisks
• filename
• programmername(s)
• blanklinewithoneasterisk • description
• lineofasterisks
• blankline
3. False. That would provide maximum room for each comment, but good programmers make the beginnings of declaration comments line up with each other, and they try to make declaration comments short enough to avoid line wrap.
4. False. Take control, and break a long line at the most logical place(s). Apago PDF Enhancer
5. Even though it’s not necessary, it’s a good idea to provide braces with single-statement if and while statements because
• Bracesprovideavisualcueforrememberingtoindent.
• Braceshelpyouavoidalogicalmistakeifyouaddcodelater.
6. Unless a block is very short, it may not be immediately obvious which block is being terminated by a particular brace. It’s good practice to terminate all but the shortest blocks with a comment, for example,
} // end if
} // end main
} // end class Whatever
7. Separate large chunks of code with blank lines.
8. Yes means include a space, No means do not.
• Yes,afterthesingleasterisksintheprologue.
• No, not between a method call and its opening parentheses.
• No,notwithineachofthethreecomponentsinaforloopheader.
• Yes,afterthetwosemicolonsintheforloopheader.
• Yes, between a closing brace and the //’s for its associated comment.
• Yes,afterthe//’sforallcomments.
• Yes, after the if, while, and switch keywords.
9. d) All of above.
10. No. the interface does not describe private members.
11. True. You should generally try to keep things as local as possible, and using local variables instead of instance variables is one way to do this. Instance variables should be reserved for attributes that describe an object’s state.