我正在尝试在Java中计算两点之间的距离。

huangapple go评论72阅读模式
英文:

I am trying to calculate the Distance Between 2 points in Java

问题

import java.util.Random;

class MainLab_2 {
    private static Random rand = new Random();

    private static int getInt() {
        int z = rand.nextInt(199) - 99; // Generate random number from -99 to 99
        return z;
    }

    public static void main(String[] args) {
        Point p1 = new Point(getInt(), getInt());
        Point p2 = new Point(getInt(), getInt());
        Line line = new Line(p1, p2);

        System.out.println(p1.toString());
        System.out.println(p2.toString());
        System.out.println(line.toString());
    }
}

class Point {
    private int x, y;

    public Point() {
    }

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Point(Point p) {
        this(p.x, p.y);
    }

    private double distance(Point p) {
        return Math.sqrt((p.y - this.y) * (p.y - this.y) + (p.x - this.x) * (p.x - this.x));
    }

    public double getDistance(Point p) {
        return distance(p);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void set(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public String toString() {
        return String.format("Given Point (%d, %d)%n", getX(), getY());
    }
}

class Line {
    private Point p1, p2;

    public Line() {

    }

    public Line(Point p1, Point p2) {
        this.p1 = new Point(p1);
        this.p2 = new Point(p2);
    }

    public Line(Line aline) {
        this(aline.p1, aline.p2);
    }

    public double getDistance() {
        return p1.getDistance(p2);
    }

    public Point getP1() {
        return p1;
    }

    public Point getP2() {
        return p2;
    }

    public void set(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public String toString() {
        return String.format("Line (Point(%d,%d), Point(%d,%d), distance = %.4f)%n",
                p1.getX(), p1.getY(), p2.getX(), p2.getY(), getDistance());
    }
}

Note: I've made corrections to the provided code. Please use this corrected code to achieve the desired functionality of generating random points and calculating distances.

英文:

import java.util.Random;

class MainLab_2 {
private static Random rand;
private static int getInt() {
int z = rand.nextInt() * 100;
return z;
}
private void getTwoPoints(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
private static void main(String[] args) {
Point p1 = new Point(getInt(), getInt());
Point p2 = new Point(getInt(), getInt());
}
}
class Point {
private static int x, y;
// Default Constructor 
public Point() {
}
// Other constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// Copy constructor
public Point(Point p) {
this(p.x, p.y);
}
private static double distance(Point p) {
return Math.sqrt((p.y - this.y) * (p.y - this.y)
+ (p.x - this.x) * (p.x - this.x));
}
// Accessor Method 	
public double getDistance(Point p) {
return	distance(Point p);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// Mutator Method
public void set(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return String.format("Given Point (%d, %d%n)", getX(),
getY());
}
}
class Line {
private static Point p1, p2;
public Line() {
}
public Line(Point p1, Point p2) {
this(p1);
this.p2 = new Line(p2);
}
public Line(Line aline) {
this(aline.p1, aline.p2);
}
// Accessor Method
public double getDistance() {	
return Point.getDistance(Point p);
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
// Mutator Method
public void set(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
private String toString() {	
return String.format ("Line (Point(%d,%d), Point(%d,%d), distance = %d%n )", 
p1.x,p1.y,p2.x,p2.y,getDistance())
}

As attached above, is my script.

Firstly I'm trying to randomly generate any 2 number from -99 to 99.

After generating I'll pass this 2 number into my class Point as
(x, y).

Generate 2 points (p.x, p.y) and (this.x, this.y) before calculating the distance between the 2 points. The formula I'm currently using was found online.

My End Results to get is a few sets of e.g

Set 1

Given Point (-13,90)

Given Point (39, 16)

Line (Point (-13,90),Point (39, 16), distance = 90.4434 )


Set 2

Given Point (89,24)

Given Point (33, -68)

Line (Point (89,24),Point (23, -68), distance = 113.2254 )


I hope to random 2 numbers from my main class and pass it to my class Point. And in my class Line I'm able to generate 2 points and calculate the distance between them. However, I'm unable to do that. with my current script. It will be very helpful if somebody can give me some guidance on how I can correct it. I'm fairly new to java and really hope I can get some pointers and step by step guidance on this. Thank you.

答案1

得分: 1

这并没有解决您的问题,因为您的代码存在严重问题。以下将指出其中一些问题。

class MainLab_2 {

类MainLab_2中没有p1p2这些点。而且甚至不清楚为什么这个方法应该存在。

    private void getTwoPoints(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

主入口点需要是public的。

    private static void main(String[] args) {
        Point p1 = new Point(getInt(), getInt());
        Point p2 = new Point(getInt(), getInt());
    }
}

class Point {

这些不应该是静态的,否则所有的点将与您创建的最后一个点具有相同的值。它们需要是实例字段。

    private static int x, y;

在静态上下文中无法使用this。静态方法不知道this指的是什么。

    private static double distance(Point p) {
        return Math.sqrt((p.y - this.y) * (p.y - this.y)
                + (p.x - this.x) * (p.x - this.x));
    }

class Line {

这些同样不应该是静态的。

    private static Point p1, p2;

您没有单参数的Line构造函数,也不合理。

 public Line(Point p1, Point p2) {
        this(p1); // <-- 这个构造函数引用不存在
        this.p2 = new Line(p2);
    }

将其设为私有将没有太大用处。它应该可以被此类的所有用户调用。

    private String toString() { 
        return String.format ("Line (Point(%d,%d), Point(%d,%d), distance = %d%n )", 
                            p1.x,p1.y,p2.x,p2.y,getDistance())
   }

总结一下:

  • 理解静态和非静态之间的区别。
  • 阅读有关构造函数以及如何使用它们的内容。
  • 了解不同的访问修饰符。

查阅Java教程获取更多信息。

英文:

This does not address your question as you have serious problems in your code. This will point some of them out to you.

class MainLab_2 {

There are no points p1, and p2 in class MainLab_2. Nor is it clear why this method should even be here.

    private void getTwoPoints(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}

The main entry point needs to be public

    private static void main(String[] args) {
Point p1 = new Point(getInt(), getInt());
Point p2 = new Point(getInt(), getInt());
}
}

class Point {

These should not be static, otherwise all your points will have the same values as the last one you created. They need to be instance fields.

    private static int x, y;

You can't use this within a static context. A static method has no idea what this refers to.

    private static double distance(Point p) {
return Math.sqrt((p.y - this.y) * (p.y - this.y)
+ (p.x - this.x) * (p.x - this.x));
}

class Line {

Once again theses should not be static.

    private static Point p1, p2;

you have no single argument Line constructor, nor would it make sense.

 public Line(Point p1, Point p2) {
this(p1); // &lt;-- this constructor reference doesn&#39;t exist
this.p2 = new Line(p2);
}

This won't be of much use making it private. It should be able to called by
all users of this class.

    private String toString() { 
return String.format (&quot;Line (Point(%d,%d), Point(%d,%d), distance = %d%n )&quot;, 
p1.x,p1.y,p2.x,p2.y,getDistance())
}

To summarize.

  • Understand the difference between static and non-static
  • Read up on constructors and how they should be used.
  • learn about the different access modifiers.

Check out The Java Tutotals for more information.

答案2

得分: -1

这是使用不可变对象的简单解决方案。

Point.java

public class Point {
    
    private static final int LOW = -100;
    
    private static final int HIGH = 100;
    
    private static final Random rand = new Random();

    private static int rand() {
        return rand.nextInt(HIGH - LOW) + LOW;
    }
    
    private final int x;
    
    private final int y;

    public Point() {
        this(rand(), rand());
    }
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
    
    public double getDistance(Point p) {
        return Math.hypot(x - p.x, y - p.y);
    }

    @Override
    public String toString() {
        return "Point [x=" + x + ", y=" + y + "]";
    }
    
}

Line.java

public class Line {

    private final Point p1;
    
    private final Point p2;
    
    private final double length;
    
    public Line() {
        this(new Point(), new Point());
    }

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
        this.length = p1.getDistance(p2);
    }

    public Point getP1() {
        return p1;
    }

    public Point getP2() {
        return p2;
    }
    
    public double getLength() {
        return length;
    }

    @Override
    public String toString() {
        return "Line [p1=" + p1 + ", p2=" + p2 + ", length=" + length + "]";
    }
    
    public static void main(String[] args) {
        Line l1 = new Line();
        Line l2 = new Line();
        System.out.println(l1);
        System.out.println(l2);
    }
    
}
英文:

Here is a simple solution using immutable objects.

Point.java

public class Point {
private static final int LOW = -100;
private static final int HIGH = 100;
private static final Random rand = new Random();
private static int rand() {
return rand.nextInt(HIGH - LOW) + LOW;
}
private final int x;
private final int y;
public Point() {
this(rand(), rand());
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public double getDistance(Point p) {
return Math.hypot(x - p.x, y - p.y);
}
@Override
public String toString() {
return &quot;Point [x=&quot; + x + &quot;, y=&quot; + y + &quot;]&quot;;
}
}

Line.java

public class Line {
private final Point p1;
private final Point p2;
private final double length;
public Line() {
this(new Point(), new Point());
}
public Line(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
this.length = p1.getDistance(p2);
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public double getLength() {
return length;
}
@Override
public String toString() {
return &quot;Line [p1=&quot; + p1 + &quot;, p2=&quot; + p2 + &quot;, length=&quot; + length + &quot;]&quot;;
}
public static void main(String[] args) {
Line l1 = new Line();
Line l2 = new Line();
System.out.println(l1);
System.out.println(l2);
}
}

huangapple
  • 本文由 发表于 2020年4月8日 20:52:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定