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

What is the Code? Description: In this assignment you’ll write a class that repr

ID: 3586470 • Letter: W

Question

What is the Code?

Description:

In this assignment you’ll write a class that represents a triangle but no test client. The test client is already provided (see below).

Notice that this class has no setters. It is an immutable class. This means that instances of this class can no longer been changed once they have been created.

Instructions:

Create a new project called Triangle. It includes two files called Triangle.java and TriangleApp.java.

The code is provided for TriangleApp.java
Copy the code from the file into the newly created TriangleApp.java.

Add the fields, constructors, and methods as specified in the UML class diagram.
Make sure to match the names, types, parameter lists, access modifiers, etc. exactly as specified.
No class members should be added or removed.

Constructor:
The constructor has 3 parameters to initialize the fields. However, before we can assign the arguments to the fields we need to ensure that they form a valid triangle.
If that is not the case all sides should be initialized with 1d.
Hint: To find out whether the 3 sides form a valid triangle the constructor should call the private method isTriangle ( see below)
FYI: Typically you might throw an exception if the arguments don’t form a valid triangle. We haven’t learned how to use exceptions yet, so for now we just set the values to something appropriate (in our case 1d)

isTriangle:
The method isTriangle determines whether the three argument values form a triangle or not. ( E.g. 3 sides of length 2, 3, and 7 do not form a triangle, nor do -1, 1, and 3. However, 2, 3, and 4 do form a triangle)
Notice that this method is declared private. It is meant for internal use only - to be called by the constructor.

isEquilateral:
The method isEquilateral does not need any parameters. It takes the values of the fields to find out whether the given triangle happens to be an equilateral triangle. It returns the corresponding boolean value.

isRight:
The method isRight also takes the field values to find out whether the given triangle is a right triangle. It returns the corresponding boolean value.  
Caveat: we do not know which of the 3 sides is the longest

Hint 1: Use the logical operators (&&, ||) to implement the methods above.
Hint 2:  The class Triangle should include no print statements.

TriangleApp.java:

public class TriangleApp

{

public static void main(String[] args)

{

double[] side1Values = {3.0, 5.0, 4.0, 4.5, 0.0, 4.0, -

2.0, 1.0, 3.1};

double[] side2Values = {5.0, 4.0, 3.0, 4.5, 0.0, 4.0,

2.0, 2.0, 2.1};

double[] side3Values = {4.0, 3.0, 5.0, 4.5, 0.0, 5.0,

1.0, 3.0, 0.0};

int numberOfTests = side1Values.length;

for(int i = 0; i < numberOfTests; i++) {

Triangle triangle =

new Triangle(side1Values[i], side2Values[i], side3Values[i]);

testTriangle(triangle);

}

}

private static void testTriangle(Triangle t) {

System.out.printf("Triangle (%.1f, %.1f, %.1f) ",

t.getSide1(), t.getSide2(), t.getSide3());

System.out.print(t.isEquilateral() ? "is equilateral" : "");

System.out.print(t.isRight() ? "is right" : "");

System.out.println();

}

}

Expected Output:

Triangle (3.0, 5.0, 4.0) is right
Triangle (5.0, 4.0, 3.0) is right
Triangle (4.0, 3.0, 5.0) is right
Triangle (4.5, 4.5, 4.5) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (4.0, 4.0, 5.0)
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral

Triangle side1 double - side2 double - side3 double +Triangle (side1: double, side2: double, side3 double ) +getSide 1) double + getSide2 (): double +getSide3 (): double - isTriangle (side1 :double, side2 double, side3: double) boolean +isEquilateral (): boolean +isRight 0 boolean

Explanation / Answer

Triangle.java

public class Triangle {
//Declaring instance variables
private double side1;
private double side2;
private double side3;

//Parameterized constructor
public Triangle(double side1, double side2, double side3) {
super();
if (isTriangle(side1, side2, side3)) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
} else {
this.side1 = 1.0;
this.side2 = 1.0;
this.side3 = 1.0;
}
}
//Setters and getters
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;
}

//This method checks whether the triangle is isosceles or not
private boolean isTriangle(double side1, double side2, double side3) {
if (side1 < 0 || side2 < 0 || side3 < 0)
return false;
else if (side2 + side3 > side1 && side1 + side3 > side2 && side1 + side2 > side3)
return true;
else
return false;

}
//This method checks whether the triangle is equilateral or not
public boolean isEquilateral() {
if (side1 == side2 && side2 == side3 && side3 == side1)
return true;
else
return false;

}

public boolean isRight() {
//this condition checks whether the sides form right triangle or not
if (Math.pow(getSide1(), 2) == Math.pow(getSide2(), 2) + Math.pow(getSide3(), 2) || Math.pow(getSide2(), 2) == Math.pow(getSide3(), 2) + Math.pow(getSide1(), 2) || Math.pow(getSide3(), 2) == Math.pow(getSide1(), 2) + Math.pow(getSide2(), 2)) {
return true;
} else {
return false;
}
}

}

_________________

TriangleApp.java

public class TriangleApp {

public static void main(String[] args) {

double[] side1Values = { 3.0, 5.0, 4.0, 4.5, 0.0, 4.0, -2.0, 1.0, 3.1 };

double[] side2Values = { 5.0, 4.0, 3.0, 4.5, 0.0, 4.0, 2.0, 2.0, 2.1 };

double[] side3Values = { 4.0, 3.0, 5.0, 4.5, 0.0, 5.0, 1.0, 3.0, 0.0 };

int numberOfTests = side1Values.length;

for (int i = 0; i < numberOfTests; i++) {

Triangle triangle = new Triangle(side1Values[i], side2Values[i],

side3Values[i]);

testTriangle(triangle);

}

}

private static void testTriangle(Triangle t) {

System.out.printf("Triangle (%.1f, %.1f, %.1f) ", t.getSide1(),

t.getSide2(), t.getSide3());

System.out.print(t.isEquilateral() ? "is equilateral" : "");

System.out.print(t.isRight() ? "is right" : "");

System.out.println();

}

}

__________________

Output:

Triangle (3.0, 5.0, 4.0) is right
Triangle (5.0, 4.0, 3.0) is right
Triangle (4.0, 3.0, 5.0) is right
Triangle (4.5, 4.5, 4.5) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (4.0, 4.0, 5.0)
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral
Triangle (1.0, 1.0, 1.0) is equilateral


_____________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