You need to create a new Triangle
object like this, so that you have a reference
Trangle triangle = new Triangle(side1, side2, side3);
// ^^^^^^ This is the most important thing you're missing. You need a reference
// point for your object. That's the only way you can access it's
// properties.
You also need to set it’s filled
and color
properties
triangle.setFilled(filled);
triangle.setColor(color);
Then, you can invoke its methods like this:
System.out.println("The Triangle Sides are \n side 1: "
+ triangle.getSide1() + "\n Side 2: " + triangle.getSide2()
+ "\n Side 3: " + triangle.getSide3());
System.out.println("The Triangle's Area is " + triangle.getArea());
System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter();
System.out.println("The Triangle's Color is " + triangle.getColor());
System.out.println("Is the Triangle filled? " + triangle.isFilled());
You’re able to access the GeometricObject
‘s isFilled()
, setFilled()
, getColor()
, and setColor()
because a Triangle
is a GeometricObject
(extends
), so it inherits all its methods.
By the way, this is not how to calculate the area of a triangle:
public double getArea() {
return (side1 + side2 + side3) / 2; // This is so wrong
}
Check out this link for correct formula
Edit: With another problem with code
public double setSide1() {
return side1;
}
public double setSide2() {
return side2;
}
public double setSide3() {
return side3;
}
/**** Should Be ******/
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
Edit: Triangle Formula
public double getArea() {
int p = getPerimeter() / 2
return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3));
}