Difference between revisions of "Dimensions of a Can of Soup"

From CompSciWiki
Jump to: navigation, search
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:
  
<code>
+
<pre>
 
final int HEIGHT = 12;
 
final int HEIGHT = 12;
 
final int RADIUS = 4;
 
final int RADIUS = 4;
</code>
+
</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

Problem

Your 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:

  1. What is the surface area of the can?
  2. What is the volume of the can?
  3. Can I eat this clam chowder?

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

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

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:

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

Solution Code

Back to the Program-A-Day homepage