Difference between revisions of "Dimensions of a Can of Soup"
Line 18: | Line 18: | ||
|Solution=During the course of the program, the height and radius of the can do not change. This would be a perfect opportunity to use our knowledge of constants and the ''final'' keyword: | |Solution=During the course of the program, the height and radius of the can do not change. This would be a perfect opportunity to use our knowledge of constants and the ''final'' keyword: | ||
− | < | + | <pre> |
final int HEIGHT = 12; | final int HEIGHT = 12; | ||
final int RADIUS = 4; | final int RADIUS = 4; | ||
− | </ | + | </pre> |
Notice that we have capitalized the variable names of our constants. This is a standard in most (if not all) of the programming world. We have also initialized the constants in the same line as we declared them. This is because after you declare a variable with the keyword ''final'' you cannot modify it. | Notice that we have capitalized the variable names of our constants. This is a standard in most (if not all) of the programming world. We have also initialized the constants in the same line as we declared them. This is because after you declare a variable with the keyword ''final'' you cannot modify it. |
Revision as of 13:30, 1 April 2010
Back to the Program-A-Day homepage
ProblemYour best friend rummages through your cupboards and emerges with a can of clam chowder. Being slightly obsessive about measuring objects, your friend concludes that the radius of the can is 4 centimeters and the height is 12 centimeters. Your friend then poses some questions to you:
Design a program that finds answers to these questions. Make sure to use constants for values that do not change. Round all answers to the nearest integer value. |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionDuring the course of the program, the height and radius of the can do not change. This would be a perfect opportunity to use our knowledge of constants and the final keyword: final int HEIGHT = 12; final int RADIUS = 4; Notice that we have capitalized the variable names of our constants. This is a standard in most (if not all) of the programming world. We have also initialized the constants in the same line as we declared them. This is because after you declare a variable with the keyword final you cannot modify it. | ||
Code |