New to abstract methods and classes, few issues
Hi there basically I'm having a few issues with this the circle class works fine as I've tried it in a test class. But the problem lies with when I try and inherit the circle get radius method in the comparing class. I keep getting the error "cannot invoke getRadius() from Circle". I'm not quite sure where I'm going wrong here and spent a few hours on it. The only thing I've managed to overload so far is my brain :P:
Many thanks for any suggestions/ help,
Matt
Code:
public class Circle extends GeometricObject {
private double radius;
public Circle() {
this(1.0);
}
public Circle(double radius) {
this(radius, "white", false);
}
public Circle(double radius, String colour, boolean filled) {
super(colour, filled);
this.radius = radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius(double radius) {
return this.radius;
}
public double findArea() {
return radius * radius * Math.PI;
}
public double findPerimeter() {
return 2 * radius * Math.PI;
}
public String toString() {
return "Circle radius " + radius;
}
}
Code:
public class ComparableCircle extends Circle implements Comparable
{
public ComparableCircle(double radius)
{
super(radius);
}
public int compareTo(Object o)
{
if (super.getRadius() > ((Circle)o).getRadius())
{
return 1;
}
else if (super.getRadius() < ((Circle)o).getRadius())
{
return -1;
}
else
{
return 0;
}
}
public static void main(String[] args)
{
ComparableCircle circle1 = new ComparableCircle(6.0);
ComparableCircle circle2 = new ComparableCircle(5.0);
if (circle1.compareTo(circle2) > 0)
{
System.out.println("The larger radius is " + circle1.getRadius(radius));
}
else
{
System.out.println("The larger radius is " + circle2.getRadius());
}
}
}
Re: New to abstract methods and classes, few issues
For some reason (which is beyond me), your getRadius( ... ) method takes a parameter of type double ... but so be it and you have to call that method with a double typed parameter.
kind regards,
Jos
Re: New to abstract methods and classes, few issues
Thank you, Serves me right for copying and pasting.... :(blush):
Re: New to abstract methods and classes, few issues
Quote:
Originally Posted by
monkeyhead
Thank you, Serves me right for copying and pasting.... :(blush):
The trouble with copying and pasting code is that you really have to scrutinize the code and completely understand every gory detail of it before you can run it; otherwise it makes you a religious person (just hope for the best and believe the code is correct). Science doesn't work like that ...
kind regards,
Jos