Answered step by step
Verified Expert Solution
Question
1 Approved Answer
RightTriangle Class Write a RightTriangle class that has the following private fields: legA: a double legB: a double The class should have the following public
RightTriangle Class
Write a RightTriangle class that has the following private fields:
legA: a double
legB: a double
The class should have the following public methods:
Noargument constructor: Sets fields to
Constructor: Accepts legs of the right triangle as arguments. Constructor throws IllegalArgumentException when one or both legs are set to or negative numbers
setLegA setLegB mutator methods. Both methods throw Illegal Argument Exception when leg is set to or negative number.
getLegAgetLegB accessor methods.
getHypotenuse calculates and returns hypotenuse of the triangle.
getArea calculates and returns area of the right triangle
getPerimeter calculates and returns perimeter of the triangle
toString method that represents object as a string in the following format: "legA legB
Test cases:
import java.ioPrintWriter;
public class TestRightTriangle
public static boolean testsPrintWriter outputStream
outputStream.printlnr
RightTriangle Class TESTS r
;
boolean t testTriangleClassoutputStream;
boolean t testTriangleClassoutputStream;
boolean t testTriangleClassoutputStream;
return t&&t&&t;
set, get, throwing exceptions
public static boolean testTriangleClassPrintWriter outputStream
int count ;
int expectedCount ;
double a b; legs for the set method tests
double aa bb; expected values for set method tests
double a b; values for testting exceptions
Test #
RightTriangle tri new RightTriangle;
trisetLegAa;
trisetLegBb;
iftrigetLegAaa && trigetLegB bb
outputStream.printfss
"RightTriangle TEST SET : setetters and getters", "PASSED";
count;
else outputStream.printfss
"RightTriangle TEST SET : setetters and getters", "FAILED";
Test #
try
trisetLegAa;
outputStream.printfss
"RightTriangle TEST SET : setLegA and exception it must throw", "FAILED";
catch IllegalArgumentException e
outputStream.printfss
"RightTriangle TEST SET : setLegA and exception it must throw", "PASSED";
count;
Test #
try
trisetLegBb;
outputStream.printfss
"RightTriangle TEST SET : setLegB and exception it must throw", "FAILED";
catch IllegalArgumentException e
outputStream.printfss
"RightTriangle TEST SET : setLegB and exception it must throw", "PASSED";
count;
if countexpectedCount return true;
else return false;
constructors and exceptions
public static boolean testTriangleClassPrintWriter outputStream
int count ;
int expectedCount ;
double a b; legs for constructor with parameters
double aa bb; expected values to be set by constructor
double aa bb; values for testing default constructor
double a b; values for testing exceptions
Test #
RightTriangle tri new RightTrianglea b;
iftrigetLegAaa && trigetLegB bb
outputStream.printfss
"RightTriangle TEST SET : constructor with parameters", "PASSED";
count;
else outputStream.printfss
"RightTriangle TEST SET : constructor with parameters", "FAILED";
Test #
RightTriangle tri new RightTriangle;
iftrigetLegAaa && trigetLegB bb
outputStream.printfss
"RightTriangle TEST SET : noargument constructor", "PASSED";
count;
else outputStream.printfss
"RightTriangle TEST SET : noargument constructor", "FAILED";
Test #
try
tri new RightTriangle;
tri new RightTriangle;
outputStream.printfss
"RightTriangle TEST SET : constructor exception nonpositive first param", "FAILED";
catch IllegalArgumentException e
outputStream.printfss
"RightTriangle TEST SET : constructor exception nonpositive first pa
Step 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