In: Computer Science
in java please
Project 2: The Triangle Class
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;
Coding: (Copy and Paste Source Code here)
public class TheTriangleClass {
public static void main(String[] args) {
Triangle triangle = new Triangle(1, 1.5, 1);
triangle.setColor("yellow");
triangle.setFilled(true);
System.out.println(triangle);
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println(triangle);
}
}
class Triangle extends GeometricObject {
// Implement it (See below for a complete example, the example is for Rectangle)
}
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();
}
/** Construct a geometric object with color and filled value
*/
protected GeometricObject(String color, boolean filled)
{
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** 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,
29 * the get method is named 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;
}
@Override
public String toString()
{
return "created on " + dateCreated + "\ncolor: " + color + " and
filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
Program code:
Output:
UML diagram that involves the classes Triangle and GeometricObject:
Code to copy:
GeometricObject.java
public
class GeometricObject {
private String color;
private boolean filled;
public GeometricObject() {
color = "white";
filled = true;
}
public GeometricObject(String color, boolean filled)
{
this.color = color;
this.filled = filled;
}
/**Getter method for color*/
public String getColor() {
return color;
}
/**Setter method for color*/
public void setColor(String color) {
this.color = color;
}
/**Getter method for filled. Since filled is
boolean,
* so, the get method name is isFilled*/
public boolean isFilled() {
return filled;
}
/**Setter method for filled*/
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Return a string representation of this
object*/
public String toString() {
return "Color: " + color + " and
filled: " + filled;
}
}
Triangle.java
public class Triangle extends GeometricObject
{
/*declaration of three double data
fields named
* side1, side2, and side3 with
default values 1.0*/
double side1 = 1.0;
double side2 = 1.0;
double side3 = 1.0;
/*A no-arg constructor that creates
a default triangle.*/
public Triangle() {
super();
side1 =
0.0;
side2 =
0.0;
side3 =
0.0;
}
/*A constructor that creates
a rectangle with the specified sides.*/
public Triangle(double a,
double b, double c, String color, boolean filled) {
super(color,
filled);
side1 = a;
side2 = b;
side3 = c;
}
/*getter method for
side1*/
public double getSide1() {
return
side1;
}
/*setter method for side1*/
public void setSide1(double side1)
{
this.side1 =
side1;
}
/*getter method for side2*/
public double getSide2() {
return
side2;
}
/*Setter method for side2*/
public void setSide2(double side2)
{
this.side2 =
side2;
}
/*getter method for side3*/
public double getSide3() {
return
side3;
}
/*setter method for side3*/
public void setSide3(double side3)
{
this.side3 =
side3;
}
public void show() {
System.out.println(side1+","+side2+","+side3+",");
}
/* method to calculate area of
triangle */
public double getArea(){
double
s=(side1+side2+side3)/2;
double area=s*(s-side1)*(s-side2)*(s-side3);
return area;
}
/* method to calculate perimeter of
triangle */
public double getPerimeter(){
return (side1 +
side2 + side3);
}
/*toString method that returns
string representing sides of triangle*/
public String toString(){
return
"Triangle: side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3
+" "+
super.toString();
}
}
Test program Asg1.java
/*test program that creates a Triangle object
with
* sides 1, 1.5, 1, sets color yellow and filled
true*/
public class Asg1 {
public static void main(String[]
args) {
//create an object of Triangle
class
Triangle my =
new Triangle(1,1.5,1,"yellow", true);
//Call method
getArea() to get the area of Triangle
System.out.println("Area of Triangle " +my.getArea());
//Call method
getPerimeter() to get the perimeter of Triangle
System.out.println("Perimeter of triangle "
+my.getPerimeter());
//call getter
method getColor()
System.out.println("color of the Triangle is "
+my.getColor());
//call getter
method isFilled()
System.out.println("is triangle filled "
+my.isFilled());
}
}
Note:
Please give me a positive rating thank you:)