Attached is the file GeometricObject.java. Include this in your project, but do not change.
Create a class named Triangle that extends GeometricObject. The class contains:
Create a TriangleTester class with a main method that creates a default triangle object and prints it's description and its area and perimeter. It should also create a second triangle object with side1 = 5, side 2 = 4 and side 3 =10. Print it's description, area and perimeter.
Formula for area of a triangle requires two steps:
double s = (side1 + side2 + side3)/2;
double area = Math.sqrt(s * Math.abs(s-side1) * Math.abs(s-side2) * Math.abs(s-side3));
Perimeter is the sum of the three sides.
NEED MORE HELP CREATING THE TESER CLASS
-----------------------------
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;
}
@Override
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
class Triangle extends GeometricObject { private double side1, side2, side3; public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } public Triangle(double side1, double side2, double side3, String color, boolean filled) { super(color, filled); this.side1 = side1; this.side2 = side2; this.side3 = side3; } public Triangle() { this.side1 = 1; this.side2 = 1; this.side3 = 1; } public double getSide1() { return side1; } public double getSide2() { return side2; } public double getSide3() { return side3; } public double getArea() { double p = (side1 + side2 + side3) / 2; return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); } public double getPerimeter() { return side1 + side2 + side3; } @Override public String toString() { return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 + " filled = " + filled + " color = " + color; } } class TriangleTester { public static void main(String[] args) { Triangle triangle1 = new Triangle(); System.out.println(triangle1); System.out.println("Area: " + triangle1.getArea()); System.out.println("Perimeter: " + triangle1.getPerimeter()); System.out.println(); Triangle triangle2 = new Triangle(5, 4, 10); System.out.println(triangle2); System.out.println("Area: " + triangle2.getArea()); System.out.println("Perimeter: " + triangle2.getPerimeter()); System.out.println(); } }
Get Answers For Free
Most questions answered within 1 hours.