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

*********Instructions*************** Design a class named Triangle that extends

ID: 3871144 • Letter: #

Question

*********Instructions***************

Design a class named Triangle that extends provided 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.

Note : first determine whether three lengths form a triangle. If not, just use default values for all the sides.

The accessor(getters and setters) 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.

The toString() method is implemented as follows:

                         return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 + super.toString();

The test program, 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.

Input1

2

2

2

red

true

output1

1.73

6.00

Triangle: side1 = 2.0 side2 = 2.0 side3 = 2.0 color: red and filled: true

Input2

22

3

5

green

false

output2

0.43

3.00

Triangle: side1 = 1.0 side2 = 1.0 side3 = 1.0 color: green and filled: false

**********My Specific Problem**********

The code written beneath contains DriverMain (cannot be modified), the GeometricObject class, and the Triangle class. I have the program working fine as long as the input for the sides of the triangle actually make a triangle. If they do not make a triangle (ex. side1=3, side2=4, side3=50) then my program is supposed to default to an equilateral triangle of sides equal to 1 and from there return the output.

What I need is help on what to do when the side lengths do not make a triangle. (How to default to 1).

*********DriverMain Class*****************

import java.util.*;

public class DriverMain {

public static void main(String args[]){

  

Scanner input = new Scanner(System.in);

  

double side1 = input.nextDouble();

double side2 = input.nextDouble();

double side3 = input.nextDouble();

  

String color = input.next();

  

boolean filled = input.nextBoolean();

  

Triangle triangle = new Triangle(side1, side2, side3);

  

triangle.setColor(color);

triangle.setFilled(filled);

  

System.out.printf("%.2f",triangle.getArea());

System.out.println();

System.out.printf("%.2f",triangle.getPerimeter());

System.out.println();

System.out.println(triangle);

}

}

********GeometricObject Class*************

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;

}

}

************Triangle Class**************

import java.util.Arrays;

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) {

side1 = sideOne;

side2 = sideTwo;

side3 = sideThree;

}

//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;

}

}

Explanation / Answer

Note : Could u please check the results..If u want me to do any modifications.I will do it.Thank You

________________

Modification:

We have to validate the three sides forming a triangle or not by writing the code inside the constructor.If the sides are not forming triangle.then we have to assign 1.0 to all three sides.For the modified code.Just Check Triangle class parameterized constructor.

_____________

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;
}
}

__________________

Triangle.java

import java.util.Arrays;

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;
}

}

_____________________

DriverMain.java

import java.util.*;

public class DriverMain {
public static void main(String args[]) {

Scanner input = new Scanner(System.in);

double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();

String color = input.next();

boolean filled = input.nextBoolean();

Triangle triangle = new Triangle(side1, side2, side3);

triangle.setColor(color);
triangle.setFilled(filled);

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

____________________

Output:

22
3
5
green
false
0.43
3.00
Triangle: side1 = 1.0 side2 = 1.0 side3 = 1.0 color: green and filled: false


_____________Could you rate me well.Plz .Thank You