Assignment 04

Task

Repeat A02, only this time using a user-entered radius and static methods to do the calculations.

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

Steps

Write a program that asks the user to enter the radius of a sphere, and then print out its details. Print out both the radius entered, as well as the diameter, circumference, surface area, and volume. 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 spheres, much in the way you reuse the methods written in the Math class. Write the following static methods in your class:

Remember that, as separate methods, these definitions will be in your class but outside of the main method. 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, in the relevant method.

You can name the parameters whatever you want--they could be radius, rather than r; these parameter names have nothing to do with the variable names you use in main. However, everything else about of the method does matter. Make sure you name the methods exactly as shown, as Tamarin will be trying to call them directly, passing different values than you used when you call them from your main method.

Step 2: Main method

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

In this assignment, your sphere methods are in the same file as the main method, and you will probably only call each method once from main. However, note how it would be much easier to calculate the details of multiple spheres by calling the methods multiple times using a different value for the radius parameter (instead of writing out the formulas each time). Also, you could now reuse these methods in another program, even calling these methods from a main method written in a different class/file.

At this point, you should be able to compile and run your program and get the same input as for A02.

Step 3: User Input

Now add to the main method to prompt the user to enter a radius to use for the calculations. The radius should still be a double. 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. Your program should not try to print out the radius or other calculated details if the user did not correctly enter a number.

Sample Output

User input is highlighted in green. Note that I ran this program three times; the command prompt line at the start of each run is not part of the progam's output.

D:\TA\grading\A04>java ZtomaszeA04
Enter the radius of a sphere: 10.00

For a sphere with radius 10.0:
Diameter = 20.0
Circumference = 62.83185307179586
Surface area = 1256.6370614359173
Volume = 4188.790204786391

D:\TA\grading\A04>java ZtomaszeA04
Enter the radius of a sphere: radius
Sorry, but you must enter a valid decimal number.

D:\TA\grading\A04>java ZtomaszeA04
Enter the radius of a sphere: 0.5

For a sphere with radius 0.5:
Diameter = 1.0
Circumference = 3.141592653589793
Surface area = 3.141592653589793
Volume = 0.5235987755982988

What to Submit

Upload your UsernameA04.java file to Tamarin.

Grading [4 points]

1 - Compiles
Your program compiles successfully (no errors)
1.4 - Robust main
You read in the radius from the user as a double, and clearly print the results returned by the four methods (0.8). Your program does not crash (but instead ends gracefully with only an error message) when given strings instead of numbers (0.6).
1.6 - Methods
Methods are named as above and each provides the correct result when passed a radius value. (0.4 each)

FAQs

Example?
As an example of using try/catch, here is an updated version of A01's ZtomaszeAge.java: SafeUserAge.java. This version will not crash when the user enters something other than a number when requested. It also uses methods to print the details.
Do I have to prevent the user from entering 0 or a negative number?
No, this is not required. (You can do so if you want to, though. You'll have to use an if statement to do it--which we won't learn until next week.)
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 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.)
Is it okay if my answer is very very close to the correct answer but not exactly the same?
Yes, that's fine. For example, you might get something like this (where the circumference and surface area incorrectly vary in the final digit):
Enter the radius of a sphere: 5000

For a sphere with radius 5000.0:
Diameter = 10000.0
Circumference = 31415.926535897932
Surface area = 3.1415926535897934E8
Volume = 5.235987755982988E11

The reason for this is how doubles are stored internally. They are only precise to a certain number of decimal places and, depending on which math operations you perform on them and in which order, that last decimal place may get rounded a digit or two either way along the way. This is always a danger with double math that will become more obvious when we get to to the == (equality) operator next week.

There is a way to format floating point numbers when you print them, so you don't always have to have a big ugly string of digits. However, the formatting either involves the use of an object (such as an instance of java.text.DecimalFormat), or else a rather arcane formatting code (for example: System.out.printf("%.4f\n", volume)). Have a look at the textbook (section 3.6) if you're interested in these. If you do choose to format your output, include at least 4 digits after the decimal point for this assignment.