Reputation: 729
I am getting this error and it is confusing me. I do not know how to fix it. I know there are some other errors in there but i'm not too worried about those for now.
Here is the code:
import java.awt.*;
public abstract class Shape {
// Instance variables
private int x;
private int y;
private Color color;
// Constructor
protected Shape(int x, int y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
// Abstract methods
public abstract void draw(Graphics g);
public abstract int getHeight();
public abstract int getWidth();
public abstract void scale();
public abstract double area();
// Other instance methods
public Color getColor() {
return color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void move(int dx, int dy) {
x += dx;
y += dy;
}
public void setColor(Color color) {
this.color = color;
}
public String toString() {
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
} }
Here are the class files:
// Represents a circle that can be displayed in a graphics
// context
import java.awt.*;
public class Circle extends Shape {
// Instance variables
private int diameter;
private double scaleFactor = 2;
private double area;
// Constructor
public Circle(int x, int y, Color color, int diameter) {
super(x, y, color);
this.diameter = diameter;
}
// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillOval(getX(), getY(), diameter, diameter);
}
public int getHeight() {
return diameter;
}
public int getWidth() {
return diameter;
}
public void scale(double scaleFactor) {
(double) diameter *= scaleFactor;
}
public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}
public String toString() {
return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " + getHeight();
}
}
import java.awt.*;
public class Rectangle extends Shape {
// Instance variables
private int width;
private int height;
private double scaleFactor = 2;
private double diameter = width * height;
private double area;
// Constructor
public Rectangle(int x, int y, Color color,
int width, int height) {
super(x, y, color);
this.width = width;
this.height = height;
}
// Instance methods
public void draw(Graphics g) {
g.setColor(getColor());
g.fillRect(getX(), getY(), width, height);
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void scale(double scaleFactor){
width *= scaleFactor;
height *= scaleFactor;
}
public double area() {
area = (3.14 * (diameter * diameter)) / 4;
return area;
}
public String toString() {
return "Diameter: " + diameter + "\nWidth: " + getWidth() + "Height: " +
getHeight();
}
}
And here is the test file:
public class ShapeTest
{
public static void main(String[] args)
{
Color myC = new Color(10, 30, 20);
Circle myCircle = new Circle(0, 0, myC, 5);
Rectangle myRectangle = new Rectangle(0, 0, myC, 2, 3);
System.out.println(myCircle.toString());
System.out.println(myRectangle.toString());
}
}
Here are the errors:
javac ShapeTest.java
ShapeTest.java:5: cannot find symbol
symbol : class Color
location: class ShapeTest
Color myC = new Color(10, 30, 20);
^
ShapeTest.java:5: cannot find symbol
symbol : class Color
location: class ShapeTest
Color myC = new Color(10, 30, 20);
^
./Shape.java:46: ';' expected
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: not a statement
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + g etBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getRed()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getBlue()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Shape.java:46: cannot find symbol
symbol : method getGreen()
location: class Shape
return "Position: (" + getX() + ", " + getY() + ") Color: (" + getRed() + ", " + getBlue() + ", " getGreen() + ")";
^
./Circle.java:16: Circle is not abstract and does not override abstract method scale() in Shape
public class Circle extends Shape {
^
./Circle.java:43: unexpected type
required: variable
found : value
(double) diameter *= scaleFactor;
^
./Rectangle.java:3: Rectangle is not abstract and does not override abstract method scale() in Shape
public class Rectangle extends Shape {
^
10 errors
Upvotes: 3
Views: 51077
Reputation: 4802
The very first issue that i could find is the that you have declared a Scale abstract method with no parameters but when you are extending the abstract class, you are implementing the Scale method with parameters. So that's why this error is thrown in all your sub classes:
Circle is not abstract and does not override abstract method scale() in Shape
Also with the following error:
./Shape.java:46: ';' expected
return "Position: (" + getX() + ", " + getY() + ") Color: (" +
getRed() + getBlue() + ", " getGreen() + ")";
You forgot to add the + sign before getGreen function and Java expects that as the end of line, so that's why the error ';'
the getRed symbol error is because you don't have a function called getRed? and so on!
Upvotes: 1
Reputation: 1519
I agree with user1490835's diagnosis, but I thought it might be good to explain what's going on with this error, just in case you're unsure.
An abstract method is a promise to the compiler that all child classes (ones extending the class with the abstract method) will be able to do that method. Part of fulfilling that promise is that the child classes have to implement the abstract method using the exact same header. So in the Java runtime, you can call the abstract method on an instance of any of the child classes and know you'll have used the correct parameters and so on.
Why? Because if the subclass used a slightly different header for that method (causing the runtime error we're trying to avoid) the code would not have compiled (it would've given the error you're getting).
For example, imagine an abstract class A
that has an abstract method f()
and two concrete subclasses B
and C
. Somewhere in the program we have:
A avar;
if (somevar < othervar) {
avar = new B();
} else {
avar = new C();
}
avar.f();
We don't know wether avar
will be a B
or a C
at runtime, because it depends on the result of the if statement. But we do know that avar
will be a subclass of A
. Since all subclasses of A
must have implemented f
with the correct header (otherwise the programmer would have gotten the compile error you're getting) we know this code won't break while it's running!
In your code, the difference between the abstract method header and its implementation is:
public abstract void scale(); // no parameter
public void scale(double scaleFactor) { // one parameter
To fix it, add the double parameter to your abstract method.
Upvotes: 6
Reputation: 167
Hmm, so the reason it tells you so is, that you inherit from the class "Shape". That class defines the method scale().
So either you implement the method scale() in "Circle" and "Rectangle" or you define them as asbtract. I would propose you just implement the scale() method and don't turn them to abstract since I think that is what you intend to do.
Upvotes: 0
Reputation: 17629
Before you can use a class in Java you have to first import that class.
In ShapeTest.java you need to add:
import Circle;
import Rectangle;
import java.awt.Color;
In Circle.java and Rectangle.java you need to add:
import Shape;
Furthermore, you should also ensure that your files are in a package. See http://en.wikipedia.org/wiki/Java_package
Upvotes: 0