Finding The Area Of A Triangle - java

I have a question that I would like to ask you. In my book for java programming, it asks me to write a program that finds the area of a triangle given 3 points. I tried many ways but I could never get the right answer. Can you please give me a solution to this problem. Thanks! Here is the question:
Here is my code:
import java.util.Scanner;
public class shw2point15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double s = ((x1 + y1) + (x2 + y2) + (x3 + y3)) / 2;
double area = Math.sqrt(s * (s - (x1 - y1)) * (s - (x2 - y2)) * (s - (x3 - y3)));
System.out.println("The area of the triangle is " + area);
}
}

The reason you're not getting a correct answer is because you are not finding the sides correctly. However, after finding the side length you can get the answer. Here is what I did:
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter three points for a triangle:");
//Store the values in an array.
double[] xCoordinates = new double[3];
double[] yCoordinates = new double[3];
double[] sides = new double[3];
// Input the values into the array
xCoordinates[0] = input.nextDouble();
yCoordinates[0] = input.nextDouble();
xCoordinates[1] = input.nextDouble();
yCoordinates[1] = input.nextDouble();
xCoordinates[2] = input.nextDouble();
yCoordinates[2] = input.nextDouble();
// Find the side length from the input. There probably are better ways to do this.
sides[0] = Math.sqrt(Math.pow(xCoordinates[0]-xCoordinates[1], 2)+Math.pow(yCoordinates[0]-yCoordinates[1], 2));
sides[1] = Math.sqrt(Math.pow(xCoordinates[1]-xCoordinates[2], 2)+Math.pow(yCoordinates[1]-yCoordinates[2], 2));
sides[2] = Math.sqrt(Math.pow(xCoordinates[2]-xCoordinates[0], 2)+Math.pow(yCoordinates[2]-yCoordinates[0], 2));
// Find s from the sides
double s = ( sides[0]+sides[1]+sides[2] )/2;
// Find the area.
double area = Math.sqrt(s*( s-sides[0] )*( s-sides[1] )*( s-sides[2] ));
// Print the area
System.out.println("The area of the triangle is "+area);
// Output~~~~~~~~~~~~~~~
//Enter three points for a triangle:
// 1.5
// -3.4
// 4.6
// 5
// 9.5
// -3.4
// The area of the triangle is 33.600000000000016
}'

Related

How to convert double x1 = input.nextDouble(); into an array? [duplicate]

This question already has answers here:
how to take user input in Array using java?
(9 answers)
Closed 1 year ago.
/*
(Geometry: area of a triangle) Write a program that prompts the user to enter
three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area.
*/
import java.util.Scanner;
public class Exercise_02_19 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter three points
System.out.print("Enter three points for a triangle: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
// Compute the area of a triangle
double side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
double side2 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
double side3 = Math.pow(Math.pow(x1 - x3, 2) + Math.pow(y1 - y3, 2), 0.5);
double s = (side1 + side2 + side3) / 2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
// Display result
System.out.println("The area of the triangle is " + area);
}
}
I am learning the Java coding language and currently stuck on a question. How can I convert double x1 = input.nextDouble(); into an array that does x1 to 3 and y1 to 3? We are currently using the Introduction to Java Programming and Data Structures Twelfth Edition and even taking the time to read and understand what is going on is hard for me to comprehend. If there is someone with Java knowledge to help what went wrong and point me in the right direction that would be great. Thank you!
double[] x = new double[3];
double[] y = new double[3];
Now you can access x1 to x3 as x[0] to x[2] and similarly for y.

(java) Decide if a line intercepts a circle

So, I need to write a program that decides whether a line intercepts a circle or not. Specific interception coordinates are not required, and simply being touching the circle or being tangent to it causes interception as well. Apologies if this has been asked before, but I couldn't find anything that suited my problem. Maybe I didn't look hard enough.
import java.util.Scanner;
public class LineCircle_Intersection {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double p1x, p2x, p1y, p2y, cx, cy, r;
System.out.print("Enter p1x: ");
p1x = in.nextDouble();
System.out.print("Enter p1y: ");
p1y = in.nextDouble();
System.out.print("Enter p2x: ");
p2x = in.nextDouble();
System.out.print("Enter p2y: ");
p2y = in.nextDouble();
System.out.print("Enter cx: ");
cx = in.nextDouble();
System.out.print("Enter cy: ");
cy = in.nextDouble();
System.out.print("Enter r: ");
r = in.nextDouble();
if ((p1x - cx < r) && (p1y - cy < r))
System.out.println("The line intersects the circle.");
else if ((p2x - cx < r) && (p2y - cy < r))
System.out.println("The line intersects the circle.");
else
System.out.println("The line does not intersect the circle.");
}
}
I've been working on an alternate route that uses line and circle formulas instead of the input points, but it's kind of a mess and I'm not sure where I'm going with it.
We can write the equation for the line as y-p1y = m (x-p1x), where m = (p2y-p1y)/(p2x-p1x)
then y = m(x-p1x) + p1y (1)
We can write the equation for the circle as (x-cx)^2 + (y-cy)^2 = r^2 (2)
If we plug (1) into (2) we get:
x^2 (1+m) + x (-2a+2cm-2dm-2bm) + a^2-2cmb+2dmb+mb^2+c^2-2cd+d^2-r^2=0
where
a = cx
b = p1x
c = p1y
d = cy
let
a' = (1+m)
b' =(-2a+2cm-2dm-2bm)
c' = a^2-2cmb+2dmb+mb^2+c^2-2cd+d^2-r^2
we know then that the line and the circurfence touch each other if
b'^2-4a'c' >= 0

Calculating the area of a triangle

I am new to Java. I am trying to calculate the area of a triangle using the formula:
s = (side 1 + side 2 + side 3)/2
area = square root (side (side - side 1)(side - side2)(side - side3).
If the user enter the three point as:
1.5 -3.4 4.6 5 9.5 -3.4 then the area of the triangle should be 33.6. However, my program runs, but it's giving me an incorrect answer. Below is my code.
// Import Java Scanner
import java.util.Scanner;
import java.lang.Math;
public class Ex_2_19 {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
float side = 0;
float area1 = 0;
float area2 = 0;
float area3 = 0;
float area4 = 0;
float calculatedarea = 0;
//Prompt the user to enter three points of a triangle
System.out.println("Enter point x1:");
System.out.println("Enter point y1:");
System.out.println("Enter point x2:");
System.out.println("Enter point y2:");
System.out.println("Enter point x3:");
System.out.println("Enter point y3:");
//Define the variables
float Pointx1 = input.nextFloat();
float Pointy1 = input.nextFloat();
float Pointx2 = input.nextFloat();
float Pointy2 = input.nextFloat();
float Pointx3 = input.nextFloat();
float Pointy3 = input.nextFloat();
//Formula to calculate the area of a triangle
side = (Pointx1 + Pointy1 + Pointx2 + Pointy2 + Pointx3 + Pointy3) / 2;
area1 = side - (Pointx1 + Pointy1);
area2 = side - (Pointx2 + Pointy2);
area3 = side - (Pointx3 + Pointy3);
area4 = side * area1 * (area2) * (area3);
calculatedarea = (float) (Math.sqrt(area4));
//calculatedarea = (float) (Math.sqrt(area1)*(area2) * (area3));
//Print result
System.out.println("The area of the triangle is " + calculatedarea);
}
}
You are trying Heron's Formula - note that a, b, c are the euclidean distance between the points, thus will need to be computed by sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)), etc., not just the sum of differences.

Having issues with rounding numbers

import java.util.Scanner;
public class clockwise {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the coordinates in a clowise order");
System.out.println("Enter the GPS coordinates for the 1st city: ");
double coordinateX1 = input.nextDouble();
double coordinateY1 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 2nd city: ");
double coordinateX2 = input.nextDouble();
double coordinateY2 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 3rd city: ");
double coordinateX3 = input.nextDouble();
double coordinateY3 = input.nextDouble();
System.out.println("Enter the GPS coordinates for the 4th city: ");
double coordinateX4 = input.nextDouble();
double coordinateY4 = input.nextDouble();
//
double earthRadius = 6371.01;
// Get distance
// distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos( y1−y2))
// ****************************************************************************1
// 1 35.2270869 -80.8431267
double distance1 = (earthRadius)
* Math.acos(Math.sin(coordinateX1) * Math.sin(coordinateX2))
+ Math.cos(coordinateX1) * Math.cos(coordinateX2)
* Math.cos(coordinateY1 - coordinateY2);
System.out.println("distance1: "+distance1);
// 2 35.2270869 -80.8431267
double distance2 =
(earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX4))
+ Math.cos(coordinateX2) * Math.cos(coordinateX4)
* Math.cos(coordinateY2 - coordinateY4);
System.out.println("distance2: "+distance2);
// 3 28.5383355 -81.3792365
double distance3 = (earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX1))
+ Math.cos(coordinateX4) * Math.cos(coordinateX1)
* Math.cos(coordinateY4 - coordinateY1);
System.out.println("distance3: "+distance3);
// ******************************************************************************2
// 4 33.7489954 -84.3879824
// 1 35.2270869 -80.8431267
double distance01 = (earthRadius)
* Math.acos(Math.sin(coordinateX2) * Math.sin(coordinateX3))
+ Math.cos(coordinateX2) * Math.cos(coordinateX3)
* Math.cos(coordinateY2 - coordinateY3);
// System.out.println("distance: "+distance01);
// 2 32.0835407 -81.0998342
double distance02 = (earthRadius)
* Math.acos(Math.sin(coordinateX3) * Math.sin(coordinateX4))
+ Math.cos(coordinateX3) * Math.cos(coordinateX4)
* Math.cos(coordinateY3 - coordinateY4);
//System.out.println("distance: "+distance02);
// 3 28.5383355 -81.3792365
double distance03 =
(earthRadius)
* Math.acos(Math.sin(coordinateX4) * Math.sin(coordinateX2))
+ Math.cos(coordinateX4) * Math.cos(coordinateX2)
* Math.cos(coordinateY4 - coordinateY2);
// System.out.println("distance: "+distance03);
double rodistance1 = Math.ceil(distance1);
double rodistance2 = Math.ceil(distance1);
double rodistance3 = Math.ceil(distance3);
double rodistance01 = Math.ceil(distance01);
double rodistance02 = Math.ceil(distance02);
double rodistance03 = Math.ceil(distance03);
double s1 = (rodistance1 + rodistance2 + rodistance3) / 2;
double s2 = (rodistance01 + rodistance02 + rodistance03) / 2;
double area1 = Math.sqrt(s1 * (s1 - rodistance1) * (s1 - rodistance2)
* (s1 - rodistance3));
double area2 = Math.sqrt(s2 * (s2 - rodistance01) * (s2 - rodistance02)
* (s2 - rodistance03));
double totalArea = Math.ceil(area1 + area2);
System.out.println("The area is: " + totalArea);
}
}
//SAMPLE
//Please enter the coordinates in a clockwise order.
//Enter the GPS coordinates for the 1st city: 35.2270869 -80.8431267
//Enter the GPS coordinates for the 2nd city: 32.0835407 -81.0998342
//Enter the GPS coordinates for the 3rd city: 28.5383355 -81.3792365
//Enter the GPS coordinates for the 4th city: 33.7489954 -84.3879824
//The area is: 117863.342
I am getting 1.06664794E8 What can I do to get the answer as the sample? Unless I am having errors with my formulas, I should be getting a ok answer. I am using Math.ceil() it might not be what I really wanted I needed to round to 3 decimals .................................................................
Use BigDecimal and setScale()
BigDecimal bg1 = new BigDecimal("1.06664794E8");
// set scale of bg1 to 3 and using CEILING as rounding mode
bg1 = bg1.setScale(3, RoundingMode.CEILING);
System.out.println("After changing the scale to 3 and rounding is "+bg1);
Output:After changing the scale to 3 and rounding is 106664794.000
You can use BigDecimal here
double val = 1.06664794E8;
BigDecimal bigDecimal = new BigDecimal(val);
System.out.println(bigDecimal);
Out put:
106664794

Loop will not end. How do i make it exit?

Got scolded earlier for not specify the question. Promise to do better this time.
For some reason, the loop doesn't stop when i enter 0, instead it continues to print out the main method again. How do i fix this? I couldn't find out what is wrong with it, since everything else runs smoothly.
Also, I want to use Switch instead of If, but i keep getting Duplicate Variable error. Why?
import java.util.Scanner;
public class FindArea
{
// side is only input provided to cube. Area of the cube is returned
public static double cube (double side)
{
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere (double radius)
{
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder (double radius, double height)
{
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut (double outerR, double innerR)
{
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
do
{
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
} while (n != 0);
System.exit(1);
}
}
It prints the output one more time because the comparison is not made until the end of the Do-While loop.
It checks for all cases of n other than 0, and prints the output no matter what.
I think what you're looking for is more so:
n = input.nextInt();
if(n!=0){
if ( n<0 || n>4) {
System.out.println("Invalid");
}
if(n == 1) {
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
}
if (n == 2) {
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
}
if (n == 3) {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
}
if (n == 4) {
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
}
System.out.println("--------");
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
}
note the n!=0
Additionally look into the use of switch-case, once you learn them these comparisons will not only be faster, but very easy to comprehend
You can try this with your switch case -
public static void main(String[] args)
{
int n = 4;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
if ( n<0 || n>4) {
System.out.println("Invalid");
}
else{
switch(n){
case 0:{
System.out.println("You enter 0 to exit");
break;
}
case 1:{
System.out.print("Enter side measurement of cube: ");
double side = input.nextDouble();
double Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
}
case 2:{
System.out.print("Enter radius measurement of sphere: ");
double radius = input.nextDouble();
double Area = sphere (radius);
System.out.println("The area of the sphere is: " + Area);
break;
}
case 3: {
System.out.print("Enter radius measurement of cylinder: ");
double radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
double height = input.nextDouble();
double Area = cylinder (radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
}
case 4:{
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
double Area = doughnut (outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
}
}
}
}
i have done this using switch case. Very simple as well
public class FindArea {
// side is only input provided to cube. Area of the cube is returned
public static double cube(double side) {
double Area;
return Area = 6 * side * side;
}
// radius is only input provided to sphere. Area of the sphere is returned
public static double sphere(double radius) {
double Area;
return Area = 4 * 3.14 * radius * radius;
}
// radius and height are the only inputs provided to cylinder.
// Area of the cylinder is returned
public static double cylinder(double radius, double height) {
double Area;
return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius;
}
// outerR and innerR are the only inputs provided to doughnut.
// Area of the doughnut is returned
public static double doughnut(double outerR, double innerR) {
double Area;
return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut, Or enter 0 to exit ");
int n = 0;
double side = 0;
double Area = 0;
double radius = 0;
double height = 0;
n = input.nextInt();
switch (n) {
case 0:
System.out.println("Exit");
System.exit(1);
break;
case 1:
System.out.print("Enter side measurement of cube: ");
side = input.nextDouble();
Area = cube(side);
System.out.println("The area of the cube is: " + Area);
break;
case 2:
System.out.print("Enter radius measurement of sphere: ");
radius = input.nextDouble();
Area = sphere(radius);
System.out.println("The area of the sphere is: " + Area);
break;
case 3:
System.out.print("Enter radius measurement of cylinder: ");
radius = input.nextDouble();
System.out.print("Enter height measurement of cylinder: ");
height = input.nextDouble();
Area = cylinder(radius, height);
System.out.println("The area of the cylinder is: " + Area);
break;
case 4:
System.out.print("Enter inner radius: ");
double innerR = input.nextDouble();
System.out.print("Enter outer radius: ");
double outerR = input.nextDouble();
Area = doughnut(outerR, innerR);
System.out.println("The area of the donut is: " + Area);
break;
default:
System.out.println("--------");
System.out.println("Invalid option selected");
System.out.println("--------");
System.out
.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut");
System.out.println("Or enter 0 to exit");
n = input.nextInt();
break;
}
}
}
Good Luck.

Categories