Write a Java program and methods in the order listed below. Calculate area of a
ID: 673106 • Letter: W
Question
Write a Java program and methods in the order listed below. Calculate area of a triangle using methods.
Declare the variables for the sides of the triangle, perimeter, and area inside the main method and use them as appropriate to call the methods.
Call a void method that will output an explanation of what the program does. Include in the explanation that this program uses methods and include your name.
Prompt for the input of three real numbers – use type double – these will be the lengths of the sides of a triangle. Input the three sides and ensure the numbers are greater than 0. Use one method to read and return the value of a side so you will call this method three times. Note: use the style of Assignment #2 for the input, not JOptionPane as in Assignment #3.
Calculate the perimeter of the triangle in a method. The perimeter is the sum of the lengths. It will have three parameters (the sides of the triangle). Then, use a method to calculate the area of the triangle – it will have three parameters (the sides of the triangle, and not the perimeter) – with Heron’s formula:
_________________
A = s(s – a)(s – b) (s – c) where
s is 1/2 of the perimeter and is a local variable, and a, b, and c represent the lengths of the sides of the triangle.
Use the predefined class Math and the method Math.sqrt to calculate the square root. Usage: value = Math.sqrt(an expression)
Use a void method to output, with appropriate messages and formatting:
The lengths of the sides of the triangle,
The perimeter of the triangle, and
The area of the triangle.
Note that this method requires five parameters.
Explanation / Answer
import java.io.*;
import java.lang.Math;
import java.util.Scanner;
public class Triangle
{
static void explanation()
{
System.out.println("Author: XXX This program reads the sides of a triangle using the method readSide() ");
System.out.println("It then calculates the perimeter of a triangle using the method calcPermeter() ");
System.out.println("It then calculates the area of a triangle using the method calcArea() ");
System.out.println("Finally it displays the results using the method displayResults(). ");
}
static double readSide()
{
Scanner in = new Scanner(System.in);
double x = in.nextDouble();
return x;
}
static double calcPerimeter(double x, double y, double z)
{
return x + y + z;
}
static double calcArea(double x, double y, double z)
{
double s = (x + y + z) / 2;
double area = Math.sqrt(s * (s - x) * (s - y) * (s - z));
return area;
}
static void displayResults(double x, double y, double z, double peri, double area)
{
String output = String.format("The lengths of the sides of the triangle are: %.2f, %.2f and %.2f", x, y, z);
System.out.println(output);
output = String.format("The perimeter of the triangle is : %.2f", peri);
System.out.println(output);
output = String.format("The area of the triangle is: %.2f", area);
System.out.println(output);
}
public static void main(String[] args)
{
explanation();
System.out.print("Enter the side 1 of a triangle: ");
double side1 = readSide();
System.out.print("Enter the side 2 of a triangle: ");
double side2 = readSide();
System.out.print("Enter the side 3 of a triangle: ");
double side3 = readSide();
double perimeter = calcPerimeter(side1, side2, side3);
double area = calcArea(side1, side2, side3);
displayResults(side1, side2, side3, perimeter, area);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.