Reputation: 147
I am having trouble figuring out how to do this. What I am trying to do is receive an X and a Y both are int and add those to the X and Y of both endpoints. For example, if one endpoint is (1,2) and other is (5,6) and the number that is received is (3,3). Then the final endpoints must be (4,5) and (8,9). Here is my code so far...
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;
}
I cannot figure out how to add it to the points. Please help thank you.
Upvotes: 0
Views: 345
Reputation: 944
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));
}
Upvotes: 2
Reputation: 140
It seems like you are adding xmove
and ymove
to the both Point1
and Point2
.
I assume that Point class consists of int x and y that have public visibility.
public void translate (int xmove, int ymove) {
this.Point1.x += xmove; this.Point2.y += ymove;
this.Point2.x += xmove; this.Point2.y += ymove;
}
Assume that Point class does have limited visibility.
If Point class do not have setters but getters,
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);
}
If Point class have both getters and setters
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);
}
Also, note that it is not recommended to name a variable starting with capital letter such as your Point1 and Point2 variables. It gives confusion between class name and variable name. Exception to this practices will be constant values such as final static Point ORIGIN
where all letters are typically capitalized.
Upvotes: 1
Reputation: 610
Try this:
public void translate (int xmove, int ymove)
{
this.Point1.x += xmove;
this.Point1.y += ymove;
this.Point2.x += xmove;
this.Point2.y += ymove;
}
Also, like others have said, you need to use .equals
instead of ==
when comparing two points. Also you need to assign the points correctly, here I have cleaned up your code a bit:
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;
}
Upvotes: 1
Reputation: 4202
You could create a method inside your Point
class named incrementPoint(int x, int y)
:
public void incrementPoint(int x, int y) {
this.x += x;
this.y += y;
}
Then, on your translate
method just do the following:
public void translate (int xmove, int ymove) {
this.point1.incrementPoint(xmove, ymove);
this.point2.incrementPoint(xmove, ymove);
}
And instead of using capital letters to start the name of your class attributes, use the standard camelcase.
Upvotes: 2