(Java)坚持方法

时间:2016-02-15 22:14:44

标签: java math methods

我无法弄清楚如何做到这一点。我要做的是接收一个X和一个都是int并将它们添加到两个端点的X和Y.例如,如果一个端点是(1,2)而另一个端点是(5,6),则接收的数字是(3,3)。然后最终端点必须是(4,5)和(8,9)。到目前为止,这是我的代码......

public class Segment
{
private Point Point1;
private Point Point2;

public Segment ( )
{
    this.Point1 = new Point(0, 0);
    this.Point2 = new Point(7, 7);
}

public Segment (int x1, int y1, int x2, int y2)
{
    if (x1 == x2 && y1 == y2)
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (x1,y1);
            Point2 = new Point (x2,y2);
        }
}

public Segment (Point p1, Point p2)
{
    if (p1 == p2)
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (p1);
            Point2 = new Point (p2);
        }
}

public Segment (Segment other)
{
    Point1 = new Point (other.Point1);
    Point2 = new Point (other.Point2);
}
public String toString ( )
{
    return (Point1) + "---------" + (Point2);
}

//More codes

public void translate (int xmove, int ymove) // <--- Need help here.
{
    this.Point1 = ((x1 + xmove), (y1 + ymove));
    this.Point2 = this.Point2 + ymove;
}

我无法弄清楚如何将它添加到点。请帮忙谢谢。

4 个答案:

答案 0 :(得分:2)

您可以在名为Point的{​​{1}}类中创建一个方法:

incrementPoint(int x, int y)

然后,在public void incrementPoint(int x, int y) { this.x += x; this.y += y; } 方法上,执行以下操作:

translate

而不是使用大写字母来启动类属性的名称,而是使用标准camelcase

答案 1 :(得分:2)

public void translate (int xmove, int ymove) // <--- Need help here.
{
    this.Point1 = new Point((this.Point1.x + xmove), (this.Point1.y + ymove));
    this.Point2 = new Point((this.Point2.x + xmove), (this.Point2.y + ymove));
}

答案 2 :(得分:1)

试试这个:

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}

此外,与其他人一样,在比较两点时,您需要使用.equals代替==。你还需要正确分配点,这里我已经清理了你的代码了一下:

public class Segment
{
private Point Point1;
private Point Point2;

public Segment ( )
{
    this.Point1 = new Point(0, 0);
    this.Point2 = new Point(7, 7);
}

public Segment (int x1, int y1, int x2, int y2)
{
    if (x1.equals(x2) && y1.equals(y2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = new Point (x1,y1);
            Point2 = new Point (x2,y2);
        }
}

public Segment (Point p1, Point p2)
{
    if (p1.equals(p2))
        throw new IllegalArgumentException ("Error, this would cause the segment to be 0. Please re-enter two different value.");
    else
        {
            Point1 = p1;
            Point2 = p2;
        }
}

public Segment (Segment other)
{
    Point1 = other.Point1;
    Point2 = other.Point2;
}
public String toString ( )
{
    return (Point1) + "---------" + (Point2);
}

//More codes

public void translate (int xmove, int ymove)
{
    this.Point1.x += xmove;
    this.Point1.y += ymove;
    this.Point2.x += xmove;
    this.Point2.y += ymove;
}

答案 3 :(得分:1)

您似乎正在向xmoveymove添加Point1Point2

我假设Point类由具有公共可见性的int x和y组成。

public void translate (int xmove, int ymove) {
  this.Point1.x += xmove; this.Point2.y += ymove;
  this.Point2.x += xmove; this.Point2.y += ymove;
}

假设Point类的可见性有限。

如果Point类没有setter而是getter,

public void translate (int xmove, int ymove) {
  this.Point1 = new Point(this.Point1.getX() + xmove, this.Point1.getY() + ymove);
  this.Point2 = new Point(this.Point2.getX() + xmove, this.Point2.getY() + ymove);
}

如果Point类同时具有getter和setter

public void translate (int xmove, int ymove) {
  this.Point1.setX(Point1.getX() + xmove); this.Point1.setY(Point1.getY() + ymove);
  this.Point2.setX(Point2.getX() + xmove); this.Point2.setY(Point2.getY() + ymove);
}

另请注意,建议不要将以大写字母开头的变量命名为Point1和Point2变量。它使类名和变量名之间产生混淆。此实践的例外情况是常量值,例如final static Point ORIGIN,其中所有字母通常都是大写的。