Exercise 2: Math Class & Formatted Output

In this exercise you will calculate various mathematical properties of oblate spheroids – flattened spheres whose shape is defined by their equatorial radius \(a\) and polar radius \(c\). Planets such as the Earth or Saturn are typically modelled as having such a shape.

Diagram showing oblate and prolate spheroids

Oblate (left) and prolate (right) spheroids

Start by creating a file named Spheroid.java, in the exercises/ex2 directory of your repository. Define a class named Spheroid in this file, and put your program in this class. You MUST use these exact names for the file and the class!

Your program should prompt the user to enter values for the equatorial and polar radii \(a\) and \(c\) of an oblate spheroid, in units of kilometres. Use the Scanner class to obtain these values, storing them as double variables. Do not do any error checking on these values.

Your program should compute the eccentricity of the spheroid, given by the formula

$$e = \sqrt{1 - \frac{c^{2}}{a^{2}}}$$

Your program should also compute the volume of the spheroid, using the formula

$$V = \frac{4\pi a^{2} c}{3}$$

Finally, your program should display two lines of output, one showing the eccentricity of the spheroid, the other showing its volume. Use System.out.printf to generate both lines of output. Eccentricity should be displayed to 3 decimal places. For the display of volume, use %g as the formatting directive. See Section 2.4.1 of Eck’s book if you need further help with formatted printing.

Here is an example of what the user should see when running the program from a terminal window:

$ java Spheroid
Enter equatorial radius in km: 6378.1
Enter polar radius in km: 6356.8
Eccentricity = 0.082
Volume = 1.08320e+12 cubic km

The example above uses radii for Earth. Here’s another example, using radii for Saturn:

$ java Spheroid
Enter equatorial radius in km: 60268
Enter polar radius in km: 54364
Eccentricity = 0.432
Volume = 8.27130e+14 cubic km

(Here the higher eccentricity indicates that Saturn is significantly more ‘squashed’ than the Earth, due to a much higher speed of rotation.)

Go to Submit My WorkExercises 1-5Exercise 2 in Minerva to submit your solution for grading. You can improve your solution and resubmit as many times as you like until the deadline. Also, don’t forget to commit your work to your Git repository and push those commits up to GitLab.

Tips