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

Work exercise 13.1, page 529, to design and implement class Triangle from the ab

ID: 3586306 • Letter: W

Question

Work exercise 13.1, page 529, to design and implement class Triangle from the abstract class GeometricObject. Follow the textbook specification for the new class and write a test program.

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.

public class GeometricObject

{

private String color = "Yellow";

private Boolean filled = true;

public String getColor( )

{  

return color;

}

public void setColor(String color)

{  

this.color = color;

}

public boolean isFilled( )

{

return filled;

}

public void setFilled(boolean filled)

{  

this.filled = filled;

}

public String toStrings( )

{  

return " Color: " + color + " and filled: " + filled;

}

}

Explanation / Answer

GeometricObject.java

public class GeometricObject

{

private String color = "Yellow";

private Boolean filled = true;

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}

public String toString() {

return " Color: " + color + " and filled: " + filled;

}

}

_________________

Triangle.java

public class Triangle extends GeometricObject {

double side1;
double side2;
double side3;
/*
* if( side1>side2+side3 || side2>side1+side3 || side3>side1+side2) { side1
* = 1.0; side2 = 1.0; side3 = 1.0; }
*/
// Default no-arg constructor
public Triangle() {
side1 = 1.0;
side2 = 1.0;
side3 = 1.0;
}
// This constructor accepts values
public Triangle(double sideOne, double sideTwo, double sideThree) {
if (sideOne + sideTwo > sideThree && sideOne + sideThree > sideTwo && sideTwo + sideThree > sideOne) {
side1 = sideOne;
side2 = sideTwo;
side3 = sideThree;
} else {
side1 = 1.0;
side2 = 1.0;
side3 = 1.0;
}
}
// Get side methods
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
// Setter methods
public void setSide1(double sideOne) {
side1 = sideOne;
}
public void setSide2(double sideTwo) {
side2 = sideTwo;
}
public void setSide3(double sideThree) {
side3 = sideThree;
}
// Get area method
public double getArea() {
double area;
double p = (side1 + side2 + side3) / 2;
double step1 = (p - side1) * (p - side2) * (p - side3);
double step2 = p * step1;
area = Math.sqrt(step2);
return area;
}
// get perimeter method
public double getPerimeter() {
double perimeter = side1 + side2 + side3;
return perimeter;
}
// A method named toString() that returns a string description for the
// triangle.
public String toString() {
String descriptionString;
descriptionString = "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 + super.toString();
return descriptionString;
}

}

____________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Length of the side#1 :");
double side1 = input.nextDouble();
System.out.print("Enter the Length of the side#2 :");
double side2 = input.nextDouble();
System.out.print("Enter the Length of the side#3 :");
double side3 = input.nextDouble();
System.out.print("Enter the Color :");
String color = input.next();
System.out.print("Filled or not (true/false)? :");
boolean filled = input.nextBoolean();

//Creating an Triangle class object by passing the length of the sides as arguments
Triangle triangle = new Triangle(side1, side2, side3);

//Setting the color and filled instance variable
triangle.setColor(color);
triangle.setFilled(filled);

//Displaying the area and perimeter of the Triangle
System.out.printf("Area of the Triangle : %.2f ", triangle.getArea());
System.out.println();
System.out.printf("Perimeter of the Triangle : %.2f ", triangle.getPerimeter());
System.out.println();
System.out.println(triangle);


}

}

__________________

Output:

Enter the Length of the side#1 :3
Enter the Length of the side#2 :4
Enter the Length of the side#3 :5
Enter the Color :green
Filled or not (true/false)? :false
Area of the Triangle : 6.00

Perimeter of the Triangle : 12.00

Triangle: side1 = 3.0 side2 = 4.0 side3 = 5.0 Color: green and filled: false

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote