Circle class, which does everything a wheels
Ellipse does, except that it always has the same width and height.
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 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.
Circle has no instance variables (since
Ellipse already keeps track of everything we need).
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.)
void setDiameter(int diam) -- Setting the diameter changes both the width and height of this
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.
What to Submit
Attach your Circle.java file to an email.
- Example code from lab?
- After adding a few more tests in
- 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
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:
This is one of the rather tricky parts of polymorphism. If you declared
round like this:
Ellipse round = new Circle();
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
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
(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.)
Out of 10 points:
- 1 - Submission
- Follows required submission policies.
- 2 - Coding standards
- 3 - Correct constructors
- 3 - Correct methods
- 1 - Main method as described