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:
- What is the surface area of the can?
- What is the volume of the can?
- 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
Taken from http://www.flickr.com/photos/daniello/565304023/
An image or By Students section
|
Solution
An effective solution to this problem involves figuring out that there are a few constant values
|
Code
Solution Code
/* Class CanOfSoup
* Created by Tyler Dielschneider
* Made in COMP3040
*
* Finds the surface area and volume of a can of soup.
* Bonus: Answer whether your friend can eat the soup or not.
*/
public class CanOfSoup
{
public static void main(String[] args)
{
// 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;
// The surface area is the circumference * the height
// and the area of the top and bottom
double surfaceArea = (2*Math.PI*RADIUS*HEIGHT) + (2*Math.PI*Math.pow(RADIUS,2));
// The volume is the area of the circlular portion of the can
// multiplied by the height
double volume = (Math.PI*Math.pow(RADIUS,2)) * HEIGHT;
// Print out the answers to your friend's questions
System.out.println("1. The surface area of the can is "+Math.round(surfaceArea)+" centimeters squared.");
System.out.println("2. The volume of the can is "+Math.round(volume)+" centimeters cubed.");
System.out.println("3. No you may not eat the clam chowder. Put it back.");
}
}
Output:
1. The surface area of the can is 402.1238592 centimeters squared.
2. The volume of the can is 603.1857888000001 centimeters cubed.
3. No you may not eat the clam chowder. Put it back.
|
Back to the Program-A-Day homepage