Answered step by step
Verified Expert Solution
Link Copied!

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:
No-argument constructor: Sets fields to 1
Constructor: Accepts legs of the right triangle as arguments. Constructor throws IllegalArgumentException when one or both legs are set to 0 or negative number(s)
setLegA(), setLegB()- mutator methods. Both methods throw Illegal Argument Exception when leg is set to 0 or negative number.
getLegA(),getLegB()- 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 =3.0, legB =4.0"
Test cases:
import java.io.PrintWriter;
public class TestRightTriangle {
public static boolean tests(PrintWriter outputStream)
{
outputStream.println("\r
----RightTriangle Class TESTS -------------------------------------------------------\r
");
boolean t1= test01TriangleClass(outputStream);
boolean t2= test02TriangleClass(outputStream);
boolean t3= test03TriangleClass(outputStream);
return t1&&t2&&t3;
}
// set, get, throwing exceptions
public static boolean test01TriangleClass(PrintWriter outputStream)
{
int count =0;
int expectedCount =3;
double a1=10.1, b1=15.1; // legs for the set method tests
double aa1=10.1, bb1=15.1; // expected values for set method tests
double a2=-5, b2=-5; // values for testting exceptions
//Test #1
RightTriangle tri1= new RightTriangle();
tri1.setLegA(a1);
tri1.setLegB(b1);
if(tri1.getLegA()==aa1 && tri1.getLegB()== bb1)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setetters and getters", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setetters and getters", "FAILED");
//Test #2
try
{
tri1.setLegA(a2);
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setLegA and exception it must throw", "FAILED");
}
catch (IllegalArgumentException e)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setLegA and exception it must throw", "PASSED");
count++;
}
//Test #3
try
{
tri1.setLegB(b2);
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setLegB and exception it must throw", "FAILED");
}
catch (IllegalArgumentException e)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 01: setLegB and exception it must throw", "PASSED");
count++;
}
if (count==expectedCount) return true;
else return false;
}
// constructors and exceptions
public static boolean test02TriangleClass(PrintWriter outputStream)
{
int count =0;
int expectedCount =2;
double a1=10.5, b1=15.3; // legs for constructor with parameters
double aa1=10.5, bb1=15.3; // expected values to be set by constructor
double aa2=1, bb2=1; // values for testing default constructor
double a3=-51, b3=-8; // values for testing exceptions
//Test #1
RightTriangle tri1= new RightTriangle(a1, b1);
if(tri1.getLegA()==aa1 && tri1.getLegB()== bb1)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: constructor with parameters", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: constructor with parameters", "FAILED");
//Test #2
RightTriangle tri2= new RightTriangle();
if(tri2.getLegA()==aa2 && tri2.getLegB()== bb2)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: no-argument constructor", "PASSED");
count++;
}
else outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: no-argument constructor", "FAILED");
//Test #3
try
{
tri1= new RightTriangle(-1,1);
tri1= new RightTriangle(0,1);
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: constructor exception non-positive first param", "FAILED");
}
catch (IllegalArgumentException e)
{
outputStream.printf("%-80s%-10s
", "RightTriangle TEST SET 02: constructor exception non-positive first pa

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

11. How can you plan for a successful career? (LO 14-8)

Answered: 1 week ago