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.
Related
Suppose a right triangle is placed in a plane as
shown below. The right-angle point is placed at (0, 0), and the other two points
are placed at (200, 0), and (0, 100). Write a program that prompts the user to enter
a point with x- and y-coordinates and determines whether the point is inside the
triangle.
`String xInput, yInput;
double x, y;
xInput = JOptionPane.showInputDialog("Enter the x-coordinate of the point");
yInput = JOptionPane.showInputDialog("Enter the y-coordinate of the point");
x = Double.parseDouble(xInput);
y = Double.parseDouble(yInput);
if (x <= 200 && x >= 0 && y <= 100 && y >= 0) {
if (y = roundup(x/2))
System.out.print("The point is in the the triangle");
else
System.out.print("The point isn't in the triangle");
}else
System.out.print("The point isn't in the triangle");`
The output is an error in the second if saying that a double can't be a boolean
This is the figure for clarifications
Basically you have a linear formula that is y = 100 - x/2 where x is between 0 and 200 so we can create simple method for that
static double calculateY(double x) {
return 100.0 - x / 2.0;
}
and then compare x against the boundaries and y against the formula
if (x < 0 || x > 200 || y < 0) {
System.out.print("The point isn't in the triangle");
} else if ( y <= calculateY(x)) {
System.out.print("The point is in the the triangle");
} else
System.out.print("The point isn't in the triangle");
}
Consider a triangle with vertices at (0, 0), (1, 0), and (0, 1). Write a program that asks the user for x and y coordinates and then outputs whether the point is inside the triangle, on the border of the triangle or outside the triangle.
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.
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.
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");
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).