I have to create a class to calculate the distance between 2 given points for class. The instructor gave us the top half of the assignment with all the necessary code without modifying, the problem that im having is creating the class part. This is what i have so far...
class Point{
int x;
int y;
public Point(){
this.x = 0;
this.y = 0;
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public double distance(int x, int y) {
double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
return distance;
}
}
the top half of the assignment looks like this:
import java.util.Scanner;
class Assignment4{
public static void main(String[] args){
// first and second points
Point first, second;
// try parsing points from command line args
if(args.length==4){
// new Point(int x, int y) creates a new Point located at position (x,y)
first = new Point(Integer.valueOf(args[0]), Integer.valueOf(args[1]));
second = new Point(Integer.valueOf(args[2]), Integer.valueOf(args[3]));
}
// if not specified as argument, get points from user
else{
Scanner input = new Scanner(System.in);
System.out.println("Enter first point: ");
first = new Point(input.nextInt(),input.nextInt());
System.out.println("Enter second point: ");
second = new Point(input.nextInt(),input.nextInt());
}
//calculate distance
//double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) );
double d = first.distance(second.x, second.y);
System.out.println("Distance between " +
"(" + first.x + "," + first.y + ")" +
" and " +
"(" + second.x + "," + second.y + ")" +
" is " + d);
System.out.println();
}
}
When i try and compile the program, it says "cannot find symbol" referring to x2, x1, y2, y1, and distance.
Here :
class Point{
int x;
int y;
.....
.....
public double distance(int x, int y) {
double d = Math.sqrt( Math.pow(x2-x1, 2) + Math.pow(y2-y1, 2) ); //ERROR IN THIS LINE
return distance; //ERROR HERE TOO...(2)
}
}
There is no x1,x2,y1,y2 defined in the class or in the method parameter.
Swap it with the following line:
double d = Math.sqrt( Math.pow(this.x-x, 2) + Math.pow(this.y-y, 2) );
(2)
Error 2 Swap with this line:
return d;
Related
Seq is a method which for given line: line's point a and slope b returns perimeter of circular segment. I used the equation of circle and line and solved it for x, and then I easily get two points. But instead of (3.5,1.9) approximately I get (6.528,0.0832000000000006)..
public class Circle {
Point center;
double r;
public double seq(Point a, double b) {
double n = -b*a.getX() + a.getY();
System.out.println("n: "+n);
if (r*r*(b*b+1)-Math.pow(b*center.getX()-center.getY()+n, 2) > 0) {
double temp = (1+b*b);
System.out.println("coeficient x^2: "+temp);
double temp1 = 2*((-1)*center.getX()+b*n-b*center.getY());
System.out.println("coeficient x: "+ temp);
double free_term = center.getX()*center.getX() + n*n -2*n*center.getY() + center.getY()*center.getY() - r*r;
System.out.println("free term: "+free_term);
double D = Math.sqrt(temp1*temp1-4*temp*free_term);
System.out.println(temp1*temp1-4*temp*free_term);
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/2*temp;
double y1 = b*x1 - b*a.getX() + a.getY();
double y2 = b*x2 - b*a.getX() + a.getY();
System.out.println("("+x1+","+y1+")"+", ("+x2+","+y2+")");
Point A = new Point(x1,y1);
Point B = new Point(x2,y2);
double d = A.dist(B);
System.out.println("d: "+d);
double angle = Math.acos((2*r*r-d*d)/2*r*r);
System.out.println("angle "+ alfa);
double l = r*Math.PI*angle/Math.toRadians(180);
return l+d;
} else return 0;
}
}
Main:
Circle k = new Circle();
Point c = new Point(0,0);
k.center = c;
k.r = 4;
Point a = new Point(0,4);
System.out.println(k.seq(a, -3.0/5.0));
Console:
n: 4.0
coeficient x^2: 1.3599999999999999
coeficient x: -4.8
free term: 0.0
23.04
(6.528,0.0832000000000006), (0.0,4.0)
d: 7.612890793910023
angleNaN
NaN
Your problem is in the implementation of the quadratic formula. You might think that A/2*B means A/(2*B) but in reality it's (A/2)*B. So add the parentheses in around the divisor:
double x1 = ((-1)*temp1+Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);
double x2 = ((-1)*temp1-Math.sqrt(temp1*temp1-4*temp*free_term))/(2*temp);
After I execute this code, the output is the output is
3 0
1 2 4
could you please explain the output? does the parameter in the mystery class sequence play a role in this?
public class MysteryReturn {
public static void main(String[] args){
int x = 1;
int y=2;
int z = 3;
z = mystery(x,z,y);
System.out.println(x + " "+y+" "+z);
}
public static int mystery(int z, int x, int y){
z--;
x =2*y +z;
y=x-1;
System.out.println(y + " "+ z);
return x;
}
}
It prints 1 2 3, and you aren't calling mystery function, so it can't affect.
UPD.
Question was updated, so lets look to your function
public static int mystery(int z, int x, int y) {
z--;
x = 2 * y + z;
y = x - 1;
System.out.println(y + " " + z);
return x;
}
it can be rewtiren to
public static int mystery(int z, int x, int y) {
return 2 * y + z-1;
}
and you calling it with arguments 1,3,2 - mystery(1,3,2) so answer becomes 2*2+1-1 which equals 4, so you have z=4, so
System.out.println(x + " "+y+" "+z);
will print 1 2 4
// *************************************************************
// Distance.java
// Computes the distance between two points in java
// *************************************************************
import java.util.Scanner;
import java.lang.Math;
public class Distance
{
public static void main (String[] args)
{
double x1, y1, x2, y2; // coordinates of two points
double distance; // distance between the points
//Formula - D=√(x2-x1)^2 + (y2-y1)^2
Scanner scan = new Scanner(System.in);
// Read in the two points
System.out.print ("Enter the coordinates of the first point " +
"(put a space between them): ");
x1 = scan.nextDouble();
y1 = scan.nextDouble();
System.out.print ("Enter the coordinates of the second point: ");
x2 = scan.nextDouble();
y2 = scan.nextDouble();
// Compute the distance
double math = x1-x2 * y1-y2;
// Print out the answer
System.out.print("The distance between (" + x1 + "," + x2 + ") and (" + y1 + "," + y2 + ") is") ;
}
}
I keep running into errors and I go on forever with codes that can be done faster and shorter. All I need to do is read the user input for coordinates, find the distance between them, and print the answer.
First you create your Point Class. Remember this is Java and OOP
class Point {
private double x;
private double y;
private String name;
public Point(String n, double xx, double yy) {
this.name = n;
this.x = xx;
this.y = yy;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public String getName() {
return this.name;
}
}
Secondly you create your Distance class with the distance calculating method
public double getEuclideanDistance(Point p, Point q) {
double ed = Math.sqrt((q.getX() - p.getX()) * (q.getX() - p.getX()) + (q.getY() - p.getY()) * (q.getY() - p.getY()));
return ed;
}
Lastly modify you main method as below
public static void main(String[] args) {
double Xp;
double Yp;
double Xq;
double Yq;
Scanner reader = new Scanner(System.in);
System.out.print("Enter x- cordinate of P : ");
Xp = reader.nextDouble();
System.out.print("Enter y- cordinate of P : ");
Yp = reader.nextDouble();
System.out.print("Enter x- cordinate of Q : ");
Xq = reader.nextDouble();
System.out.print("Enter y- cordinate of Q : ");
Yq = reader.nextDouble();
Point q = new Point("Q", Xq, Yq);
Point p = new Point("P", Xp, Yp);
System.out.println("Point " + p.getName() + "'s Coordinates are: " + "P(" + p.getX() + " , " + p.getY() + ")");
System.out.println("Point " + q.getName() + "'s Coordinates are: " + "Q(" + q.getX() + " , " + q.getY() + ")");
System.out.println("The Euclidean distance from p to q is :" + getEuclideanDistance(p, q));
}
The issue is that you are not quite implementing the distance formula the correct way... you have double math = x1-x2 * y1-y2 but aren't calling Math.pow() or Math.sqrt() at all.
Try to implement the following:
I have a method that gets the area of a triangle, however it is returning 0.0.
public double getArea() {
//Find the length of sides
double side1 = p1.findLength(p2);
double side2 = p2.findLength(p3);
double side3 = p3.findLength(p1);
//Get area
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
My Point class with findLength(). This function works as intended in my testing:
public double findLength(Point another) {
return Math.sqrt(((another.getX() - this.getX()) * (another.getX() - this.getX()) )+
((another.getY() - this.getY()) * (another.getY() - this.getY())));
}
I can't seem to figure out why it isn't working.
Entire Point class:
/**
* Created by wilson on 9/8/2014.
*/
import java.math.*;
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double findLength(Point another) {
return Math.sqrt(((another.getX() - this.getX()) * (another.getX() - this.getX()) )+
((another.getY() - this.getY()) * (another.getY() - this.getY())));
}
}
My Test Program:
Point p1 = new Point(0,0);
Point p2 = new Point(3,3);
Point p3 = new Point(-3,-3);
Triangle2D t1 = new Triangle2D(p1, p2, p3);
System.out.println("Area of triangle: " + t1.getArea());
The "triangle" you tested:
Point p1 = new Point(0,0);
Point p2 = new Point(3,3);
Point p3 = new Point(-3,-3);
actually consists of three points all on the same line (the line x = y). Therefore, 0 is the correct answer. As far as I can tell, the code is correct.
Side note: I recommend using Math.hypot, which computes sqrt(x2 + y2) in the findLength function. Besides making findLength easier to read, it has the advantage that it won't overflow if x or y is so large that x2 or y2 won't fit in a floating-point number. (Not that it's likely you'll run into that problem, but why not do things right?)
The purpose of the method is to take in a Point (c), then move the current centroid (x,y) to the new coordinated provided. This is done through finding the difference of the two and then translating the point to the new coordinate. However, I do not know how to set the new centroid value since I am setting the methods as voids.... Any ideas?
Sorry for all of the code, tried to put as little as possible. Translate takes the point(x,y) and adds the values given (dx,dy).
private Point centroid;
Triangle(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
triPoints = new Point[] { a, b, c };
}
public Point getCentroid() {
Point centroid;
double x = 0.0;
double y = 0.0;
x = (a.getX() + b.getX() + c.getX()) / 3;
y = (a.getY() + b.getY() + c.getY()) / 3;
centroid = new Point(x, y);
return centroid;
}
public void move(double dx, double dy) {
centroid = getCentroid();
System.out.println(centroid.getX() + " "+ centroid.getY());
centroid = centroid.translate(dx, dy);
Point newCentroid = new Point(centroid.getX(),centroid.getY());
System.out.println(newCentroid.getX() + " "+ newCentroid.getY());
if(getCentroid().equals(newCentroid)){
}else{
}
for (int index = 0; index < triPoints.length - 1; index++) {
triPoints[index].translate(dx, dy);
}
}
public void move(Point c) {
double firstx = 0.0;
double firsty = 0.0;
Point f = getCentroid();
System.out.println(f.getX()+ " "+ f.getY() + " "+ c.getX()+ " "+ c.getY());
if (f.getX() >= c.getX()) {
firstx = c.getX() - f.getX();}
else{
firstx = Math.abs(f.getX() - c.getX());
}
if (f.getY()>= c.getY()){
firsty = c.getY() - f.getY();
}
else {
firsty = Math.abs(f.getY() -c.getY());
}
move(firstx, firsty);
//System.out.println(centroid.getX() + " " + centroid.getY());
}
I am assuming you are using java.awt.Point?
If so, centroid.translate(dx, dy) is a void method: no value is actually returned, so
centroid =centroid.translate(dx, dy)
does not actually do anything.
Since you are just wanting a new centroid, try doing
centroid = getCentroid();
System.out.println(centroid.getX() + " "+ centroid.getY());
Point newCentroid = new Point(centroid.getX() + dx,centroid.getY() + dy);
System.out.println(newCentroid.getX() + " "+ newCentroid.getY());
There is no need to do the translate if all you want to do once you translate is make a new centroid.
Also, neither the translate function nor the constructor take a double (which makes sense if you are on a standard x,y graph), so you can make dx and dy int.