In: Computer Science
In Java
Design a Triangle class (Triangle.java) that extends GeometricObject. Draw the UML diagram for both classes and implement Triangle. The Triangle class contains: ▪ Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. ▪ A no-arg constructor that creates a default triangle. ▪ A constructor that creates a triangle with the specified side1, side2, side3, color, and filled arguments. ▪ The accessor methods for all three data fields. ▪ A method named getArea() that returns the area of this triangle. ▪ A method named getPerimeter() that returns the perimeter of this triangle. ▪ A method named toString() that returns a string description for the triangle. return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; The formulas to compute the area of a triangle are as follows: ? = (????1 + ????2 + ????3) 2 ???? = √?(? − ????1)(? − ????2)(? − ????3) The implementation for the Triangle’s toString() method is as follows: return "Triangle:\n" + super.toString() + "\nTriangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; Download the attached project: HomeworkCh11.zip. Complete the HomeworkCh11.java test program as follows 1. Prompt the user to enter a. Each of the three sides of a triangle, b. the Triangle’s color, and c. whether the triangle is filled. 2. The program should create a Triangle object with these sides and set the color and filled properties using the input. 3. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not. 4.
Create an ArrayList and add at least one of each of the following objects to it: Triangle, String, Rectangle, Date, Circle. The list should contain at least seven objects.
The implementation for instantiating the ArrayList and adding objects to it is as follows: ArrayListlist = new ArrayList(); list.add(new Circle(1.5, "green", true)); list.add(new Date()); 5. Create a loop to display all the elements in the list by invoking its toString method. Use the instanceof operator to invoke the correct getArea() and getPerimeter() methods to display additional information about each shape object.
public class Triangle extends GeometricObject { // Three Double fields of type double. You can use Double class or primitive type double. double side1 = 1.0; // Default value is 1.0 so gave it while initializing. There are other places too where you can do it like init blocks and constructors but this is simple and easy way. double side2 = 1.0; double side3 = 1.0; String color; boolean isFilled; // No Arg Constructor public Triangle() { } public Triangle(double side1, double side2, double side3, String color, boolean isFilled) { this.side1 = side1; this.side2 = side2; this.side3 = side3; this.color = color; this.isFilled = isFilled; } // Returns Area of a Triangle public double getArea(){ double s = side1 + side2 + side3; double side1Diff = s - side1; double side2Diff = s - side2; double side3Diff = s - side3; return (Math.sqrt(s * side1Diff * side2Diff * side3Diff))/2; } // Returns perimeter of this triangle public double getPerimeter(){ return side1+side2+side3; } @Override public String toString() { return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; } // Accessor methods for three data fields. public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } public String getColor() { return this.color; } public boolean isFilled() { return this.isFilled; } }
import java.util.Scanner; public class TriangleTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Scanner Object to take input from user. System.out.println("Enter First Side of a Triangle = " ); double side1 = sc.nextDouble(); System.out.println("Enter Second Side of a Triangle = " ); double side2 = sc.nextDouble(); System.out.println("Enter Third Side of a Triangle = " ); double side3 = sc.nextDouble(); System.out.println("Enter Color of a Triangle = " ); String color = sc.next(); System.out.println("Is Triangle Filled true/false"); boolean isFilled = sc.nextBoolean(); // Create triangle object with the information provided by user. Triangle triangle = new Triangle(side1,side2,side3,color,isFilled); System.out.println("Area = " + triangle.getArea()); System.out.println("Perimeter = " + triangle.getPerimeter()); System.out.println("Color = " + triangle.getColor()); System.out.println("Is Filled = " + triangle.isFilled()); } }
Last part of the question seems related to some other information not provided above.
UML is attached.