Back to 111 Main Page

Mobius strip

Assignment 09

Task

Repeat A02, only this time using static methods and a user-entered radius and height.

Textbook: 4.4; 10.1 - 10.3
New concepts: defining methods, exception handling (try/catch)

Steps

Write a program that asks the user to enter the radius and height of a right circular cone, and then print out its details. Print out both the radius and height entered, as well as the slant height, volume, lateral surface area, and total surface area. See A02 if you need to review the necessary formulas.

Step 1: Methods

It would be nice to be able to reuse these calculations for other cones, much in the way you reuse the methods written in the Math class. Write the following static methods in your class:

  • public static double getSlantHeight(double r, double h)
  • public static double getVolume(double r, double h)
  • public static double getLateralSurfaceArea(double r, double h)
  • public static double getTotalSurfaceArea(double r, double h)

Remember that, as separate methods, these definitions will be in your class but outside of the main method. Also, you can name the parameters whatever you want--you could use radius, rather than r. These parameter names have nothing to do with the variable names you use in main.

However, the name of the method does matter. Make sure you name the methods exactly as shown, as Tamarin will be trying to call them, passing different values than you used when you call them from your main method. Remember that case matters.

Clarification (23 Sep 2009): Also, note that all of these methods takes the same two values: the radius and the height of the cone, given in that order. (So do not write any of your methods to take the slant height or some other value; while your main method might still give the correct output if you did, the methods themselves would then return to the wrong result to Tamarin.)

You will need to provide the {bodies} for these methods that do the appropriate calculation in each case and returns the result. Ideally, the calculation of each detail will occur in only one place in your code. Remember that you can call one method from another. (For example, it might be helpful to call getSlantHeight from getLateralSurfaceArea.)

Step 2: Main method

Call each of the four methods you wrote from your main method. Again, the methods will now perform the calculations for you, so you don't need to repeat them in main.

Also, make sure the user's input is valid. Use a try/catch block so that your program ends gracefully (rather than crashing) if the user enters a String when you ask for a number. Also, make sure that the entered radius and height lengths are not negative. The user should be able to enter decimal numbers.

Sample Output

D:\TA\grading\A09>java ZtomaszeA09
Enter the radius of a circular cone: radius
Sorry, but you must enter a valid decimal number.

D:\TA\grading\A09>java ZtomaszeA09
Enter the radius of a circular cone: 1
Enter the height of the cone: -5
Sorry, a cone's dimensions must be >= 0.

D:\TA\grading\A09>java ZtomaszeA09
Enter the radius of a circular cone: 3
Enter the height of the cone: 4.0

For a cone with radius 3.0 and height 4.0:
Slant height = 5.0
Volume = 37.69911184307752
Lateral surface area = 47.12388980384689
Total surface area = 75.39822368615503

What to Submit

Upload your UsernameA09.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1.4 - Robust main
You read in the radius and height from the user as doubles, and clearly print the results returned by the four methods (0.6). Your program does not crash (but instead ends gracefully) when given strings instead of numbers (0.5). You do not allow negative lengths (0.3).
1.6 - Methods
Methods are named as above and each provides the correct result when passed a radius and height value (given in that order) (0.4 each).

FAQs

Example?
As an example of using try/catch, here is an updated version of A04's UserAge.java: SafeUserAge.java. This version will not crash when the user enters something other than a number when requested.
How do I use an exception to catch negative numbers?
You don't. (In fact, you can't.)

The only time you're going to get an exception from your Scanner is when you ask for a number and the user enters something else. (And the only way to handle this situation is by catching the InputMismatchException. You can't use an if here because, if the user enters a String rather than a number, the call to nextDouble() never even returns.)

Now, once you've successfully read in a number, your particular program might have additional requirements on the format: only even numbers, only something between 1 and 10, or (as for A09) only a number > 0. This you have to test using an if after you successfully read in the number. (You can't use an exception here; you have to test the number yourself with some sort of comparison operator.)
Having a cone with a dimension of 0 doesn't really make sense.
You can disallow 0 if you want to--that's fine.


~ztomasze Index : TA Details: ICS111: Assignment 9
http://www2.hawaii.edu/~ztomasze
Last Edited: 25 Sep 2009
©2008 by Z. Tomaszewski.