Results 1 to 4 of 4
  1. #1
    monkeyhead is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default 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

    Many thanks for any suggestions/ help,
    Matt

    Java 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;
        }
    
    
    }





    Java 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());
          }
        } 
    
      
      
      
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    monkeyhead is offline Member
    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Default Re: New to abstract methods and classes, few issues

    Thank you, Serves me right for copying and pasting....

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default Re: New to abstract methods and classes, few issues

    Quote Originally Posted by monkeyhead View Post
    Thank you, Serves me right for copying and pasting....
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 02:30 PM
  3. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 08:31 AM
  4. Replies: 12
    Last Post: 03-16-2010, 06:45 PM

Posting Permissions

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