Distance in BlueJ - java

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.

Related

Java: .print not working the way I intended in my code

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.

Working with Negative Infinity (Vertical) slopes in BlueJ

Everything in my code finally seems right. i'm just having trouble with something tricky.
How do I write a code so that when i enter two points and the slope is -infinity, it's identified and the output says Vertical and NOT Negative Slope.
For example, a Positive Slope OUTPUT would 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
AND a Vertical would look like:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Vertical
AND right now my Vertical Output looks like:
Enter the x and y coordinates of the first point: 4 5
Enter the x and y coordinates of the second point: 4 -3
Distance: 8.000
Negative Slope
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");
}
}
You can check explicitly if slope == Double.NEGATIVE_INFINITY. You should do the same for Double.POSITIVE_INFINITY.
if (slope == Double.NEGATIVE_INFINITY || slope == Double.POSITIVE_INFINITY)
System.out.println("Vertical");
else 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");
Or you can use Double.isInfinite :
if (Double.isInfinite(slope))
System.out.println("Vertical");
else 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");

Java - Two Circles overlap or inside

I have a problem on my homework, and I am having trouble getting the last part to work. What am I doing wrong here? It will not print "Circle 2 overlaps Circle 1." or "Circle 2 does not overlap Circle 1." The first two cases do print. Here is the question:
Write a program (TwoCircles.java) that prompts the user to enter the center coordinates and radii of two circles and determines the geometrical relationship between the two and print one of the following messages accordingly:
1. Circle 1 is inside Circle 2.
2. Circle 2 is inside Circle 1.
3. Circle 2 overlaps Circle 1.
4. Circle 2 does not overlap Circle 1.
Here is what i have so far for my code:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1){
if (distance <= (radius2 - radius1))
System.out.println("Circle 1 is inside Circle 2.");}
else if (radius1 >= radius2){
if (distance <= (radius1 - radius2))
System.out.println("Circle 2 is inside Circle 1.");}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
Any guidance appreciated
Use the following code:
import java.util.Scanner;
public class TwoCircles {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Circle 1 center x-, y-coordinates, and radius: ");
double X1 = input.nextDouble();
double Y1 = input.nextDouble();
double radius1 = input.nextDouble();
System.out.print("Enter Circle 2 center x-, y-coordinates, and radius: ");
double X2 = input.nextDouble();
double Y2 = input.nextDouble();
double radius2 = input.nextDouble();
double distance = Math.pow((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2), 0.5);
if (radius2 >= radius1 && distance <= (radius2 - radius1)){
System.out.println("Circle 1 is inside Circle 2.");
}
else if (radius1 >= radius2 && distance <= (radius1 - radius2) ) {
System.out.println("Circle 2 is inside Circle 1.");
}
else if (distance > (radius1 + radius2)){
System.out.println("Circle 2 does not overlap Circle 1.");
}
else {
System.out.println("Circle 2 overlaps Circle 1.");}
}
}
The problem with the code which you are using is that either of the first two conditions:
if(radius1>=radius2)
or
else if(radius1<=radius2)
will always be true and hence the code will never reach the third or fourth condition. The correct way to achieve what you are trying to do is to merge all the conditions together which i have done in the code presented above.
Hope this helps!
You probably "lost" yourself in that if-else statements.
First of all, I would recommend to write it with ALL the braces to understand correctly, what exactly is happening there :
if (radius2 >= radius1) {
if (distance <= (radius2 - radius1)) {
System.out.println("Circle 1 is inside Circle 2.");
}
} else if (radius1 >= radius2) {
if (distance <= (radius1 - radius2)) {
System.out.println("Circle 2 is inside Circle 1.");
}
} else if (distance > (radius1 + radius2)) {
System.out.println("Circle 2 does not overlap Circle 1.");
} else {
System.out.println("Circle 2 overlaps Circle 1.");
}
As you can see, you say "if radius2 is equal or greater than radius1" do something and if not and if "radius1 is greater or equal radius2" do something else.
If you think about it, there is no possibility which is neither first or second condition. So the last two statements are logically unreachable.

Finding whether a point is within a triangle

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");

calculate area of triangle given 3 user defined points -Beginner

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).

Categories

Resources