简体   繁体   English

如何移动点并打印新位置?

[英]How can I move a point and print the new position?

I'm new to Java and I received an assignment that asks me to write a class called PointerTester that has two Points as instance variables. 我是Java的新手,我收到了一个赋值,要求我编写一个名为PointerTester的类,它有两个Point作为实例变量。 I need to initialize one of these at coordinate (0.0,0.0) and one at (10.0,12.0). 我需要在坐标(0.0,0.0)和一个在(10.0,12.0)初始化其中一个。 Then I need to move each point by +2.0 in x and -3.0 in Y, query the coordinates of the points, and print out the values. 然后我需要将每个点移动x中的+2.0和Y中的-3.0,查询点的坐标,并打印出值。

So far I have this: 到目前为止我有这个:

public class PointerTester{
/*instance variables*/
    private double pointOneX;
    private double pointOneY;
    private double pointTwoX;
    private double pointTwoY;
    private double deltaX;
    private double deltaY;

    /*constructor to initialize*/
    public PointerTester (){
        pointOneX = 0.0;
        pointOneY = 0.0;
        pointTwoX = 10.0;
        pointTwoY = 12.0;
        deltaX = 2.0;
        deltaY = -3.0;
    }

    /*constructor to initialize to specific value*/
    PointerTester(double pointOneX, double pointOneY, double pointTwoX, double pointTwoY){
        this.pointOneX = pointOneX;
        this.pointOneY = pointOneY;
        this.pointTwoX = pointTwoX;
        this.pointTwoY = pointTwoY;
    }

    /*command to change value*/
    public void moveBy(double deltaX, double deltaY){
        this.pointOneX = this.pointOneX + deltaX;
        this.pointOneY = this.pointOneY + deltaY;
        this.pointTwoX = this.pointTwoX + deltaX;
        this.pointTwoY = this.pointTwoY + deltaY;
    }

    /*Queries*/
    public double getOneX(){
        return pointOneX;
    }
    public double getOneY(){
        return pointOneY;
    }
    public double getTwoX(){
        return pointTwoX;
    }
    public double getTwoY(){
        return pointTwoY;
    }
    /*print values*/
    public static void main(String[] args){
        PointerTester pointOne = new PointerTester();
        PointerTester pointTwo = new PointerTester();
        System.out.println("Point One after move (" + pointOne + ")");
        System.out.println("Point Two after move (" + pointTwo + ")");
    }
}

I cannot figure out how to correctly output the values or if I am completely wrong in working on this problem. 我无法弄清楚如何正确输出值,或者我是否完全错误地解决了这个问题。

Edit It seems I needed to use this code that I at first thought was supposed to be separate 编辑似乎我需要使用我最初认为应该是独立的代码

public class Point{
    public double x;
    public double y;


    public Point(double x, double y){
        this.x = x;
        this.y = y;
    }
    public void setX(double x){
        this.x = x;
    }
    public void setY(double y){
        this.y = y;
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
}

How do I incorporate this into my code? 如何将其合并到我的代码中?

I think to get started you should have a separate class called "Point" that encapsulates an X and Y value, and includes a "moveBy" method. 我认为要开始你应该有一个名为“Point”的单独的类,它封装了一个X和Y值,并包含一个“moveBy”方法。 It could also implement "toString()" such that "System.out.println" will print something nice for it. 它还可以实现“toString()”,这样“System.out.println”将为它打印一些不错的东西。 [Edit: Or just use java.awt.Point.] [编辑:或者只使用java.awt.Point。]

Beyond that, I'll leave that for you to do as your homework. 除此之外,我会把它留给你作为你的功课。

You might want to use some Point objects. 您可能想要使用一些Point对象。

Point p1 = new Point(); Point p1 = new Point(); Point p2 = new Point(10,12); 点p2 =新点(10,12);

you can then use the setLocation or translate methods found in the point class. 然后,您可以使用点类中的setLocation或translate方法。 Maybe the whole thing would look something like this? 也许整件事看起来像这样? Hope this helps. 希望这可以帮助。

main(){

//make some points
Point p1 = new Point();
Point p2 = new Point(10,12);

//move our points
p1.translate(2,-3);
p2.translate(2,-3);

//print our points
System.out.println(p1);
System.out.println(p2);
}

You can implement the toString method. 您可以实现toString方法。 There are examples here: http://www.javapractices.com/topic/TopicAction.do?Id=55 这里有一些例子: http//www.javapractices.com/topic/TopicAction.do?Id=55

You're trying to print the PointerTester object instead of the values contained within. 您正在尝试打印PointerTester对象而不是其中包含的值。 System.out.println(pointOne.getOneX()) should at least stop throwing exceptions and allow you to print correctly. System.out.println(pointOne.getOneX())至少应该停止抛出异常并允许您正确打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM