131 Triangle class Design a new Triangle class that extends

**13.1 (Triangle class) Design a new Triangle class that extends the abstract

GeometricObject class. Draw the UML diagram for the classes Triangle

and GeometricObject and then implement the Triangle class. Write a test

program that prompts the user to enter three sides of the triangle, a color, and a

Boolean value to indicate whether the triangle is filled. The program should create

a Triangle object with these sides and set the color and filled properties using

the input. The program should display the area, perimeter, color, and true or false

to indicate whether it is filled or not.

..

I don\'t understand this one. I have the GeometricObject class. I just the rest along with the UML diagram. Please no JOptionPane please.

..

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

/** 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,
so, the get method name is 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 + \"\ color: \" + color +
\" and filled: \" + filled;
}

/** Abstract method getArea */
public abstract double getArea();

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

Solution

// Triangle.java

public class Triangle extends GeometricObject{
   double a, b, c;
   public double getArea() {
       double s = (a + b + c) / 2;
       return Math.sqrt(s * (s - a) * (s - b) * (s - c));
   }

   public double getPerimeter() {
       return a + b + c;
   }
   public Triangle(double a, double b, double c) {
       this.a = a;
       this.b = b;
       this.c = c;
   }
   public String toString(){
       String temp = \"Area: \" + getArea() + \", Perimeter: \" + getPerimeter() + \", Color: \" + getColor() + \", Filled: \" + isFilled();
       return temp;
   }
}

// Test.java

import java.util.Scanner;

public class Test{
   public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       double a, b, c;
       System.out.print(\"Enter the sides of the triangle: \");
       a = in.nextDouble();
       b = in.nextDouble();
       c = in.nextDouble();
       Triangle obj = new Triangle(a, b, c);
       System.out.print(\"Enter the color: \");
       obj.setColor(in.next());
       System.out.println(\"Enter 1 if triangle is filled: \");
       int temp = in.nextInt();
       if(temp == 1) obj.setFilled(true);
       else obj.setFilled(false);
       System.out.println(obj);
   }
}

**13.1 (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and Geometric
**13.1 (Triangle class) Design a new Triangle class that extends the abstract GeometricObject class. Draw the UML diagram for the classes Triangle and Geometric

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site