Got project that make points on the Cartesian coordinate system, and stuck on something.
Everything works fine but in the isUnder() and at isRight() field I got problem
when I enter point with the same y (for isUnder()) or the same x (for isRight()),
This is work for my degree, got some limitation about them..
I need both of them to
- use the other method ( for isUnder() use isAbove() and for isRight() use isLeft() )
- without using other operation and without using the property of the Point.
What can I do ?
here is the relevant code:
public class Point
{
private int _x;
private int _y;
/**
* creates a new Point object.
* @parm _x is the coordinates of a point on the x axis.
* @parm _y is the coordinates of the point on the y axis.
*/
public Point(int x, int y){
_x=x;
_y=y;
}
/** check if two points are in the same coordinates.
* @parm other the point to compare to this point.
* @return true if the points are in the same coordinates.
*/
public boolean equals (Point other){
if(_x==other._x && _y==other._y){
return true;
}else{
return false;
}
}
/** check if one point is above another.
* @parm other the point to compare to this point.
* @return true if the point is above the point we compare to.
*/
public boolean isAbove (Point other){
if(_y>other._y){
return true;
}else{
return false;
}
}
/** check if one point is under another.
* @parm other the point to compare to this point.
* @return true if the point is under the point we compare to.
*/
public boolean isUnder(Point other){
if(isAbove(other)){
return true;
}else{
return false;
}
}
/** check if one point is left to another.
* @parm other the point to compare to this point.
* @return true if the point is left to the point we compare to.
*/
public boolean isLeft(Point other){
if(_x<other._x){
return true;
}else{
return false;
}
}
/** check if one point is right to another.
* @parm other the point to compare to this point.
* @return true if the point is right to the point we compare to.
*/
public boolean isRight(Point other){
if(!isLeft(other)){
return true;
}else{
return false;
}
}
}
CodePudding user response:
You can return null when your coordinate(x or y) are same, and after check if it null.
public Boolean isLeft(Point other){
if(_x<other._x){
return true;
}
else if(_x==other._x){
return null;
}
else{
return false;
}
}
public boolean isRight(Point other){
if(isLeft(other)==null) {
return false;
}
return !isLeft(other);
}
CodePudding user response:
Just swap this
and other
.
public boolean isUnder(Point other) {
return other.isAbove(this);
}
public boolean isRight(Point other) {
return other.isLeft(this);
}
CodePudding user response:
You can simplify a lot your code and get rid of the unnecessary if conditions.
The issues you are getting when you are comparing two Point
s with the same y
(for isUnder()
) or the same x
(for isRight()
) are explained as follows:
isUnder()
: you delegate this toisAbove(other)
. In fact, you should have used!isAbove(other)
, but even then you would get an issue, becauseisAbove(other)
will returnfalse
if bothy
are the same and thenisUnder()
would returntrue
because you are negating the value fromisAbove(other)
.isRight()
: you deleagete this to!isLeft(other)
and then again if both have the samex
,isLeft(other)
will returnfalse
andisRight()
would returntrue
, but that is also wrong.
To fix this you also need to check in isUnder()
if y
are the same and in isRight()
if x
are the same. Something as follows:
public class Point
{
private int _x;
private int _y;
/**
* creates a new Point object.
* @parm _x is the coordinates of a point on the x axis.
* @parm _y is the coordinates of the point on the y axis.
*/
public Point(int x, int y){
_x=x;
_y=y;
}
/** check if two points are in the same coordinates.
* @parm other the point to compare to this point.
* @return true if the points are in the same coordinates.
*/
public boolean equals (Point other){
return _x == other._x && _y == other._y;
}
/** check if one point is above another.
* @parm other the point to compare to this point.
* @return true if the point is above the point we compare to.
*/
public boolean isAbove(Point other){
return _y > other._y;
}
/** check if one point is under another.
* @parm other the point to compare to this point.
* @return true if the point is under the point we compare to.
*/
public boolean isUnder(Point other){
return !isAbove(other) && (_y != other._y);
}
/** check if one point is left to another.
* @parm other the point to compare to this point.
* @return true if the point is left to the point we compare to.
*/
public boolean isLeft(Point other){
return _x < other._x;
}
/** check if one point is right to another.
* @parm other the point to compare to this point.
* @return true if the point is right to the point we compare to.
*/
public boolean isRight(Point other){
return !isLeft(other) && (_x != other._x);
}
}
Given that you are not supposed to use the properties to check this, then you could do the following:
public class Point
{
private int _x;
private int _y;
/**
* creates a new Point object.
* @parm _x is the coordinates of a point on the x axis.
* @parm _y is the coordinates of the point on the y axis.
*/
public Point(int x, int y){
_x=x;
_y=y;
}
/** check if two points are in the same coordinates.
* @parm other the point to compare to this point.
* @return true if the points are in the same coordinates.
*/
public boolean equals (Point other){
return _x == other._x && _y == other._y;
}
/** check if one point is above another.
* @parm other the point to compare to this point.
* @return true if the point is above the point we compare to.
*/
public boolean isAbove(Point other){
return _y > other._y;
}
/** check if one point is under another.
* @parm other the point to compare to this point.
* @return true if the point is under the point we compare to.
*/
public boolean isUnder(Point other){
return other.isAbove(this);
}
/** check if one point is left to another.
* @parm other the point to compare to this point.
* @return true if the point is left to the point we compare to.
*/
public boolean isLeft(Point other){
return _x < other._x;
}
/** check if one point is right to another.
* @parm other the point to compare to this point.
* @return true if the point is right to the point we compare to.
*/
public boolean isRight(Point other){
return other.isLeft(this);
}
}
This means you would basically check if this
is under other
by checking if other
is above this
. Similarly, you would check if this
is at the right of other
by checking if other
is at the left of this
.