Dimensions of a Can of Soup

From CompSciWiki
Revision as of 13:54, 1 April 2010 by TylerD (Talk | contribs)

Jump to: navigation, search

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:

// Useful constants - The point of this program.
// If you don't have constants for these then shame on you.
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.

Using these constants we must now calculate the surface area and the volume of the can. Let's start with the surface area:

// The surface area is the circumference * the height
// and the area of the top and bottom
final double SURFACE_AREA = (2*Math.PI*RADIUS*HEIGHT) + (2*Math.PI*Math.pow(RADIUS,2));

As you can see, you can declare a constant that is initialized with mathematical operators and other variables - but only if the other variables are constant themselves! There are four different constants in this line not counting the one we just declared:

  1. 2 - The constant number 2.
  2. RADIUS - One of the constants that we created.
  3. HEIGHT - The other constant that we created.
  4. Math.PI - This is a constant approximation of pi that Java gives to you. You should use it whenever possible rather than declaring your own.

If you were to include in the initialization a variable that was not declared of type final, Java would issue a compile error.


We continue by calculating the volume of the can:

// The volume is the area of the circlular portion of the can
// multiplied by the height
final double VOLUME = (Math.PI*Math.pow(RADIUS,2)) * HEIGHT;

The Math.pow(x,y) function is used in both the calculation of the surface area and the volume. This function is used when you want to calculate x to the y'th power. In this case we are calculating the radius squared.

It is also important to note that brackets play an important role in order of operations. If you are unsure about using brackets, use them. If you don't use them when you should, you may get weird results.

Code

Solution Code

Back to the Program-A-Day homepage