Question
Run Unit Tests TriangleTest.java import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays;
Run Unit Tests
TriangleTest.java
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import org.junit.jupiter.api.Test; class TriangleTest { // Define the margin of error for double comparisons. private static final double EPSILON = 0.001; // Define triangle data for the tests. private static final double[][] SIDES = { {1, 1, 1}, {-1, -1, -1}, {0, 0, 0}, {4.51, 4.51, 4.51}, {1, 1, Math.sqrt(2)}, {1, 2, 3}, {1, Math.sqrt(3), 2}, {3, 4, 5}, {0, 1, 1}, {5, 6, 10.999}}; private static final double[][] ANGLES = { {60, 60, 60}, null, null, {60, 60, 60}, {45, 45, 90}, null, {30, 60, 90}, {36.870, 53.130, 90}, null, {0.7053, 0.8463, 178.4484}}; private static final boolean[] IS_TRIANGLE = { true, false, false, true, true, false, true, true, false, true}; // Define a helper method to check the output of all side getters. private static void checkSides(Triangle triangle, double[] expected, String message) { assertEquals(expected[0], triangle.getSideA(), EPSILON, message); assertEquals(expected[1], triangle.getSideB(), EPSILON, message); assertEquals(expected[2], triangle.getSideC(), EPSILON, message); assertArrayEquals(expected, triangle.getSides(), EPSILON, message); } // Define a helper method to check the output of all angle getters. private static void checkAngles(Triangle triangle, double[] expected, String message) { assertEquals(expected[0], triangle.getAngleA(), EPSILON, message); assertEquals(expected[1], triangle.getAngleB(), EPSILON, message); assertEquals(expected[2], triangle.getAngleC(), EPSILON, message); assertArrayEquals(expected, triangle.getAngles(), EPSILON, message); } @Test void testIsTriangle() { for (int idx = 0; idx
Triangle.java
public class Triangle { }
In this lab, we will write a class named "Triangle" that represents proper triangles. (A "proper" triangle has positive side lengths and nonzero area.) To accomplish this, the class will have three double instance variables. Each object of the class will have its own copy of these variables that it uses to store the side lengths of a particular triangle. While writing this class, we will contend with the fact that some sets of double values cannot be the lengths of a triangle (e.g., -1, 0, 3). To ensure that each Triangle object represents an actual triangle, we will use encapsulation to prevent objects from being initialized or changed into invalid states. UML Class Diagram The members of the Triangle class are shown in the UML (unified modeling language) diagram below. Triangle -sideA: double -sideB: double -sidec: double +DEFAULT SIDE: double = 1 +Triangle() +Triangle (sideA: double, sideB: double, sidec: double) +Triangle (sides: double[]) +Triangle (triangle: Triangle) +getsideA(): double +getSideb(): double +getsidec(): double +getsides(): double[] +getAngleA(): double +getAngleB(): double +getAnglec(): double +getAngles (): double [] +setSideA (sideA: double): boolean +setSideB (sideB: double): boolean +setSidecisidec: double): boolean +setSides (sides: double[]): boolean +toString(): String +isTriangle (a: double, b: double, c: double): boolean tisTriangle (sides: double[]): boolean +lawofCosines (a: double, b: double, c:double): double Each of the methods in the bottom compartment of the UML diagram is described below. Use these descriptions along with the diagram and JUnit tests to implement the class. Constructors Triangle() : Initialize a new Triangle by assigning DEFAULT_SIDE to each instance variable. Triangle(double siden, double sideb, double sidec) : Initialize a new Triangle by assigning the given values to the instance variables. If any of the following conditions is true, assign DEFAULT_SIDE to each variable instead: o One or more of the values is nonpositive. The values violate the triangle inequality. Triangle(double[] sides) : Initialize a new Triangle by assigning the elements of the given array to the instance variables. Assign element 0 to sideA , 1 to sideb , and 2 to sidec . If any of the following conditions is true, assign DEFAULT_SIDE to each variable instead: o The reference given to the method is null. o The length of the array is not equal to 3. o One or more of the elements is nonpositive. o The elements violate the triangle inequality. Triangle(Triangle triangle) : Initialize a new Triangle by assigning each instance variable of the given Triangle to the corresponding variable of the new Triangle. (This is known as a "copy constructor.") If the reference given to the method is null, assign DEFAULT_SIDE to each variable instead. Side Getters getSide(): Return the length of side A. getSideB(): Return the length of side B. getSidec(): Return the length of side C. getSides(): Return an array with the lengths of all three sides in order from A to C. Angle Getters Each of these methods should return angles in units of degrees. getAngleA() : Return the interior angle that is opposite to side A. getAngleb() : Return the interior angle that is opposite to side B. getAnglec(): Return the interior angle that is opposite to side C. getAngles(): Return an array with the three interior angles in order from A to C. Side Setters setSide A (double sideA) : Assign the given value to side and return true. setSideB (double sideb) : Assign the given value to side and return true. setSideC(double sideB) : Assign the given value to side and return true. For each of the individual side setters ( setSideA, setSideB , setSidec ), if the given value is nonpositive or the resulting triangle would violate the triangle inequality, the method leaves the object unchanged and returns false. setSides (double[] sides): Assign the elements of the given array to the instance variables and return true. (Assign element 0 to sideA, 1 to sideb, and 2 to sidec .) If any of the conditions listed below Triangle(double[] sides) is true, leave the object unchanged and return false. Use these methods to avoid writing duplicate code in the constuctors, side setters, and angle getters. istriangle(double a, double b, double c) : Return true if the given values are positive and satisfy the triangle inequality. Otherwise, return false. isTriangle(double[] sides) : Return true if the given array has three elements that are positive and satisfy the triangle inequality. Otherwise, return false. (If the method is given a null reference, return false.) lawofCosines (double a, double b, double c): Use the law of cosines to calculate the interior angle of a triangle with the given side lengths that is opposite to the side with length c. String Representation toString(): Return the String "Triangle(side, sideb, sidec)" with sides, sides, and sidec replaced with the values assigned to the variables. Round each value to 3 decimal places. There are multiple ways to convert floating-point values to Strings with a given precision. One of the easiest is to use the format method of the String class. See the JUnit tests for examplesStep 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