I really want to understand this triangle problem in sense of testing. my questions are below.
1. Complete the reportTriangleType method in the Triangle class below. Given three integer
arguments as the three angles of a triangle, reportTriangleType returns the type of the
triangle, scalene, isosceles, or equilateral.
public class Triangle {
public static String reportTriangleType(int angleA, int angleB, int angleC) {
…
}
}
2. Design a test suite for the above program using equivalence partitioning and boundary value
analysis.
• Use a table to list all valid and invalid classes.
• Use a table to list all of your test cases to cover the valid and invalid classes. Each test
case should include both input and expected output.
3. Write a class TriangleTest to test reportTriangleType with all the test cases you have
designed. Fix the bugs, if any. You may use JUnit or NUnit.
For example, the test case whose input is (60, 60, 60) and expected result is "equilateral"
can be translated into assertEqual(Triangle.reportTriangleType(60, 60, 60), "equilateral") or
if (!Triangle.reportTriangleType(60, 60, 60).equalsIgnoreCase("equilateral"))
System.out.println("test failed");
4. For each conditional statement in your program, determine whether or not the test cases
you designed for (2) have achieved branch coverage and multi-condition coverage
Thanks for your help!
Hemant SrivastavaPosted Sep 24, 2013, 2:12 PM
I would code like this.. (slightly modified version of your code)
public class TriangleTester
{
///
/// Given the angle a, b, and c, determine and return
/// what type of triangle describe, or whether the input is invalid
///
/// Angle a
/// Angle b
/// Angle c
///
public static TriangleType GetTriangleType(int a, int b, int c)
{
// Set an error first (default value)
TriangleType resultantType = TriangleType.Error;
//Placing angles in an array for processing
int[] values = new int[3] { a, b, c };
// Each angle must be poitive and their sum should be 180 degree
if ((a > 0 && b > 0 && c > 0) && (a + b + c) == 180)
{
// All angles are same therefore all the sides (opposite to corresponding angle) should be
// of same sizes. That means 'Equilateral Triangle'
if (values.Distinct().Count() == 1)
{
resultantType = TriangleType.Equilateral;
}
// Two of the angles are same therefore two of the sides (opposite to corresponding angle) should be
// of same sizes. That means 'Isosceles Triangle'
else if (values.Distinct().Count() == 2)
{
resultantType = TriangleType.Isosceles;
}
// All angles are different therefore all the sides (opposite to corresponding angle) should be
// of different sizes. That means 'Scalene Triangle'
else if (values.Distinct().Count() == 3)
{
resultantType = TriangleType.Scalene;
}
}
return resultantType;
}
}
RAHUL goswamiPosted Sep 24, 2013, 12:47 PM
public class TriangleTester
{
///
/// Given the side lengths a, b, and c, determine and return
/// what type of triangle the lengths describe, or whether
/// the input is invalid
///
/// length of side a
/// length of side b
/// length of side c
///The triangle type based on the number of matching sides passed in.
public static TriangleType GetTriangleType(int a, int b, int c)
{
//Placing items in an array for processing
int[] values = new int[3] {a, b, c};
// keeping this as the first check in case someone passes invalid parameters that could also be a triangle type.
//Example: -2,-2,-2 could return Equilateral instead of Error without this check.
//We also have a catch all at the end that returns Error if no other condition was met.
if (a <= 0 || b <= 0 || c <= 0)
{
return TriangleType.Error;
}
else if (values.Distinct().Count() == 1) //There is only one distinct value in the set, therefore all sides are of equal length
{
return TriangleType.Equilateral;
}
else if (values.Distinct().Count() == 2) //There are only two distinct values in the set, therefore two sides are equal and one is not
{
return TriangleType.Isosceles;
}
else if (values.Distinct().Count() == 3) // There are three distinct values in the set, therefore no sides are equal
{
return TriangleType.Scalene;
}
else
{
return TriangleType.Error;
}
}
}