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
).
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.
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.
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.
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.
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
Upload your UsernameA04.java
file to Tamarin.
main
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.)
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.)
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 double
s 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.