Back to 111 Main Page

Mobius strip

Assignment 12

Task

Explore basic inheritance, including method overriding and polymorphism.

Steps

For the purposes of this assignment, we're going to think of a Sphere as a kind of Circle--a Circle with a third dimension. In other words, a Sphere has all the same details as a Circle with the same radius.

You are going to break the behavior of Assignment8's Sphere class into a Circle and a Sphere.

Circle.java

A Circle should have a private double radius instance variable.

It should have a public constructor that takes a double parameter, which is the radius of the Circle to create.

It should then have the following methods:

  • public double getRadius()
  • public double getDiameter()
  • public double getCircumference()
  • public double getArea() -- Equals πr2
  • public String toString() -- Returns "Circle(r=##)", where ## == the radius.

Sphere.java

The Sphere class should extend the Circle class.

A Sphere should have no instance variables.

It should have a public constructor that takes a double parameter, which is the radius of the sphere to create. (The constructor will have to invoke its superclass constructor in order to pass the given radius to the Circle class.)

A Sphere will have the following methods:

  • public double getSurfaceArea()
  • public double getVolume()
  • public String toString() -- Returns "Sphere(r=##", where ## == the radius.

Assignment8.java

You can test your classes with the following program: Assignment12.java.

You should get output something like this:

Please enter a radius: 5.0

For Circle(r=5.0):
Diameter = 10.0
Circumference = 31.41592653589793
Area = 78.53981633974483

For Sphere(r=5.0) (as a Circle):
Diameter = 10.0
Circumference = 31.41592653589793
Area = 78.53981633974483

For Sphere(r=5.0):
Diameter = 10.0
Circumference = 31.41592653589793
Surface area = 314.1592653589793
Volume = 523.5987755982989

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

Questions

Paste the answers to the following questions into the body of your email:

1. In this assignment, Circle (when compared to Sphere) is the:
A.) sub class
B.) super class
C.) upper class
D.) starter class

2. The Circle class defines a getDiameter() method. The Sphere class does not. Yet you can call getDiameter() on a Sphere. This is possible because of:
A.) polymorphism
B.) overriding
C.) overloading
D.) inheritance
E.) casting
F.) an interface

3. The Circle class defines a toString() method. The Sphere class also defines a toString() method. This is an example of:
A.) polymorphism
B.) overriding
C.) overloading
D.) inheritance
E.) casting
F.) an interface

4. The following code:

Circle c = new Circle(3);
System.out.println(c.toString());
prints out "Circle(r=3.0)".

And this code:

Circle c = new Sphere(3);
System.out.println(c.toString());
prints out "Sphere(r=3.0)".

Yet in both cases, I print c.toString() and c is always Circle variable. The output is different in these two cases due to (pick the best answer):
A.) polymorphism
B.) overriding
C.) overloading
D.) inheritance
E.) casting
F.) an interface

5. Can I do this? Why or why not?

  Object c = new Sphere(3.0);

6. Can I do this? Why or why not?

  Sphere s = new Circle(3.0);

7. Can I do this? Why or why not?

  Circle c = new Sphere(3.0);
  System.out.println(c.getVolume());

8. What class is the superclass of all classes in Java?

What to submit

Attach both your Circle.java and your Sphere.java files to an email, and paste your answers in the body before any pasted code.

FAQs

Do you have any examples of this sort of thing?
Here is an example modeling a person: Person.java, Student.java, and PersonTester.java.
I get compile errors because Sphere can't access Circle's radius.
Remember that private variables are not inherited by the subclass. That is, Circle's radius variable still exists, but the Sphere class does not have access to it because it is private. But, not to worry, because Sphere does inherit access to Circle's public getRadius() method, which is all you need.

Grading

Out of 10 points:

1 - Submission and Coding Standards
Follows required submission policies and coding standards.
2 - Compiles
Your Circle and Sphere work with the provided Assignment12.java
1.5 - Circle
All instance variables are private. Has the specified constructor and 5 methods.
1.5 - Sphere
Extends Circle. Has the specified constructor and 3 methods.
4 - Questions


~ztomasze Index : TA Details: ICS111: Assignment 12
http://www2.hawaii.edu/~ztomasze
Last Edited: 31 Mar 2008
©2008 by Z. Tomaszewski.