![]() |
Assignment 09Task
Repeat A02, only this time using static methods and a user-entered radius.
Textbook: 4.4; 10.1 - 10.3 StepsWrite 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
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--they could be 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. 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. Step 2: Main methodCall 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 radius length entered is not negative. Sample OutputD:\TA\grading\A09>java ZtomaszeA09 Enter the radius of a sphere: radius Sorry, but you must enter a valid decimal number. D:\TA\grading\A09>java ZtomaszeA09 Enter the radius of a sphere: -20 Sorry, a sphere's radius must be >= 0. D:\TA\grading\A09>java ZtomaszeA09 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 What to Submit
Upload your Grading [4 points]
FAQs
|
~ztomasze Index :
TA Details: ICS111:
Assignment 9 http://www2.hawaii.edu/~ztomasze |
Last Edited: 12 Feb 2009 ©2008 by Z. Tomaszewski. |