Back to 111 Main Page

Mobius strip

Assignment 8

Task

Explore constructors, methods, and basic encapsulation.

Steps

Write a short program named Assignment8 that asks the user for a radius and then prints out the details of a sphere of that size. This is essentially a repeat of Assignment 5 with the following changes.

Step 1: Sphere.java

Write class named Sphere, saved in a file named Sphere.java. This class should have the following details.

Instance variable. A Sphere has a single double instance variable named radius. Make this variable private.

Constructor. Write a public constructor that takes a single double parameter--the radius of the new Sphere.

Methods. Write five instance methods. These methods should all be public, take no parameters, and return the appropriate detail:

  • public double getRadius()
  • public double getDiameter()
  • public double getCircumference()
  • public double getSurfaceArea()
  • public double getVolume()
Since these methods qualify as "accessors", you do not need to write javadoc comments for them.

All Sphere members (variables and methods) are associated with the instance rather than the class. Therefore, the word static should not appear anywhere in Sphere.java.

Step 2: Assignment8.java

Now write another class named Assignment8. This method should have only a main method. In this main method, ask the user to enter a radius. Use this radius to create a Sphere object. Call all of the object's methods to retrieve the details you need to print. Ask the user if they would like to enter the radius of another Sphere, and keep repeating the program as long as they say "yes".

In other words, your output will be the same as it was for Assignment5.

What to submit

Attach both your Assignment8.java and your Sphere.java files to an email.

FAQs

Do you have any examples of this sort of thing?
Here is an example modeling a person. Person.java has two private instance variables, a constructor, and three public methods. PersonTester.java then uses a Person instance. To run PersonTester, you need to save both files into the same directory, compile both, and then run PersonTeseter.
Do we import something into our code for the Sphere class? Maybe something like import java.util.Sphere?
No. Sphere.java just needs to be in the same directory/folder as Assignment8.java and Java will find it.
Does Sphere.java contain the formulas for the sphere, or would they be in the main method of Assignment8.java?
The formulas would be in the methods of the Sphere class. The Sphere methods still look a lot like the methods of Assignment 5, except:
  • They are not static
  • They do not take a parameter
  • They use this.radius (rather than the parameter) in calculating the result.
Assignment8.java only has one method (main), which looks quite similar to how it did in A5. However, rather than passing the radius as a argument to each method, you first create a Sphere object (with the radius as an argument to the constructor), and then you call the methods on that Sphere object.

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
1 - Coding Standards
Follows required coding standards.
1 - Compiles
1.5 - Sphere instance variable
Should be private and non-static.
1 - Sphere constructor
You have a public constructor that takes a single parameter.
2.5 - Sphere's five get methods
Should all be public and non-static. Should take no parameters. Returned results should be correct.
2 - Input, Output, and Loop
Main program asks user to enter a radius, prints the five details for that sphere, and keeps looping as long as the user chooses to do so.


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