(The Triangle class ) Design a class named Triangle that extends GeometricObject
ID: 3665322 • Letter: #
Question
(The Triangle class ) 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 Programming Exercise 2.19.
The toString() method is implemented as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
Draw the UML diagrams for the classes Triangle and GeometricObject and
implement the classes. 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.
Explanation / Answer
package com.he.chegg.member;
public class GeometricObject {
private String color;
private boolean filled;
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;
}
}
============================
package com.he.chegg.member;
import java.util.Scanner;
public class Triangle extends GeometricObject {
private double side1;
private double side2;
private double side3;
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 Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle() {
super();
this.side1 = this.side2 = this.side3 = 1;
}
private double getArea() {
double temp = (side1 + side2 + side3) / 2;
return Math.sqrt(temp * (temp - side1) * (temp - side2) * (temp - side3));
}
private double getPerimeter() {
return side1 + side2 + side3;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter side 1 : ");
double side1 = scanner.nextDouble();
System.out.println("Please enter side 2 : ");
double side2 = scanner.nextDouble();
System.out.println("Please enter side 3 : ");
double side3 = scanner.nextDouble();
System.out.println("Please enter color : ");
String color = scanner.next();
System.out.println("Please enter filled / not filled : ");
boolean filled = scanner.nextBoolean();
Triangle tr = new Triangle(side1, side2, side3);
tr.setFilled(filled);
tr.setColor(color);
System.out.println(tr.toString());
}
@Override
public String toString() {
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.