Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write a program that takes lengths of three sides of a triangle and determines if the triangle is right ( one of its angles is

Write a program that takes lengths of three sides of a triangle and determines if the triangle is right (one of its angles is 90 degrees).. Start coding with RightTriangle.java provided. Keep in mind that lengths of the triangle sides can be entered in any possible order.
Your goal is to determine the largest side and check if the other 2 sides fit the Pythagorean theorem equation. See Pythagorean theorem here
Example: Determine if a Triangle is a Right Triangle Given the Length of 3 SidesLinks to an external site.
Example: Determine if a Triangle is a Right Triangle Given the Length of 3 Sides
If the triangle is a right one, output must contain word RIGHT in all caps and the Pythagorean theorem equation as an explanation, see samples below:
Sample run #1:
Enter the size of the first side of the triangle =>3
Enter the size of the second side of the triangle =>5
Enter the size of the third side of the triangle =>4
This is a RIGHT triangle because 5.0^2=3.0^2+4.0^2
Sample run #2:
Enter the size of the first side of the triangle =>4
Enter the size of the second side of the triangle =>3
Enter the size of the third side of the triangle =>5
This is a RIGHT triangle because 5.0^2=3.0^2+4.0^2
If the triangle is not a right one, output must contain word NOT in all caps, see sample below:
Sample run #3:
Enter the size of the first side of the triangle =>2
Enter the size of the second side of the triangle =>3
Enter the size of the third side of the triangle =>5
This is NOT a right triangle.
In addition, your program must prevent zero and negative values to be used for calculations. When non-positive number is entered, the program must quit right away ;) The error message must contain word ERROR in all caps. See samples below:
Sample run #4:
Enter the size of the first side of the triangle =>-1
ERROR: Side size must be a positive number! Quitting...
Sample run #5:
Enter the size of the first side of the triangle =>1
Enter the size of the second side of the triangle =>0
ERROR: Side size must be a positive number! Quitting...
Sample run #6:
Enter the size of the first side of the triangle =>1
Enter the size of the second side of the triangle =>2
Enter the size of the third side of the triangle =>-3
ERROR: Side size must be a positive number! Quitting...
Test cases
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.regex.Pattern;
public class TestRightTriangle {
public static boolean tests(PrintWriter outputStream)
{
int count =0;
int expectedCount =8;
outputStream.print("\r
----Tests for RightTriangle --------------------------------------------------------\r
");
// Test 1//
outputStream.print("\r
____Test 01____________________________________________________________________________\r
");
String sep = System.lineSeparator();
String userInput = String.format("-5.0%s3%s4", sep, sep);
String expectedOutput ="(?si).*?ERROR.*?";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
PrintStream standard = System.out;
ByteArrayInputStream bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.setOut(printStream);
RightTriangle.main(null);
//System.out.println("End of Test");
//String[] lines = baos.toString().split(System.lineSeparator());
String actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "RightTriangle TEST 01- first value negative", "PASSED");
count++;
}
else
{
outputStream.printf("%-80s%-10s\r
", "RightTriangle TEST 01- first value negative", "FAILED");
}
// Test 2//
outputStream.print("\r
____Test 02____________________________________________________________________________\r
");
userInput = String.format("5.0%s-33.3%s4", sep, sep);
expectedOutput ="(?si).*?ERROR.*?";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
b

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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