Dimensions of a Can of Soup
Back to the Program-A-Day homepage
ProblemYour best friend in the whole world rummages through your cupboards and emerges with a can of clam chowder. Being slightly obsessive about measuring objects, your friend uses a measuring tape and concludes that the radius of the can is 4 centimeters and the height is 12 centimeters. Your friend then poses some questions to you:
Being slightly obsessive about using Java to do all of your mathematical computations, you design a program to find answers to these questions. You are also a rebel; You do not like using the Java constant for PI, so you create your own. |
SideSectionTitle
An image or By Students section | |
---|---|---|
Solution
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 double PI = 3.14159265; // Rebellious 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*PI*RADIUS*HEIGHT) + (2*PI*Math.pow(RADIUS,2)); // The volume is the area of the circlular portion of the can // multiplied by the height double volume = (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 "+surfaceArea+" centimeters squared."); System.out.println("2. The volume of the can is "+volume+" centimeters cubed."); System.out.println("3. No you may not eat the soup. Put it back."); } } | ||
CodeSolutionCode goes here. Please DO NOT put your code in <pre> tags! |