/**
* A simple class to store 2 points so we can practice with comparisons, conditional statements, and logical operators
*
* @author Chuck Cusack
* @version 9/11/25
*/
public class TwoPoints
{
private Point p1;
private Point p2;
public TwoPoints(Point point1, Point point2) {
p1 = point1;
p2 = point2;
}
/**
* @return true if point1 and point2 have the same name,
* and false otherwise
*/
public boolean sameName() {
// When comparing two object types (in this case Strings),
// we typically need to use .equals instead of ==.
// See below for a version that is maybe a little clearer.
if(p1.getName().equals(p2.getName())) {
return true;
} else {
return false;
}
}
public boolean sameNameV2() {
String name1 = p1.getName();
String name2 = p2.getName();
// If we asked if(name1==name2) we would be asking if name1 and name2
// refer to (or point to) the same objects, NOT whether or not the strings
// have the same contents. But we want to know if they have the same contents,
// so we use equals. We will talk about .equals more as the semester progresses.
if(name1.equals(name2)) {
return true;
} else {
return false;
}
}
public boolean sameLocation() {
int x1=p1.getX();
int y1=p1.getY();
int x2=p2.getX();
int y2=p2.getY();
if(x1==x2 && y1==y2) {
return true;
} else {
return false;
}
// Or the short way:
//return p1.getX()==p2.getX() && p1.getY()==p2.getY();
}
}