Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Triangle { solution should be here } Lab 2: Encapsulating Triangle Data In Lab 1 we wrote a class that consisted of nothing

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

image text in transcribed

public class Triangle {
solution should be here
}
Lab 2: Encapsulating Triangle Data In Lab 1 we wrote a class that consisted of nothing but static methods. In the real world, classes are only occasionally written like this, such as the Math and Arrays classes from the Java API. In contrast, most classes have instance (i.e., non-static) methods and variables. A class with instance variables is a bit like a custom data type. Objects of the class have their own copies of each instance variable. This allows the objects to represent particular examples of whatever category of things the class represents. Consider, for example, the String class, which represents an arbitrary sequence of characters. String objects represent particular character sequences, such as "Java" or "CS 2334" or ":-)". To accomplish this, the String class has a character array instance variable. Each String object has its own copy of the array that it uses to store its unique text. In this lab, we will write a class named "Triangle" that represents any three-sided polygon. Triangle objects represent particular triangles, such as a 3-4-5 right triangle. To accomplish this, the class will have three double instance variables. Each Triangle object will have its own copies of these variables that it uses to store its side lengths. While writing the Triangle 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 the user of the class from creating objects in invalid states. Task List X .......... Find All Activate... (? Triangle.java Triangle Test.java X 1 limport static org. junit.jupiter.api. Assertions. *; 2. 3 import org.junit.jupiter.api. Test; 4. 5 class TriangleTest { 6 70 @Test 8 void testValidConstructor Input1() { 9 double a = 3; 10 double b = 4; 11 double c = 5; 12 Triangle triangle = new Triangle(ambame); x 13 assertEquals(a, triangle.getSideA()); 14 assertEquals(b, triangle.getSideB(); x 15 assertEquals(c, triangle.getSidec()); 16 17 a = 5; 18 b = 3; 19 C = 4; 20 triangle = new Triangle(aambame); Ex 21 assertEquals(a, triangle.getSideA); a 22 assertEquals(b, triangle.getSideB()); 23 assertEquals(c, triangle.getSidec()); 24 25 a = 4; 26 b = 5; 27 C = 3; 28 triangle = new Triangle(a, b, c); x 29 assertEquals(a, triangle.getSideA()); x 30 assertEquals(b, triangle.getSideB()); 31 assertEquals(c, triangle.getSideC(); 32 } 33 340 @Test 35 void testValidConstructorInput2() { 36 double a = 1; 37 double b = 2; 38 double c = Math.sqrt(3); Outline x B 1 - Triangle Test testValidConstructorInput10:void testValidConstructorlnput20: void testinvalidConstructorinput10:void testinvalidConstructorinput20: void testinvalidConstructorinput30: void testValidSetterlnput10:void testValidSetterinput20:void testinvalidSetterlnput10:void testinvalidSetterlnput20:void testToString(: void Task List x 0 000 Find All Activate... ? Triangle.java Triangle Test.java X 37 double b = 2; 38 double c = Math.sqrt(3); 39 Triangle triangle = new Triangle(a, b, c); 40 assertEquals(a, triangle.getSideA()); 41 assertEquals(b, triangle.getSideB()); Ex 42 assertEquals(c, triangle.getSide(); 43 44 a = Math.sqrt(3); 45 b = 1; 46 C = 2; 47 triangle = new Triangle(ambame); 48 assertEquals(a, triangle.getSideA()); 49 assertEquals(b, triangle.getSideB()); x 50 assertEquals(c, triangle.getSide(); 51 52 a = 2; 53 b = Math.sqrt(3); 54 C = 1; C 55 triangle = new Triangle(ambame); 56 assertEquals(a, triangle.getSideA()); 57 assertEquals(b, triangle.getSideB()); Ex 58 assertEquals(c, triangle.getSide(); 59 } 60 610 @Test 62 void testInvalidConstructor Input1() { Ex 63 Triangle triangle = new Triangle(1, 2); assertEquals(1, triangle.getSideA()); 65 assertEquals(1, triangle.getSideB()); 666 assertEquals(1, triangle.getSidec()); 67 268 triangle = new Triangle(2emem1).; Ex 69 assertEquals(1, triangle.getSideA()); C70 assertEquals(1, triangle.getSideB()); 71 assertEquals(1, triangle.getSidec()); 72 73 triangle = newTriangle(1amame); 74 assertEquals(1, triangle.getSideA); BOutline x 61928 - Triangle Test testValidConstructorinput10:void testValidConstructorlnput20: void testinvalidConstructorlnput10:void testInvalidConstructorlnput20: void testinvalidConstructorlnput30: void testValidSetterinput10:void testValidSetterlnput20: void testinvalidSetterlnput10:void testInvalid Setterlnput20: void testToString: void 3 64 Triangle.java Task List X * X X6 | TriangleTest.java x LI LU11511 - usus assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSide(); Find All Activate... ? } @Test void testInvalidConstructor Input2() { Triangle triangle = new Triangle(1,miem3); assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSide(); triangle = new Triangle(3, 1, 1); assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSide(); 3 74 75 276 77 78 790 80 4281 82 13 83 284 85 2 86 x 87 88 x 89 90 91 292 93 93 94 95 96 970 98 99 2100 2101 62102 103 0104 0105 62106 62107 108 109 110 111 triangle = new Triangle(1,3,1); assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSideco); } x @Test void testInvalidConstructor Inputs() { Triangle triangle = new Triangle(1,3cm2); assertEquals(1, triangle.getSideA); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSide(); Outline x 61928 - Triangle Test testValidConstructorinput10:void testValidConstructorlnput20:void testinvalidConstructorlnput10:void testinvalidConstructorinput20: void testinvalidConstructorlnput30: void testValidSetterlnput10:void testValidSetterlnput20:void testinvalidSetterinput10: void testinvalidSetterlnput20:void E testToString: void triangle = new Triangle(2, 1, 3); assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); assertEquals(1, triangle.getSide(); triangle = new Triangle(3,2a 1); assertEquals(1, triangle.getSideA()); assertEquals(1, triangle.getSideB()); Triangle.java Triangle Test.java X 132 assertEquals(newLength, triangle.getSideA); :133 assertEquals(newLength, triangle.getSideB()); 134 assertEquals(c, triangle.getSidec()); 135 02136 assert True(triangle.setSidec(newLength)); 6:137 assertEquals(newLength, triangle.getSideA()); :138 assertEquals(newLength, triangle.getSideB()); 2139 assertEquals(newLength, triangle.getSide()); 140 } 141 1420 @Test 143 void testValidSetter Input2() { 02144 Triangle triangle = new Triangle(56); :145 assertEquals(5, triangle.getSideA()); 02146 assertEquals(6, triangle.getSideB()); 147 assertEquals(7, triangle.getSidec(); 148 02149 assertTrue(triangle.setSideA(12)); 150 assertEquals(12, triangle.getSideA(); #151 assertEquals(6, triangle.getSideB(); 4152 assertEquals(7, triangle.getSide(); 153 92154 assert True (triangle.setSideB(18)); 4155 assertEquals(12, triangle.getSideA(); #156 assertEquals(18, triangle.getSideB()); 2157 assertEquals(7, triangle.getSidec()); 158 42159 assert True (triangle.setSidec (29.99999)); 160 assertEquals(12, triangle.getSideA(); 02161 assertEquals(18, triangle.getSideB()); 02162 assertEquals(29.99999, triangle.getSide(); 163 } 164 1650 @Test 166 void testInvalidsetter Input1() { 167 double a = 2; 168 double b = 4; 169 double c = 5; 0170 Triangle triangle = new Trianglela. b. c): Triangle.java TriangleTest.java X double c = 5; Triangle triangle = new Triangle(a,mbame); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSide(); assertFalse(triangle.setSideA(-100)); assertEquals(a, triangle.getSideA(); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); assertFalse(triangle.setSideB(2)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); 169 4170 02171 02172 0173 174 02175 2176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 1910 192 193 194 195 x196 197 198 199 200 201 202 203 204 205 206 assertFalse(triangle.setSideC(2)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); T I mmm mmm mmm mm mmm mmm mm } @Test void testInvalidsetterInput2() { double a = 7; double b = 3; double c = 9; Triangle triangle = new Triangle(a, b, c); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); assertFalse(triangle.setSideA(b + c)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); assertFalse(triangle.setSideB(a + c)); Triangle.java TriangleTest.java X usserleyuusi, ridugie.gelblue, 100 } @Test void testInvalidSetter Input2() { double a = 7; double b = 3; double c = 9; Triangle triangle = new Triangle(aambame); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSide()); assertFalse(triangle.setSideA(b + c)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); 189 190 1910 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2170 218 3219 220 221 222 223 224 225 } 226 assertFalse(triangle.setSideB(a + c)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); assertFalse(triangle.setSidec(a + b)); assertEquals(a, triangle.getSideA()); assertEquals(b, triangle.getSideB()); assertEquals(c, triangle.getSidec()); } @Test void testToString() { Triangle triangle = new Triangle(1, 1, Math.sort(2)); assertEquals("Triangle(1, 1, 1.414)", triangle.toString()); triangle = new Triangle(4.0/2.0, 3.0/2.0, 4.0/3.0); assertEquals("Triangle(2, 1.5, 1.333)", triangle.toString()); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

=+3. How do you want to affect this reputation?

Answered: 1 week ago

Question

1-4 How will MIS help my career?

Answered: 1 week ago