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

From CompSciWiki
Jump to: navigation, search
(Changed to CodeBlocks; Put output before (not inside) solution code; changed some wording/misspelling)
 
Line 22: Line 22:
 
|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. If you haven't covered these in class yet, know that it's a way of recording a value in a variable so the value does not get accidentally changed.  
 
|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. If you haven't covered these in class yet, know that it's a way of recording a value in a variable so the value does not get accidentally changed.  
  
<pre>
+
{{CodeBlock
 +
|Code=
 
// Useful constants - The point of this program.
 
// Useful constants - The point of this program.
 
// If you don't have constants for these then shame on you.
 
// If you don't have constants for these then shame on you.
 
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.
Line 33: Line 34:
 
Using these constants we must now calculate the volume of the can.  
 
Using these constants we must now calculate the volume of the can.  
  
Hopefully you remember that the equation  
+
Remember, the volume of a cylinder (such as the can) is given by the equation:
  
volume = Pi * R to the power of 2.
+
Volume = H * (Pi * R ^ 2)
  
Use constant  Math.PI to get the value of PI. Then, use Math.pow to raise R (the radius) to the power of 2.  
+
Where H is the height of the can and R is the radius of the circle.
 +
 
 +
Use constant  Math.PI to get the value of PI. Then, use Math.pow to raise R to the power of 2.  
 
   
 
   
<pre>
+
{{CodeBlock
// The volume is the area of the circlular portion of the can
+
|Code=
 +
// The volume is the area of the circular portion of the can
 
// multiplied by the height
 
// multiplied by the height
 
final double VOLUME = (Math.PI*Math.pow(RADIUS,2)) * HEIGHT;
 
final double VOLUME = (Math.PI*Math.pow(RADIUS,2)) * HEIGHT;
</pre>
+
}}
  
 
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.
 
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.
Line 51: Line 55:
 
Now that we've calculated our values, we must answer our friend's questions.  We make sure to round the values in our output:
 
Now that we've calculated our values, we must answer our friend's questions.  We make sure to round the values in our output:
  
<pre>
+
{{CodeBlock
 +
|Code=
 
// Print out the answers to your friend's questions
 
// Print out the answers to your friend's questions
 
System.out.println("1. The volume of the can is "+Math.round(VOLUME)+" centimeters cubed.");
 
System.out.println("1. The volume of the can is "+Math.round(VOLUME)+" centimeters cubed.");
 
System.out.println("2. No you may not eat the clam chowder. Put it back.");
 
System.out.println("2. No you may not eat the clam chowder. Put it back.");
</pre>
+
}}
  
 
You're probably familiar with the System.out.println function.  What may seem confusing is the fact that we are calling the Math.round function in the middle of the line.  This is entirely legal.  We are rounding our values "on the fly" in a sense.
 
You're probably familiar with the System.out.println function.  What may seem confusing is the fact that we are calling the Math.round function in the middle of the line.  This is entirely legal.  We are rounding our values "on the fly" in a sense.
Line 61: Line 66:
 
An important lesson to learn in University is not to omit any requirement.  The problem states to answer whether or not your friend may eat the clam chowder.  You may decide for yourself whether they may or not, but it is important that you do decide.
 
An important lesson to learn in University is not to omit any requirement.  The problem states to answer whether or not your friend may eat the clam chowder.  You may decide for yourself whether they may or not, but it is important that you do decide.
  
That concludes the program. See below for the entire source code.
+
Your output should look something like this:
 +
 
 +
{{OutputBlock
 +
|Code=
 +
1. The volume of the can is 603 centimeters cubed.
 +
2. No you may not eat the clam chowder. Put it back.
 +
}}
 +
 
 +
That concludes the program. See below for the entire source code.
  
 
|SolutionCode=
 
|SolutionCode=
Line 68: Line 81:
 
  * Made in COMP3040
 
  * Made in COMP3040
 
  *
 
  *
  * Finds the volume of a can of soup.
+
  * Finds the volume of a can of soup and
  * Bonus: Answer whether your friend can eat the soup or not.
+
  * determines whether your friend can eat the soup or not.
 
  */
 
  */
  
Line 92: Line 105:
 
   }
 
   }
 
}
 
}
 
Output:
 
 
1. The volume of the can is 603 centimeters cubed.
 
2. No you may not eat the clam chowder. Put it back.
 
 
 
}}
 
}}

Latest revision as of 14:37, 4 December 2011

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 calculates 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 volume of the can?
  2. May I eat this clam chowder?

Design a program that finds answers to these questions. Round all answers to the nearest integer value.

To solve this problem you'll need to use one Math constant and two Math Methods:

  • Math.PI
  • Math.pow
  • Math.round

Also, this problem reminds you to use Named Constants for values that do not change.

 

If Statements and Named Constants

Wiki sign01.jpg

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. If you haven't covered these in class yet, know that it's a way of recording a value in a variable so the value does not get accidentally changed.

 // 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 volume of the can.

Remember, the volume of a cylinder (such as the can) is given by the equation:

Volume = H * (Pi * R ^ 2)

Where H is the height of the can and R is the radius of the circle.

Use constant Math.PI to get the value of PI. Then, use Math.pow to raise R to the power of 2.

 // The volume is the area of the circular 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.

Now that we've calculated our values, we must answer our friend's questions. We make sure to round the values in our output:

 // Print out the answers to your friend's questions
System.out.println("1. The volume of the can is "+Math.round(VOLUME)+" centimeters cubed.");
System.out.println("2. No you may not eat the clam chowder. Put it back."); 

You're probably familiar with the System.out.println function. What may seem confusing is the fact that we are calling the Math.round function in the middle of the line. This is entirely legal. We are rounding our values "on the fly" in a sense.

An important lesson to learn in University is not to omit any requirement. The problem states to answer whether or not your friend may eat the clam chowder. You may decide for yourself whether they may or not, but it is important that you do decide.

Your output should look something like this:

 1. The volume of the can is 603 centimeters cubed.
2. No you may not eat the clam chowder. Put it back. 

That concludes the program. See below for the entire source code.

Code

Solution Code

Back to the Program-A-Day homepage