(TCO 1) Write a simple Java application which calculates the area of a triangle.
ID: 3672568 • Letter: #
Question
(TCO 1) Write a simple Java application which calculates the area of a triangle. Your application should ask the user to enter the base and height values of the triangle. You must use the Scanner class to collect the user's input. The base and height values should be treated as floating point values. Calculate the area of triangle: area = 0.5 * base * height. You must use a JOptionPane to output the result. Your output must show 3 digits after the decimal point. Make sure you remember to import what your program will use. (Points : 40)
Explanation / Answer
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Test{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
float base = 0;
float height = 0;
float area;
System.out.print("Enter the length of base of triangle : ");
base = input.nextFloat();
System.out.print("Enter the length of height of triangle : ");
height = input.nextFloat();
area =(float)(base*height*0.5);
JOptionPane.showMessageDialog(null, String.format("traingle area is %.3f",area));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.