need help with this java program i have literally no clue about teacher doesnt h
ID: 3716535 • Letter: N
Question
need help with this java program i have literally no clue about teacher doesnt help much at all. All i know is i need to use import.java.util.scanner(or something like that)
1. Geometrical problem: area of a triangle: Write, compile and run a Java program called AreaOfTriangle.java that prompts the user to enter three points in Cartesian coordinates (x1, y1), ( x2, y2), ( x3, y3) of a triangle and calculates its area based on the user input values. The program then displays the calculated area. The formula for computing the area of a triangle is s = (side1 + side2 + side3)/ 2; (s is the semi-perimeter of the triangle) area = ?s( s - side1)( s - side2)( s - side3) Any side would involve finding out the distance between a pair of points. The distance between two points with coordinates (x1, y1) and (x2, y2) can be found out by the formula s = ?( x2 - x1) 2 + ( y2 - y1) 2
Sample output (The print in blue is generated by the program while running and the one in green is what the user provides as input):
First Run
This program reads in the x and y coordinates of the three points of a triangle and calculates the area of the triangle based on the coordinates. --------------------------------------------------
Enter the x and y coordinates of the first point:1.0 1.0
Enter the x and y coordinates of the second point: 1.0 5.0
Enter the x and y coordinates of the third point: 5.0 1.0
The first side is: 4.0 units
The second side is: 5.656854249492381 units The third side is: 4.0 units
The area of the triangle is: 7.999999999999997 square units
Second Run This program reads in the x and y coordinates of the three points of a triangle and calculates the area of the triangle based on the coordinates. --------------------------------------------------
Enter the x and y coordinates of the first point: 2.5 3.6
Enter the x and y coordinates of the second point: 5.75 4.96
Enter the x and y coordinates of the third point: 3.2 9.8
The first side is: 3.523081037955272 units
The second side is: 5.470658095695618 units The third side is: 6.2393909959226 units
The area of the triangle is: 9.599000000000004 square units
Explanation / Answer
AreaOfTriangle.java
import java.util.Scanner;
public class AreaOfTriangle {
public static void main(String[] args) {
//Declaring variables
double x1,y1,x2,y2,x3,y3,s1,s2,s3;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter the x and y coordinates of the first point:");
x1=sc.nextDouble();
y1=sc.nextDouble();
System.out.print("Enter the x and y coordinates of the second point:");
x2=sc.nextDouble();
y2=sc.nextDouble();
System.out.print("Enter the x and y coordinates of the third point:");
x3=sc.nextDouble();
y3=sc.nextDouble();
//calling the method which calculate the distance between two points
s1= findDistance(x1, y1, x2, y2);
s2= findDistance(x2, y2, x3, y3);
s3= findDistance(x3, y3, x1, y1);
//Displaying the Sides of the Triangle
System.out.println("The first side is:"+s1+" units");
System.out.println("The second side is:"+s2+" units");
System.out.println("The third side is:"+s3+" units");
//Calculating the Semi-perimeter of the Triangle
double s=(s1+s2+s3)/2;
//Calculating the area of the Triangle
double area=areaOfTriangle(s1,s2,s3);
System.out.println("The area of the triangle is:"+area+" square units");
}
/* This method is used to calculate
* the area of the triangle using Herons formula
*/
private static double areaOfTriangle(double side1, double side2, double side3)
{
double p=(side1+side2+side3)/2;
double area=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
return area;
}
//This method will find the length of each side of the triangle
private static double findDistance(double x1, double y1, double x2,
double y2) {
return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}
}
________________
Output:
Enter the x and y coordinates of the first point:1.0 1.0
Enter the x and y coordinates of the second point:1.0 5.0
Enter the x and y coordinates of the third point:5.0 1.0
The first side is:4.0 units
The second side is:5.656854249492381 units
The third side is:4.0 units
The area of the triangle is:7.999999999999997square units
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.