Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I didn't read all your code but it looks like you made those classes abstract. The shape class should be abstract while everything else should inherit from the abstract shape class. Abstract classes can't be instantiated as the error says.

  3. #3
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    so okay i remove the, abstract from circle and rectangle class. now this is the error i get.

    Circle.java:2: Circle is not abstract and does not override abstract method getArea() in Shape
    public class Circle extends Shape {
    ^
    Rectangle.java:2: Rectangle is not abstract and does not override abstract method getArea() in Shape
    public class Rectangle extends Shape {

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you inherit from an abstract class you have to implement(override) any abstract methods in the concrete classes. In circle and rect you must override the abstract method getArea.

  5. #5
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    I'm wondering what does this instruction mean?

    Modify the Shape.toString() method to also return the area of the shape.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Right now toString returns the color name and the radius. You want to edit the code so it includes the area of the shape.

  7. #7
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    I have the following error:

    Rectangle.java:18: cannot find symbol
    symbol : variable getArea
    location: class Rectangle
    return super.toString() + " with length " + length + " and width " + width + "with area" + getArea;
    ^
    1 error

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The method is getArea() and you want it in the base class(shape)

  9. #9
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    okay i got that part taken care of already.

    * (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.

    I understand what they are asking me what to do, but i don't know how to do it.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I suggest you read up(in regards to modifying toString), that way you really understand what happens. It's quite cool, the abstract class(shape, which cannot be instantiated) has an abstract method(getArea), this method is called in the shapes toString method. You may be wondering how it's possible to call an unimplemented method in a classes method. This method depends on you to subclass and implement the getArea method and when you call toString it then uses this implemented method in the toString. As far as sorting goes(I am assuming you are a college student), it is quite annoying to not be able to use the built in sort method but it's nice to understand the logic of how the methods work. You really need to step through the problem and think about the logic. I will not give you much to go on, and this may be challenging to figure out, but, whats the fun if it were easy? Take some time and think, and do research. Look up what selection sort is, what quick sort is, what insertion sort is, and finally, what bubble sort is. Try and understand how each works and decide what you think would be the most fun (or easy if you want) to implement. You can google and find lots of resources to help you out.

    Let me google that for you

  11. #11
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    thank you, for your timing. i really do appreciate it.

    I understand how selection sort work, but I don't know where I can place the code in in order to make it work.

  12. #12
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    I have the following code below:

    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 selectionSort(double list){
    boolean didSwap = true;

    while (didSwap) {
    didSwap = false;
    for (double i = 1; i < a.length; i++) {
    if (a[i-1] > a[i]) {
    double temp = a[i];
    a[i] = a[i-1];
    a[i-1] = temp;
    didSwap = true;
    }
    }
    }
    }

    public static void main(String[] args) {
    double[] area = {78.53981633974483
    ,15.0,50.26548245743669,12.0};
    sort(area);
    System.out.println(Arrays.toGetArea(area));
    }

    }
    }


    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 = 5; 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);
    printGetArea(shapes);

    ;
    }

    public static void printGetArea(Shape[] shapes) {
    for (int i = 0; i < shapes.length; i++) {
    System.out.println(shapes[i] + ". My area is " + shapes[i].getArea());
    }
    }




    }

    with the error below:



    ShapeSorter.java:51: class, interface, or enum expected
    }
    ^
    ShapeSorter.java:54: class, interface, or enum expected
    public static void sortShapesByArea(Shape[] shapes) {
    ^
    ShapeSorter.java:62: class, interface, or enum expected
    public static boolean isSortedByArea(Shape[] shapes) {
    ^
    ShapeSorter.java:66: class, interface, or enum expected
    }
    ^
    ShapeSorter.java:71: class, interface, or enum expected
    public static boolean testSortShapesByArea(Shape[] shapes) {
    ^
    ShapeSorter.java:73: class, interface, or enum expected
    if (isSortedByArea(shapes) == true) {
    ^
    ShapeSorter.java:75: class, interface, or enum expected
    return true;
    ^
    ShapeSorter.java:76: class, interface, or enum expected
    } else {
    ^
    ShapeSorter.java:78: class, interface, or enum expected
    return false;
    ^
    ShapeSorter.java:79: class, interface, or enum expected
    }
    ^
    ShapeSorter.java:86: class, interface, or enum expected
    public static void printShapes(Shape[] shapes) {
    ^
    ShapeSorter.java:87: class, interface, or enum expected
    for (int i = 5; i < shapes.length; i++) {
    ^
    ShapeSorter.java:87: class, interface, or enum expected
    for (int i = 5; i < shapes.length; i++) {
    ^
    ShapeSorter.java:89: class, interface, or enum expected
    }
    ^
    ShapeSorter.java:96: class, interface, or enum expected
    public static void main(String[] args) {
    ^
    ShapeSorter.java:101: class, interface, or enum expected
    testSortShapesByArea(shapes);
    ^
    ShapeSorter.java:102: class, interface, or enum expected
    printShapes(shapes);
    ^
    ShapeSorter.java:103: class, interface, or enum expected
    printGetArea(shapes);
    ^
    ShapeSorter.java:106: class, interface, or enum expected
    }
    ^
    ShapeSorter.java:108: class, interface, or enum expected
    public static void printGetArea(Shape[] shapes) {
    ^
    ShapeSorter.java:109: class, interface, or enum expected
    for (int i = 0; i < shapes.length; i++) {
    ^
    ShapeSorter.java:109: class, interface, or enum expected
    for (int i = 0; i < shapes.length; i++) {
    ^
    ShapeSorter.java:111: class, interface, or enum expected
    }
    ^

    I would like to know what do i have to do to get everything clear up?

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may be missing a } somewhere, generally when I get that error thats what is happening. Sadly I am quite reliant on the built in sort methods, if I were to sit and think for a while I would be able to give more insight on the correct way to do the sort. Perhaps someone else here will be able to help more in that respect.

  14. #14
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    that's not it, is the coding for the bubble sort that has error,I am just not sure how to fix the problem
    import java.util.Arrays;

    public class BubbleSort {

    public static void sort(int[] a) {
    boolean didSwap = true;

    while (didSwap) {
    didSwap = false;
    for (int i = 1; i < a.length; i++) {
    if (a[i-1] > a[i]) {
    int temp = a[i];
    a[i] = a[i-1];
    a[i-1] = temp;
    didSwap = true;
    }
    }
    }
    }

    public static void main(String[] args) {
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    sort(x);
    System.out.println(Arrays.toString(x));
    }
    }


    The following error:
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:23: sort(int[]) in BubbleSort cannot be applied to (java.lang.String[])
    sort(x);
    ^
    5 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    ----jGRASP exec: javac -g ShapeSorter.java


    ----jGRASP: operation complete.

    ----jGRASP exec: javac -g BubbleSort.java

    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:23: sort(int[]) in BubbleSort cannot be applied to (java.lang.String[])
    sort(x);
    ^
    5 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    ----jGRASP exec: javac -g BubbleSort.java

    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:22: incompatible types
    found : double
    required: java.lang.String
    String[] x = {78.53981633974483,15.0,50.26548245743669,12.0};
    ^
    BubbleSort.java:23: sort(int[]) in BubbleSort cannot be applied to (java.lang.String[])
    sort(x);
    ^
    5 errors

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Check out the errors, they tell you whats wrong. It says found: double, expected: string. Thats your problem.

  16. #16
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    What about that errors?

    BubbleSort.java:23: sort(int[]) in BubbleSort cannot be applied to (java.lang.String[])
    sort(x);
    ^

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The error tells you, read it and you will know what the error is.

  18. #18
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    if sort cannot be applied, what can be applied than?

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    it can be applied, however, only to specific types. In this case, ints, and not strings.

  20. #20
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 02-19-2011, 05:32 AM
  2. Replies: 3
    Last Post: 08-13-2010, 05:57 AM
  3. class is abstract; cannot be instantiated
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 10:16 PM
  4. Replies: 17
    Last Post: 07-12-2009, 07:42 PM
  5. Replies: 2
    Last Post: 11-21-2008, 01:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •