Circle is abstract; cannot be instantiated
Hi , I am having problem with the following error:
ShapeSorter.java:80: Circle is abstract; cannot be instantiated
Shape[] shapes = {new Circle("red", "c", 5),
^
ShapeSorter.java:81: Rectangle is abstract; cannot be instantiated
new Rectangle("blue", "r", 3, 5),
^
ShapeSorter.java:82: Circle is abstract; cannot be instantiated
new Circle("green", "c2", 4),
^
ShapeSorter.java:83: Rectangle is abstract; cannot be instantiated
new Rectangle("yellow", "r2", 2, 6)};
^
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Below are the 4 classes that I am working with
public abstract class Shape {
String color;
String name;
public Shape(String c, String n) {
color = c;
name = n;
}
public String getColor() {
return color;
}
public String getName() {
return name;
}
public String toString() {
return "I'm a " + color + " shape named " + name;
}
public abstract double getArea();
}
public abstract class Rectangle extends Shape {
double length;
double width;
public Rectangle(String c, String n, double l, double w) {
super(c, n); // Call Shape's constructor.
length = l;
width = w;
}
public void print() {
System.out.println("I'm a " + color + " rectangle named " + name +
" with length " + length + " and width " + width);
}
public String toString() {
return super.toString() + " with length " + length + " and width " + width;
}
}
double radius;
public Circle(String c, String n, double r) {
super(c, n);
radius = r;
}
public void print() {
System.out.println("I'm a " + color + " circle named " + name + " with radius " + radius);
}
public String toString() {
return super.toString() + " with raidus " + radius;
}
}
/**
* (1) Add an abstract method: double getArea() to the Shape class
*
* (2) Implement the getArea method in Circle and Rectangle, like we did in
* class.
*
* (3) Modify the Shape.toString() method to also return the area of the shape.
*
* (4) Implement ShapeSorter.sortShapesByArea, which sorts the shape in
* increasing order of area. You may use bubbleSort, insertionSort, or
* selectionSort. You may NOT use a built-in sorting function (like
* Arrays.sort). Modify the Shape.toString() method to also print the area of
* the Shape.
*
* (5) Implement the ShapeSorter.isSortedByArea method, which returns true if
* the shapes array is already sorted in increasing order of area.
*
* When you run the main method, the test should pass, and all the shapes should
* be printed to the screen.
*
*
*/
public class ShapeSorter {
/**
* Sort the accounts array in order of increasing balance.
*
* Use bubbleSort, insertionSort, or selectionSort, all of which are in your
* book.
*/
public static void sortShapesByArea(Shape[] shapes) {
// TODO: Finish me.
}
//abstract get area
/**
* Return true if the shapes array is correctly sorted in increasing order of area.
*/
public static boolean isSortedByArea(Shape[] shapes) {
// TODO: Finish me.
return true;
}
/**
* You don't need to edit this method. It's done.
*/
public static boolean testSortShapesByArea(Shape[] shapes) {
sortShapesByArea(shapes);
if (isSortedByArea(shapes) == true) {
System.out.println("sort passes!");
return true;
} else {
System.out.println("sort fails!");
return false;
}
}
/**
* This one's done already, too.
*/
public static void printShapes(Shape[] shapes) {
for (int i = 0; i < shapes.length; i++) {
System.out.println(shapes[i]);
}
}
/**
* This one's done. The test should pass.
*/
public static void main(String[] args) {
Shape[] shapes = {new Circle("red", "c", 5),
new Rectangle("blue", "r", 3, 5),
new Circle("green", "c2", 4),
new Rectangle("yellow", "r2", 2, 6)};
testSortShapesByArea(shapes);
printShapes(shapes);
}
}
I am wondering what i can do to solve the error.