bmw2213
03-04-04, 08:39 PM
Hi, I'm having a little trouble with this script. I know exactly what is going wrong, but I don't know the best way to fix it. Here's the script:
import java.awt.*;
public class Circle {
static double PI = 3.14159;
//Constructors
public Circle(double xCenter, double yCenter, double radius, Color color){
loc = new Point((int)xCenter, (int)yCenter);
this.radius = radius;
this.color = color;
}
//Instance methods
public Point getLoc(){return loc;}
public void setLoc(Point p){loc = p;}
public double getRadius(){return radius;}
public void setRadius(double rad){radius = rad;}
public Color getColor( ){return color;}
public void setColor(Color newColor){color = newColor;}
public double getArea( ) {return PI*radius*radius;}
public void draw(){
//details omitted
}
public boolean equals(Object o){
if (this.getClass() == o.getClass()) {
if ((o.getLoc() == this.getLoc()) && (o.getRadius() == this.getRadius()) && (o.getColor() == this.getColor())) {
return true;
}
}
else
return false;
}
//Instance variables
private Point loc;
private double radius;
private Color color;
}
I get the obvious error that o is not necessarily a circle. What I need to know is, how do I make it run those circle methods if I can't be sure that it's a circle?
import java.awt.*;
public class Circle {
static double PI = 3.14159;
//Constructors
public Circle(double xCenter, double yCenter, double radius, Color color){
loc = new Point((int)xCenter, (int)yCenter);
this.radius = radius;
this.color = color;
}
//Instance methods
public Point getLoc(){return loc;}
public void setLoc(Point p){loc = p;}
public double getRadius(){return radius;}
public void setRadius(double rad){radius = rad;}
public Color getColor( ){return color;}
public void setColor(Color newColor){color = newColor;}
public double getArea( ) {return PI*radius*radius;}
public void draw(){
//details omitted
}
public boolean equals(Object o){
if (this.getClass() == o.getClass()) {
if ((o.getLoc() == this.getLoc()) && (o.getRadius() == this.getRadius()) && (o.getColor() == this.getColor())) {
return true;
}
}
else
return false;
}
//Instance variables
private Point loc;
private double radius;
private Color color;
}
I get the obvious error that o is not necessarily a circle. What I need to know is, how do I make it run those circle methods if I can't be sure that it's a circle?