Back to 111 Main Page

Mobius strip

Assignment 6

Task

Write a Circle class, which does everything a wheels Ellipse does, except that it always has the same width and height.

Details:

Wheels

Use the coordwheels.jar version of wheels again for this project. (Use a Frame, rather than a CoordinateFrame though.)

Also remember you can read the wheels documentation.

Circle.java

Write a Circle class that extends Ellipse. It should have the following details:

package wheels.users; -- We're extending wheels here, so we should probably use their package.

A Circle has no instance variables (since Ellipse already keeps track of everything we need).

Constructors (public):

  • Circle() -- Should create a Circle with all the default settings of Ellipse, except ensuring that the height of a new Circle is the same as the width.
  • Circle(int x, int y) -- As Circle() (creates a default ellipse/circle), but at the given location.
  • Circle(int x, int y, int diameter, Color c) -- Creates a Circle with all the given details. (Both its width and height == its diameter.)

Methods (public):

  • void setDiameter(int diam) -- Setting the diameter changes both the width and height of this Circle.
  • void setSize(int width, int height) -- Overrides the setSize method from Ellipse so that a user can't change a Circle's height to be different than its width. Instead, this method should set the diameter (ie, both width and height) to the smaller of the given width and height parameters. (You can use the Math.min(int, int) method to determine which is smaller.)
  • void setSize(Dimension d) -- As for the previous setSize method, only using a Dimension object. (Don't change the contents of the passed Dimension object, though.)

Now write a main method in the Circle class to test your methods.

  • Try creating a Circle in an Ellipse variable:
    Ellipse round = new Circle();
    
  • Call the overridden Ellipse methods on this object.
  • Make sure you can't make the Circle's height different from its width.
  • Create another Circle in a Circle variable, and test setDiameter.

What to Submit

Attach your Circle.java file to an email.

FAQ

Example code from lab?
After adding a few more tests in main: Square.java
Will we get a tester program for this assignment?
There will be no tester for this one. So make sure you closely follow the spelling, case, and parameter types given for the different methods in the assignment specs above. And test each of your constructors and methods from your main method.
I get an error like this when I try to call setDiameter on my Ellipse:
java:48: cannot find symbol
symbol  : method setDiameter(int)
location: class wheels.users.Ellipse
round.setDiameter(150);
     ^
1 error

This error is saying Java can't resolve this call to the method setDiameter(int), even though that method exists (spelled correctly and everything) in Circle. But look at the location Java's looking for it: wheels.users.Ellipse.

This is one of the rather tricky parts of polymorphism. If you declared round like this:

Ellipse round = new Circle();

then round is of type Ellipse. At compile time, Java will only let you do Ellispey things to it--call Ellipse's methods or those of its superclasses. However, at runtime (when your code is actually executing), when Java dereferences the variable to the actual object on the heap, it will discover that round really points to a Circle. At this point, it will instead run any Circle method that overrides the called Ellipse method.

What this means is that you can call round.setSize(30, 50), and the version in Circle will run. But you can't call round.setDiameter(30), because there is no such method for an Ellipse. To test setDiameter, create another Cirle object in a a Circle variable. (Note: Previously, the assignment said create a Ellipse variable and "call both Ellipse and Circle methods on this object". This can be done with casting, but it's probably just easier to create two objects.)

Grading

Out of 10 points:

1 - Submission
Follows required submission policies.
2 - Coding standards
3 - Correct constructors
3 - Correct methods
1 - Main method as described

Solution

Circle.java



~ztomasze Index : TA Details: ICS111: Assignment 6
http://www2.hawaii.edu/~ztomasze
Last Edited: 05 Oct 2007
©2007 by Z. Tomaszewski.