Write a java program which receives 3 integers as input data. These integers wil
ID: 3669395 • Letter: W
Question
Write a java program which receives 3 integers as input data. These integers will be as the length of 3 sides of a triangle. You program needs to check if the input data can make a triangle, and then prints out the result. If they cannot make a triangle, print out a message, otherwise print out the area of the triangle. But do not use Scanner. Use another one input approach such as (Console or InputStreamReader)
The output looks like the following:
Hints:
Show your program works on NetBeans.
run: Enter first side length: 3 Enter second side length: 4 Enter third side length: 5 The area of this triangle is:6 BUILD SUCCESSFUL (total time: 10 seconds run: Enter first side length: 1 Enter second side length: 1 Enter third side length: 3 These sides cannot make a triangle! BUILD SUCCESSFUL (total time: 9 seconds) 1 run: Enter first side length: 4 Enter second side lengLh: 5 The area of this triangle iu: The area of this triangle is: 0 BUILD SUCCESSFUL (total time: 11 seconds)Explanation / Answer
import java.io.Console;
public class Test {
public static void main(String args[]){
Console cnsl = System.console();
int a = 0, b = 0, c = 0;
if(cnsl != null){
String x, y, z;
x = cnsl.readLine("Enter first side length: ");
y = cnsl.readLine("Enter second side length: ");
z = cnsl.readLine("Enter third side length: ");
a = Integer.parseInt(x);
b = Integer.parseInt(y);
c = Integer.parseInt(z);
}
if(a + b >= c && b + c >= a && a + c >= b){
double s = (a + b + c) / 2.0;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.println("The area of this triangle is: " + area);
}
else{
System.out.println("These sides cannot make a triangle!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.