Back to 111 Main Page

Mobius strip

Assignment 5

Task

Explore static methods and simple loops.

Steps

Write a short program named Assignment5 that prints out the details for a sphere. This is essentially a repeat of Assignment 2 with the following changes.

Step 1: Static Methods

Write four static methods that calculate the following details of a sphere:

  • Diameter (2r)
  • Circumference (πd)
  • Surface area (4πr2)
  • Volume (4/3 * πr3)

Each method should take a double parameter (the radius of the sphere) and return a double (the result of the calculation). Here is some code to get you started:

public class Assignment5 {

  public static void main(String[] args) {
    double radius = 5;

    System.out.println("For a sphere with radius " + radius + ": ");
    System.out.println("Diameter = " + getDiameter(radius));
    //...
  }

  public static double getDiameter(double radius) {
    return (radius * 2);
  }

}

Step 2: User Input

Now use a Scanner to ask the user to enter the radius each time.

Step 3: Loop

Now add a loop that asks the user if they would like to enter another radius. (A while or do-while would be best for this.) If the user enters "yes", the program should run again, asking for a new radius. If they enter "no", the program should end.

Sample Output

Here is some sample output for the finished program:

Enter the radius of a sphere: 5

For a sphere with radius 5.0:
Diameter = 10.0
Circumference = 31.41592653589793
Surface area = 314.1592653589793
Volume = 523.5987755982989

Would you like to display the details of another sphere (yes/no)? yes
Enter the radius of a sphere: 10

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

Would you like to display the details of another sphere (yes/no)? no

What to submit

Attach your Assignment5.java file to an email.

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
2 - Program compiles
2 - Static methods
You call methods that you wrote in order to do the calculations.
1 - Input
The program asks the user to enter a radius each time.
1 - Program is correct
Prints correct results for diameter, circumference, surface area, and volume.
3 - Loop
Your program asks the user whether to repeat and then does so accordingly.


~ztomasze Index : TA Details: ICS111: Assignment 5
http://www2.hawaii.edu/~ztomasze
Last Edited: 22 May 2008
©2008 by Z. Tomaszewski.