Let's consider the following simple class.
class Point {
private float x;
private float y;
public Point(float x, float y){
this.x=x;
this.y=y;
}
public float getX(){return this.x;}
public float getY(){return this.y;}
public void setX(float x){this.x=x;}
public void setY(float y){this.y=y;}
#Override
public String toString(){
return ("x = "+this.x+", y = "+this.y+";");
}
#Override
public Point clone(){
return new Point(this.x,this.y);
}
#Override
public boolean equals(Object object){
if (object != null && object.getClass()==Point.class){
return object.getX()==this.x && object.getY()==this.y;
}
else{
return false;
}
}
The problem is in the rewrite of method equals: I use the general Object class as attribute to make it more flexible, but netbeans prints error on return line: "Object has no method getX" which is perfectly logical.
But the problem is still here, how can I manage to fix this?
Thanks you in advance
This is pretty simple but you need to cast object:
#Override
public boolean equals(Object object){
if (object != null && object.getClass()==Point.class){
Point p = (Point)object;
return p.getX()==this.x && p.getY()==this.y;
}
else{
return false;
}
}
This is also relevant: Casting in equals method
Related
I need to create a class called Point, this Object will have parameters of X and Y. Then I had to make a method which checks if that point is above another point, so I did that:
public boolean isAbove(Point1 other) {
if(other.getY() > _y) { return true; }
return false;
}
And the next method I had to do called "isUnder", but in this method I must only use the "isAbove" method I've created. This is the "isUnder" method I've created:
public boolean isUnder(Point1 other) {
return !isAbove(other);
}
How can I know if the point aren't at the same "height"? With the methods I created the only results I get are TRUE, FALSE, but if the points are the same "height" I should get FALSE,FALSE on both methods.
class Point {
private double x;
private double y;
public PointYPosition getYPosition(Point otherPoint) {
if (otherPoint.y > y) return PointYPosition.ABOVE;
if (otherPoint.y < y) return PointYPosition.UNDER;
return PointYPosition.EQUALS;
}
}
enum PointYPosition {
ABOVE,
UNDER,
EQUALS
}
You can use the following implementation, however I must say if you need to only use the other method, there is no way
public boolean isUnder(Point1 other) {
return !isAbove(other) && other.getY() != _y;
}
That way you can even have a third method isEqual
public boolean isEqual(Point1 other) {
return !isAbove(other) && !isUnder(other);
}
That being said here is how I would implement this myself
public boolean isAbove(Point1 other) {
return other.getY() < _y;
}
public boolean isUnder(Point1 other) {
return other.getY() > _y;
}
public boolean isOnTheSameHeight(Point1 other) {
return !isAbove(other) && !isUnder(other);
}
public boolean isAboveOrOnTheSameHeight(Point1 other) {
return isAbove(other) || isOnTheSameHeight(other);
}
// etc.
You can do something like this:
public int compareTo(Point otherPoint) {
return Integer.compare(_y, otherPoint.getY());
}
The Integer.compare(x, y) returns -1 if x is less than y, returns 0 if they're equal, and returns 1 otherwise.
Apparently I found a post from 7 years ago with the exact same question, someone solved it there
return other.isAbove(this);
Thank you all very much!
I have a project with two java files. One is the class withe the main-method and the other is a interface with two methods, which is impelemented in the Java-class and I did override the functions there.
This is my Code from the Java-Class:
public class Point implements Compare {
int y;
int x;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isEqualTo(Point x) {
if ((this.x == x.getX()) && (this.y == x.getY()))
return true;
else
return false;
}
public boolean isSmallerThan(Point x) {
if (this.x < x.getX())
return true;
else if (this.y < x.getY())
return true;
else
return false;
}
public static void main(String[] args) {
System.out.println("Test");
}
}
And this is the Code from my Interface:
public interface Compare {
public boolean isEqualTo();
public boolean isSmallerThan();
}
When I try to run the code i always get the following error:
Point.java:1: error: Point is not abstract and does not override abstract method isSmallerThan() in Compare
public class Point implements Compare {
^
1 error
The strange thing is now, that the same code works when i write it in project in IntelliJ IDEA.
I havent found anything on the internet yet.
ah, and i work on macOS.
Hopefully anybody can help, why the code doesnt work in VSC.
Thanks
Actually, it's not Visual Studio Code. It's a Java compiler.
It tells you "If you declared interface methods and then implementing class with this interface you have to implement them or use abstract class. The method signature should be the same."
I am going through the Effective Java 3rd edition and I was reading Item 10: Follow Equals contract when overriding.
There is an example there which I was trying to simulate on my machine. Below is the code for the same.
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
#Override
public boolean equals(Object obj) {
if (!(obj instanceof Point))
return false;
Point p = (Point)obj;
return (x == p.x) && (y ==p.y);
}
// Use this for demonstration with AtomicPoint class.
/*#Override
public boolean equals(Object obj) {
if(obj == null || (obj.getClass() != getClass())) {
return false;
}
Point p = (Point)obj;
return p.x == x && p.y == y;
}*/
}
public class AtomicPoint extends Point{
private static final AtomicInteger counter = new AtomicInteger();
public AtomicPoint(int x, int y) {
super(x, y);
counter.incrementAndGet();
}
public static int getCounter() {
return counter.get();
}
private static Set<Point> sampleSet = new HashSet<>();
public static void main(String[] args) {
sampleSet.add(new Point(1,2));
sampleSet.add(new Point(1,3));
sampleSet.add(new Point(1,4));
AtomicPoint ap = new AtomicPoint(1,3);
// This should return true but its returning false
System.out.println(sampleSet.contains(ap));
}
}
As you can see from the comment in the AtomicPoint class, I am getting false for the contains check, whereas Joshua Bloch states that this should return true. Can someone help me here?
For using HashSet<T> or HashMap<T> you need to override hashCode() methods from super class. You should have in your editor automatically generating hashCode() and equals() methods (and i'm suggesting you to use that always in every class). If you want to use TreeSet<T> or TreeMap<T> you will need to implement Comparable or Comparator<T> interface and override their compare() or compareTo() methods for using it.
I have a point class:
class Point
{
public int x;
public int y;
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
And I have a map to store the values:
Map<Point,Integer> map = new HashMap<Point,Integer>();
map.put(new point(1,1)) = 10;
I want to get the values in map by a definite point:
map.get(new point(1,1));
but it returns null.
It may because of the difference of their reference.
I want to know how to fix it,instead of using a two-dimensional array.
when using structures like Map class you should implement the equals and hashCode method so when the get method of Map get called these methods will call respectively
something like this:
class Point {
public int x;
public int y;
public Point(int x,int y)
{
this.x=x;
this.y=y;
}
#Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
#Override
public int hashCode() {
return Objects.hash(x, y);
}
}
I'm supposed to write a Cruiser class which implements the Locatable interface. Cruiser will have x, y, and speed properties. x, y, and speed are integer numbers. You must provide 3 constructors for class Cruiser. Class Cruiser must implement the Locatable interface.
One constructor must be a default. One constructor must be an x and y only constructor. One constructor must be an x, y, and speed constructor. You must provide an equals method. The equals() method should compare the properties of two Cruiser Objects. You must provide a toString() method. The toString() should return the x, y, and speed of the Cruiser.
When i compile this it says "class Cruiser is public, should be declared in Cruiser.java"
When i do this my IDE says ";" as expected after public boolean equals. but that doesn't make sense why you would need a semicolon in a method.
this is what i have so far
public interface Locatable
{
public int getxPos();
public int getyPos();
}
public class Cruiser implements Locatable
{
private int xPos, yPos, speed;
public Cruiser()
{
xPos=yPos=speed=0;
}
public Cruiser(int x,int y)
{
xPos=x;
yPos=y;
speed=0;
}
public Cruiser(int x, int y, int spd)
{
xPos=x;
yPos=y;
speed=spd;
}
public int getxPos()
{
return xPos;
}
public int getyPos()
{
return yPos;
}
public int getSpeed()
{
return speed;
}
public void compare(Cruiser A, Cruiser B)
{
#Override
public boolean equals(Object obj)
{
if (obj instanceof Cruiser) {
Cruiser cruiserToCompareTo = (Cruiser)obj;
if(xPos == cruiserToCompareTo.getXpos() &&
yPos == cruiserToCompareTo.getYpos() &&
speed == cruiserToCompareTo.getSpeed())
return true;
}
return false;
}
public String toString()
{
String properties = "X position:"+ xPos+ ", Y position:"+yPos+ ",Speed:"+speed;
return properties;
}
}
}
Here is what it means that equals should compare the fields/properties.
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point pt = (Point)obj;
return (x == pt.x) && (y == pt.y);
}
return super.equals(obj);
}
This example is taken from java.awt.Point
which is one of the Java's built-in classes.
So Point has 2 properties x and y, and
in its equals method it is comparing them.
See also:
java.awt.Point.equals
You have a Cruiser A with properties x,y,speed and you have Cruiser B with x,y,speed. Equals shall return true, if A.x == B.x, A.y == B.y, A.speed == B.speed all are true. Cruiser A and Cruiser B are therefor equal, when all parameters are equal.
Edit: Following Code should be entered into Cruiser-Class
public int getSpeed() {
return speed;
}
#Override
public boolean equals(Object obj) {
if (obj instanceof Cruiser) {
Cruiser cruiserToCompareTo = (Cruiser)obj;
if(xPos == cruiserToCompareTo.getXpos() &&
yPos == cruiserToCompareTo.getYpos() &&
speed == cruiserToCompareTo.getSpeed())
return true;
}
return false;
}
And please: Format you code better and furthermore name methods in Cruiser-Class same way as in the interface.
Edit 2: Regarding your second problem:
You need to have to separate files, Cruiser.java and Locatable.java. Code runs well here