So this is my very first homework assignment regarding code and I've spent the past few hours trying to figure this out but I'm completely stumped.
I'm trying to find the area of a triangle by inputting its three sides (Which I managed to figure out how to do), but when I actually compile and run the code, my .print isn't working as intended.
Here's my code thus far:
import java.util.Scanner;
// Purpose: To get the area of a triangle
public class ComputeTriangleArea {
// main method
public static void main(String[] args) {
// Creating a scanner
Scanner scanner = new Scanner(System.in);
// Entering dimensions
double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble();
double x3 = scanner.nextDouble(); double y3 = scanner.nextDouble();
// Inputting side 1
System.out.print("Enter the dimensions of side 1: ");
double side1 = Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
// Inputting side 2
System.out.print("Enter the dimensions of side 2: ");
double side2 = Math.sqrt((x1-x3) * (x1-x3) + (y2-y3) * (y2-y3));
// Inputting side 3
System.out.print("Enter the dimensions of side 3: ");
double side3 = Math.sqrt((x2-x3) * (x2-x3) + (y2-y3) * (y2-y3));
// Convert to Area
double s = (side1+side2+side3) / 2;
double area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3));
// Display the result
System.out.printf("The area is %.2f\n", area);
}
}
and the result is if I input let's say 1, 2, 3, 4, 5, 6
it shows up as:
java -cp . ComputeTriangleArea
1
2
3
4
5
6
Enter the dimensions of side 1: Enter the dimensions of side 2: Enter the dimensions of side 3: The area is 3.87
Exit code: 0
Would anyone mind guiding me in the right direction?
Thank you so so much!
Edit: Here's the code I managed to get from the help of everyone in the comments below:
import java.util.Scanner;
// Purpose: To get the area of a triangle
public class ComputeTriangleArea {
// main method
public static void main(String[] args) {
// Creating a scanner
Scanner scanner = new Scanner(System.in);
// Inputting side 1
scanner = new Scanner(System.in);
System.out.print("Enter the dimensions of side 1: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
// Inputting side 2
System.out.print("Enter the dimensions of side 2: ");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
// Inputting side 3
System.out.print("Enter the dimensions of side 3: ");
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
double side1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double side2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
double side3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
// Convert to Area
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
// Display the result
System.out.printf("The area is %.2f\n", area);
}
}
You can try below code:
// main method
public static void main(String[] args) {
// Creating a scanner
Scanner scanner = new Scanner(System.in);
// Inputting side 1
System.out.print("Enter the dimensions of side 1: ");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
// Inputting side 2
System.out.print("Enter the dimensions of side 2: ");
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
// Inputting side 3
System.out.print("Enter the dimensions of side 3: ");
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
double side1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double side2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y2 - y3) * (y2 - y3));
double side3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
// Convert to Area
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
// Display the result
System.out.printf("The area is %.2f\n", area);
}
Updated
Use single Scanner instead of multiple create Scanner, the enter key would not break System.in(Which i'm incorrect previously with thought enter key would break System.in)
That output is expected from the code you wrote.
I assume you want the output to be (please provide expected output)
Enter the dimensions of side 1: 1 2
Enter the dimensions of side 2: 3 4
Enter the dimensions of side 3: 5 6
The area is 3.87
If you observe your code, you've scanned all the 6 numbers first and are then printing the statements. Thus the output is as you've got.
If you want the output to appear as I've shown, then you need to print statement for each side, scan 2 numbers after each print statement.
Related
I tried using the top formula of distance from the other question but it display 'NaN'; can someone tell me where I got it wrong. Also, sometimes it doesn't go 'NaN' but the answer is still inaccurate. I'm a beginner.
import java.util.Scanner;
public class Great_Circle{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double r = 6371.0;
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 +
Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))));
System.out.println(distance + " kilometers ");
}
}
ASin is valid on the range [1; 1]. Function spec is saying:
If the argument is NaN or its absolute value is greater than 1, then the result is NaN.
If the argument is zero, then the result is a zero with the same sign as the argument.
However this Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))) seems returning value out of the range.
Hence you get NaN.
OUTPUT should look like:
Enter the x and y coordinates of the first point: 3 -2
Enter the x and y coordinates of the second point: 9 2
Distance: 7.211
Positive Slope
I have updated the OP with what the code should look like if you're trying to find the distance.
Here is what my code looks like right now:
import java.util.Scanner;
public class LineEvaluator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the x and y coordinates of the first point: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter the x and y coordinates of the second point: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.printf("Distance: %.3f", distance);
double slope = ((y2 - y1) / (x2 - x1));
if(slope > 0)
System.out.println("Positive Slope");
else if(slope < 0)
System.out.println("Negative Slope");
else if(slope == 0)
System.out.println("Horizontal");
}
}
So... First of all don't say int to double like that - go in the documentations and you'll find that input.nextDouble() exists and you can use that instead directly.
e.x.:
int y2 = input.nextInt();
double g = y2;
to...
double g = input.nextDouble()
Also, in your distance formula, you are using:
double distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)*(y1 - y2));
If you're using that, please name your doubles to the proper names(x1, x2, etc...)
When you use int, it truncates. Read up on the differences:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Then, you can calculate the slope, say you store it in a variable slope.
When you're trying to find a slope that has infinite slope, you will get this error: ArithmeticException.
You can fix this by a try catch, surrounding the code you might divide by 0, and then System.out.println("Vertical Line") at that situation, or you could evaluate later and leave the try catch blank. Use this: Double.isInfinite(double) to evaluate later on.
Try catch example:
try{
slope = (y2-y1)/(x2-x1)//if it's divide by zero then we will get this error
}catch(ArithmeticException e){
//Take care of the error as we discussed.
}
Use if-elses or switch statements to evaluate the slope:
if(slope > 0)
System.out.println("Positive slope")
else if(slope == 0)
System.out.println("Flat slope")...
Hope you got the idea.
Here is my task:
Create a class named MyTriangle that contains the following two methods:
/** Return true if the sum of any two sides is * greater than the third side. */
public static boolean isValid (double side1, double side2, double side3)
/** Return the area of the triangle. */
public static double area (double side1, double side2, double side3)
Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid.
Attempt below: Question: I cannot figure this out and the constantly rereading the chapter isn't breaking through any walls. The issue is commented in the code below:
import java.util.Scanner;
public class NewClass1 {
double area;
double side1, side2, side3;
double x1, x2, x3, y1, y2, y3;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two integers for side 1:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter two integers for side 2:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
System.out.print("Enter two integers for side 3:");
double x3 = input.nextDouble();
double y3 = input.nextDouble();
boolean isValid = true;
if (isValid) {
System.out.println("Input is invalid");
}
else
area(side1, side2, side3); //Using area does not work and I don't know how to remedy this. I've read the chapter over and over... I cannot get it to work.
}
public static double area(double side1, double side2, double side3) {
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
double x3 = 0;
double y3 = 0;
side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (area);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
}
Reviewing the code, can someone please explain what it is that I'm doing wrong, and explain a possible remedy. Everything is there, I simply cannot connect the dots.
Revision--Code
import java.util.Scanner;
public class NewClass1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter side 1: ");
double side1 = input.nextDouble();
System.out.print("Enter side 2: ");
double side2 = input.nextDouble();
System.out.print("Enter side 3: ");
double side3 = input.nextDouble();
double a = area(side1, side2, side3);
boolean isV = isValid(side1, side2, side3);
if (isV)
System.out.println("Inout is Invalid");
else
System.out.println("Area is: " + a);
}
public static boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2) > side3) && ((side1 + side3) > side2) && ((side2 + side3) > side1));
}
public static double area(double side1, double side2, double side3) {
//Calculates the sides/angles using Heron's formula
double s = (side1 + side2 + side3)/2;
double theArea = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
return (theArea);
}
}
I keep getting NaN as the answer for the area. What am I doing wrong?
So you write the following:
if the input is valid, show error message. Otherwise (else) compute area.
You should just swap your if and else parts! Your program never calls the area() method if you are calling it with points for a valid triangle.
Moreover, you never call the isValid() method. You assign true to the variable, and then check it in the next line, but the method that actually checks it has never been called.
ALSO you need side variables for isValid(), but you only compute them in the area() method. You should compute them right after you get the points.
You are just declaring a variable called "isValid" and setting it equal to true. You need to instead calculate the length of the sides before you check if the input is valid. Then you call the isValid function by calling
isValid(side1, side2, side3);
When you call the area method (which you are not calling per #mashaned 's answer)
area(side1, side2, side3);
You are calling it with variables that have only been initialized and not set.
side1, side2, and side3 do not have a value when you call area.
You should either create a class for side variables so that you can pass in the x and y values like this:
area(new Side(x1, y1), new Side(x2, y2), new Side(x3, y3));
Or
You should change the area method to accept x1, y1, x2, y2, x3, y3 instead of sides since you are computing sides in the area method like:
public static double area(double x1, double y1, double x2, double y2, double x3, double y3) {
Additionally, you are not doing anything with the area returned by the area method when you call it.
I suggest something like:
System.out.println("Area " + String.valueOf([INSERT VERSION OF AREA METHOD CALL OF YOUR CHOICE]));
The first answer is also right about the isValid variable. You are not using the isValid() method for validation.
You should use it in similar fashion to area in terms of what is passed in.
An example class (roughly done) might be like the following:
public class Side(){
double x = 0;
double y = 0;
public Side(double x, double y){
this.x = x;
this.y = y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;
}
}
You may want to consider adding this method and removing the similar code from your area method:
public void computeSides(double x1, double y1, double x2, double y2, double x3, double y3){
side1 = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);
side2 = Math.pow(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2), 0.5);
side3 = Math.pow(Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2), 0.5);
}
Then when you go to call isValid() or area() you just make sure you call computeSides() first and then the side variables will have values and both should work.
Write a method computeArea that takes three sides of a triangle and returns the area of that triangle (assume the sides are valid) Write a main method to read the sides of two triangles, compute their areas (using the computeArea method), and print the sides of the triangle with larger area. run:
Enter the sides for the triangle: 4 6 9
Enter the sides for the triangle: 3 9 8
the triangle with largest area has the folowing sides:
side1: 3.0, Side2: 9.0, and side3: 8.0
I have been on this for hours, attempting different methods looking at just about every question. Perhaps I have it completely wrong, but I feel that I have my math of it correct, but no matter what numbers I input, I get the same output. My code is off somewhere and I have to turn it in by midnight.
It is the all so fun: Find if a point is within a triangle code. (for beginners)
import java.util.Scanner;
public class PointsTriangle {
// checks if point entered is within the triangle
//given points of triangle are (0,0) (0,100) (200,0)
public static void main (String [] args) {
//obtain point (x,y) from user
System.out.print("Enter a point's x- and y-coordinates: ");
Scanner input = new Scanner(System.in);
double x = input.nextDouble();
double y = input.nextDouble();
//find area of triangle with given points
double ABC = ((0*(100-0 )+0*(0 -0)+200*(0-100))/2.0);
double PAB = ((x*(0 -100)+0*(100-y)+0 *(y- 0))/2.0);
double PBC = ((x*(100-0 )+0*(0 -y)+200*(y-100))/2.0);
double PAC = ((x*(0 -100)+0*(100-y)+200*(y- 0))/2.0);
boolean isInTriangle = PAB + PBC + PAC == ABC;
if (isInTriangle)
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");
}//end main
}//end PointsTriangle
You placed the wrong value order into your formula; therefore, the result is wrong. If the 3 vertices are as the following
A(x1, y1) B(x2, y2), C(x3, y3)
then the area is calculated as
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2;
After that, you just replace each vertex with the input point, we will have the following triangles: PBC, APC, ABP.
Put everything together, we will have the correct one
int x1 = 0, y1 = 0;
int x2 = 0, y2 = 100;
int x3 = 200, y3 = 0;
// no need to divide by 2.0 here, since it is not necessary in the equation
double ABC = Math.abs (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
double ABP = Math.abs (x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
double APC = Math.abs (x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
double PBC = Math.abs (x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));
boolean isInTriangle = ABP + APC + PBC == ABC;
If you draw a picture, you can see the point has to satisfy simple inequalities (below / above / to the right of certain lines). Whether "on the edge" is in or out I will leave up to you:
Y > 0 (above the X axis)
X > 0 (to the right of the Y axis)
X + 2* Y < 200 (below the hypotenuse)
Write an if statement around these three and you're done:
if( (y > 0) && (x > 0) && (x + 2*y < 200) )
System.out.println("The point is in the triangle");
else
System.out.println("The point is not in the triangle");
I can get my code to compile, but it doesn't produce the area that is desired. I'm not sure where I have stumbled.
They want you to have the user enter 6 coordinates (x and y value) for the 3 points of a triangle and get the area. My code is as follows:
import java.util.Scanner;
public class AreaTriangle {
// find the area of a triangle
public static void main (String [] args) {
double side1 = 0;
double side2 = 0;
double side3 = 0;
Scanner input = new Scanner(System.in);
//obtain three points for a triangle
System.out.print("Enter three points for a triangle (x and y intercept): ");
double side1x = input.nextDouble();
double side1y = input.nextDouble();
double side2x = input.nextDouble();
double side2y = input.nextDouble();
double side3x = input.nextDouble();
double side3y = input.nextDouble();
//find length of sides of triangle
side1 = Math.pow(Math.pow((side2x - side1x), 2) + Math.pow((side2y - side1y), 2) * .05, side1);
side2 = Math.pow(Math.pow((side3x - side2x), 2) + Math.pow((side3y - side2y), 2) * .05, side2);
side3 = Math.pow(Math.pow((side1x - side3x), 2) + Math.pow((side1y - side3y), 2) * .05, side3);
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s-side3)) * 0.5;
System.out.println("area" + area);
}
}
You should try implementing this equation. http://www.mathopenref.com/coordtrianglearea.html
#Michael's suggestion is a good one. Following your code, I'd use Pythagoras' Theorem like this:
side1 = Math.sqrt(
Math.pow((side2x - side1x), 2)
+ Math.pow((side2y - side1y), 2));
In your code:
side1 = Math.pow(
Math.pow((side2x - side1x), 2)
+ Math.pow((side2y - side1y), 2) * .05
, side1);
side1 is 0 before the calculation, and almost anything to the power 0 is 1. Therefore side1 ends as 1 regardless of the points.
Another way I've discovered is that you can use the cross product to find the area of a triangle. This may be slightly easier for you, since you already have the points. You can turn the three points in to two vectors and take the cross product.
edit:
Whoops, forgot to add in the area of a triangle would be half of the cross product, since the cross product would give you the area of a parallelogram formed by the two vectors (and a triangle is half that).