-4

I have to extend the class geometric object to class triangle.

I cannot seem to find the error which causes the compiler to say

"must implement the inherited method GeometricObject.getArea" "must implement the inherited method GeometricObject.getPerimeter"

Here is my code for triangle

public class Triangle extends GeometricObject {



double side1 = 1.0;
double side2 = 1.0;
double side3 = 1.0;

    public Triangle()
    {

    }

    public Triangle(double s1, double s2, double s3)
    {
        double side1 = s1;
        double side2 = s2;
        double side3 = s3;
    }

    public void getSide1()
    {
        System.out.print(side1);
    }

    public void getSide2()
    {
        System.out.print(side2);
    }

    public void getSide3()
    {
        System.out.print(side3);
    }


    public double getArea(double s1, double s2, double s3)
    {
        double s = (s1+s2+s3);
        double area = Math.sqrt((s-s1)-(s-s2)-(s-s3));
        return area;

    }

    public double getPerimeter(double s1, double s2, double s3)
    {
        double peri = (s1+s2+s3);
        return peri;
    }

    public String toString()
    {
        return ("Triangle Side 1: " + side1 + " Triangle Side 2: " + side2 + " Triangle Side 3: " + side3);
    }

}

Here is the code for geometricobject

    public abstract class GeometricObject { 
  private String color = "white"; 
  private boolean filled; 
  private java.util.Date dateCreated;

  /** Construct a default geometric object */ 
  protected GeometricObject() { 
          dateCreated = new java.util.Date(); 
        } 

 /** Construct a geometric object with color and filled value */ 
   protected GeometricObject(String color, boolean filled) { 
        dateCreated = new java.util.Date(); 
        this.color = color; this.filled = filled; 
        }

 /** Return color */ 
    public String getColor() {
         return color; 
     } 

  /** Set a new color */ 
     public void setColor(String color) { 
        this.color = color; } 
     /** Return filled. Since filled is boolean, the get method is named isFilled */ public boolean isFilled() {
           return filled;
           }

        /** Set a new filled */ 
        public void setFilled(boolean filled) { 
             this.filled = filled; 
           } 

        /** Get dateCreated */ 
         public java.util.Date getDateCreated() { 
             return dateCreated; 
           } 

        /** Return a string representation of this object */ 
         public String toString() {
            return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; }

        /** Abstract method getArea and getPerimete */
           public abstract double getArea();    //abstract method
           public abstract double getPerimeter();
        } 

Can someone point out what did I miss?

Yellow_13
  • 39
  • 1
  • 1
  • 10

5 Answers5

2

These two methods in the parent class:

public abstract double getArea();
public abstract double getPerimeter();

...are marked abstract. That means that a subclass must either 1) Implement them, or 2) Declare itself abstract (abstract class Triangle extends GeometricObject).

Note that neither of them declares any parameters. The getArea and getPerimeter you have in Triangle take parameters (three doubles), and so they don't provide an implementation of the two methods above, which don't.

To be non-abstract, Triangle must have:

@Override
public double getArea() {
    // ...implementation...
}
@Override
public double getPerimeter() {
    // ...implementation...
}

Note that those, unlike the ones you currently have, don't accept any parameters.

There's no need for parameters on your methods; instead, use your instance fields side1, side2, and side3:

@Override
public double getArea()
{
    double s = this.side1 + this.side2 + this.side3;
    double area = Math.sqrt((s - this.side1) - (s - this.side2) - (s - this.side3));
    return area;
}

@Override
public double getPerimeter()
{
    double peri = this.side1 + this.side2 + this.side3;
    return peri;
}
Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

In your child class, you have not overridden the abstract methods called getArea() and getParimeter() from GeometricObject class. You have simply overloaded them ( getArea() and getPermission() methods ) in the child class, for instance check the prototypes of the corresponding methods in the two classes as they are not identical. Define getArea() [and getparimeter() ] method as follows (in the child class) :

public double getArea(){
  // your code goes here... 

  }

OR again declare them abstract as follows:

   public abstract double getArea();

but then you will have to make your child class abstract which will cost you that you won't be able to create it's ( child class' ) object.

log0
  • 2,206
  • 2
  • 14
  • 24
0

Your getArea() method is abstract. Thus, it should be have a definition somewhere. This is because when you declare a method as abstract, you cannot have a body for it, in the class in which it is declared. Hence, in the sub-class you need to provide that.

Abstract methods are more to ensure that all the sub-classes are following the same signature for that method, even though each sub- class' definition for that may be different.

So alter your arguments so that the signatures match.

So where you have declared getArea() as abstract, add the 3 double arguments that you need, in the signature.

i.e. Your declaration should be:

public abstract double getArea(double s1,double s2,double s3);

0

You don't override but rather overload getArea and getPerimeter - Overriding means implementing the method according to the specifications of the super class, Overloading means extending a method with parameters but keeping the name and return type the same as the overloaded method.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

or you need to add parameters to your abstract methods in the abstract class:

public abstract double getArea(double side1, double side2, double side3);

    //abstract method
public abstract double getPerimeter(double side1, double side2, double side3);
Chris Wilson
  • 135
  • 3
  • 16