Question: 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

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, The toString() method is implemented as follows: return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3;

the codes are here, but I don't know how to run it please explain.

//Triangle.java

import java.util.Arrays;

class Triangle extends GeometricObject { double side1; double side2; double side3;

// Default constructor public Triangle() { /*Three double data fields named side1, * side2, and side3 with default values 1.0 */ side1 = 1.0; side2 = 1.0; side3 = 1.0; }

// A constructor that creates //a triangle with the specified side1, //side2, and side3 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; } }

/*The accessor (getters and setters) * methods for all three data fields */ 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; }

/*A method named getArea() * that returns the area of this triangle */ 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; }

/* A method named getPerimeter() * that returns the perimeter of this triangle */ 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; }

}

-------------

//GeometricObject.java

import java.util.*; import java.lang.*; import java.io.*;

public class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;

/** Construct a default geometric object */ public GeometricObject() { dateCreated = new java.util.Date(); }

/** * Construct a geometric object * with the specified color and filled value */ public 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, its getter 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; }

public double getArea() { return 0; }

public double getPerimeter() { return 0; }

/** Return a string representation * of this object */ public String toString() { return " color: " + color + " and filled: " + filled; } }

-------------------

//DriverMain.java

import java.util.*; // class DriverMain public class DriverMain { //main method public static void main(String args[]) { Scanner input = new Scanner(System.in); double side1 = input.nextDouble(); double side2 = input.nextDouble(); double side3 = input.nextDouble(); // Prompt and read color from the user String color = input.next(); boolean filled = input.nextBoolean(); // Create an object for the class Triangle Triangle triangle = new Triangle(side1, side2, side3); triangle.setColor(color); triangle.setFilled(filled);

// Display the output System.out.printf("%.2f", triangle.getArea()); System.out.println(); System.out.printf("%.2f", triangle.getPerimeter()); System.out.println(); System.out.println(triangle); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!