Question
What is the Code? Description: In this assignment youll write a class that represents a triangle but no test client. The test client is already
What is the Code?
Description:
In this assignment youll 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 dont form a valid triangle. We havent 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
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 booleanStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started