Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 2: The Triangle Class(need in java and need uml too) Problem Description

ID: 3888127 • Letter: P

Question

Project 2: The Triangle Class(need in java and need uml too)

Problem Description:

Design a class named Triangle that extends GeometricObject. The 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, and side3.

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.

For the formula to compute the area of a triangle, see Exercise 5.19. The toString() method is implemented as follows:

return "Triangle: side1 = " + side1 + " side2 = " + side2 +

" side3 = " + side3;

Draw the UML diagram that involves the classes Triangle and GeometricObject. Implement the class. Write a test program that creates a Triangle object with sides 1, 1.5, 1, color yellow and filled true, and displays the area, perimeter, color, and whether filled or not.

Design:

Draw the UML class diagram here

Explanation / Answer

import java.util.*;

import java.lang.*;

class GeometricObject // base class

{

  

private String color;

private boolean filled;

  

public GeometricObject(String color,boolean filled)//argument constructor

{

this.color = color;

this.filled = filled;

}

  

//accessor methods

public String getColor()

{

return color;

}

public boolean getFilled()

{

return filled;

}

  

}

class Triangle extends GeometricObject

{

  

private double side1,side2,side3;

  

  

public Triangle() //default constructor

{

super("White",false); // calling base class constructor

side1 = 0.0;

side2 = 0.0;

side3 = 0.0;

  

}

  

public Triangle(double side1,double side2,double side3,String color,boolean filled)

{

super(color,filled);//calling base class constructor

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

  

}

  

//accessors methods for sides of triangle

public double getSide1()

{

return side1;

}

public double getSide2()

{

return side2;

}

public double getSide3()

{

return side3;

}

  

// compute area of triangle

public double getArea()

{

double s = (side1+side2+side3)/2;

return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));

}

  

//compute perimeter of triangle

public double getPerimeter()

{

return (side1+side2+side3);

}

public String toString()

{

return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;

}

}

class Test

{

public static void main (String[] args)

{

Triangle t = new Triangle (1,1.5,1,"Yellow",true);

System.out.println("Area of Triangle : "+ t.getArea()+ " square units");

System.out.println("Permeter of Triangle : "+ t.getPerimeter()+ " units");

System.out.println("Color of Triangle : "+ t.getColor());

System.out.println("Filled with color : "+ t.getFilled());

}

}

Output:

Area of Triangle : 0.49607837082461076 square units
Permeter of Triangle : 3.5 units
Color of Triangle : Yellow
Filled with color : true

UML

+getColor (): String

+getFilled() : Boolean

^

|

GeometricObject -color : String - filled:Boolean + GeometricObject(String color,Boolean filled)

+getColor (): String

+getFilled() : Boolean