THINK FIRST·CODE LATER

← All labs

MyTriangle: methods, validation and formatted output

Problem

Write a class MyTriangle with two static methods: isValid(double s1, double s2, double s3), returning true when the three lengths can form a triangle (the sum of any two sides exceeds the third), and area(double s1, double s2, double s3), using Heron's formula √(s(s−a)(s−b)(s−c)) with s = (a+b+c)/2. The main method reads three sides from the keyboard and prints the area with two decimal places, or a message saying the input is invalid.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

a1-data-1.txt

3 4 5
What your program must print for a1-data-1.txt
The area is 6.00

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < a1-data-1.txt

Windows PowerShell does not understand <:

Get-Content a1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.