gpt4 book ai didi

java - 点类坐标

转载 作者:行者123 更新时间:2023-12-02 02:49:43 24 4
gpt4 key购买 nike

我有一个作业是这样解释的:

Write a definition of a class named Point that might be used to store and manipulate the location of a point on the plane. The point is stored as two coordinates: x and y. You will need to declare and implement the following methods:

  1. Two constructors:a. no-argument constructor that sets the point coordinates to (0,0), andb. a constructor that takes x and y coordinate of the point and sets membervariables.
  2. Method set that sets the private data after an object of this class is created.
  3. A method to move the point by an amount along the vertical and horizontal directions specified by the first and second arguments: move(double dx, double dy)
  4. The method to rotate the point by 90 degrees clockwise around the origin. Hint: when point is getting rotated 90 clockwise around the origin the following changes happen to its coordinates: xrotated = y; yrotated = -x .
  5. two accessor methods to retrieve the coordinates of the point.

这应该是两个不同的调用。这些项目在第二个(不是主类)我将在主课上调用它。 (我就是这么做的)。

这是我的代码,但我不明白下一步应该做什么。

private double x;    
private double y;


public Point(double initialX, double initialY) {
x = initialX;
y = initialY;
}


public Point() {
x = 0;
y = 0;
}


public double getX() {
return x;
}


public double getY() {
return y;
}


public void move(double dx, double dy) {
x += dx;
y += dy;
}

最佳答案

你几乎拥有了一切,干得好。对于旋转 90 度函数,您已经获得了关于要做什么的很好的线索。假设您有一个点 (1,2),然后旋转它。你最终会得到(2,-1)。如果你再次旋转它,你会得到(-1,-2)。再次旋转得到 (-2,1),第四次 90 度旋转得到 (1,2),这就是你开始时的结果。想出一个可以做到这一点的函数。它不应超过 3 行。

设置函数(用于设置或更改值的函数)只是可用于设置点的值的函数。因此,您将有一个函数“setX(...) { ... }”和一个函数“setY(...) { ... }”。这些应该非常简单。

如果您仍然感到困惑,请随时提出进一步的问题。

关于java - 点类坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44035393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com