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."
Related
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.
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
My class details attributes of restaurants downtown, said attributes being x/y locations and rank. The problem is, whenever I run the program It throws an error, saying that non-abstract class "Downtown" does not override abstract method "compareTo". I cannot make this class abstract because I need to initialise the object outside this block of code. Where does my program go wrong? Is there a problem with my compareTo implementation? Any suggestions will be much appreciated.
public class Downtown implements Comparable<Downtown> {//Throws error on this line
private int x;
private int y;
private int rank;
public Downtown(int x, int y, int rank) {
this.x = x;
this.y = y;
this.rank = rank;
}
//Appropriate setters and getters for x , y and rank
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int compareTo(Downtown p1, Downtown p2)//Actual comparison
{
// This is so that the sort goes x first, y second and rank last
// First by x- stop if this gives a result.
int xResult = Integer.compare(p1.getX(),p1.getX());
if (xResult != 0)
{
return xResult;
}
// Next by y
int yResult = Integer.compare(p1.getY(),p2.getY());
if (yResult != 0)
{
return yResult;
}
// Finally by rank
return Integer.compare(p1.getRank(),p2.getRank());
}
#Override
public String toString() {
return "["+x+' '+y+' '+rank+' '+"]";
}
Java's Comparable<T> interface defines compareTo method as follows:
int compareTo(T o);
This means that the method must take one parameter; the other parameter is the object itself, i.e. this. You need to implement this one-argument method in place of your two-argument method to fix this problem.
Compiler will help you figure out issues like this by using #Override annotation on your method:
#Override // Issues an error
public int compareTo(Downtown p1, Downtown p2)
#Override // Compiles fine
public int compareTo(Downtown other)
The compareTo method should compare the current object (this) to just one other. It shouldn't have two parameters for comparison. You could write your method like this.
public int compareTo(Downtown p2)//Actual comparison
{
// This is so that the sort goes x first, y second and rank last
// First by x- stop if this gives a result.
int xResult = Integer.compare(getX(),p2.getX());
if (xResult != 0)
{
return xResult;
}
// Next by y
int yResult = Integer.compare(getY(),p2.getY());
if (yResult != 0)
{
return yResult;
}
// Finally by rank
return Integer.compare(getRank(),p2.getRank());
}
Notice how I've replace all the calls on p1 to calls on the current object.
I have the following code:
package relational;
public class compare
{
public int getMax(int x,int y)
{
if(x>y){
return x;
}
else{
return y;
}
}
}
import relational.compare;
public class Pack
{
public static void main(String a[])
{
int a=7,b=9;
compare ob=new compare();
int max=ob.getMax(a,b);
}
}
This won't compile for some reason.
please help me solve this error.
Is the error class-related, interface or enum-related?
the problem seems to be in import relational.compare.
There are several issue with this code.
If this whole code is in one java file then there cab be only one public class.
Change the name of class from "compare" to "Compare" (C in upper case), because object you are making have 'C' in upper case.
You have two variables with same name 'a', change any one of them.
After these changes your code will work fine.
Your problem is that only one class can be public in a file.
you have both compare and pack as public.
try to separate them into 2 files.
OR
do this:
package relational;
import relational.Compare;//not really needed at all because they are in the same file
public class Pack {
public static void main(String args[]) {
int a = 7, b = 9;
Compare ob = new Compare();
int max = ob.getMax(a, b);
}
}
class Compare {
public int getMax(int x, int y) {
if (x > y) {
return x;
}
else {
return y;
}
}
}
There is an example of "Implementing an Interface" in Java tutorial. I have repeated this example but it doesn't work. NetBeans shows the mistake on te left of RectanglePlus class declaration. And mistake is:
rectangleplus.RectanglePlus is not abstract and does not override
abstract method isLargerThan(rectangleplus.Relatable) in
rectangleplus.Relatable
I did the same as written in tutorial. Why it shows the mistake? Here is my implementation of the project.
The name of the project is RectanglePlus.
The name of the package is rectangleplus.
1st file in the project is Interface Relatable:
package rectangleplus;
public interface Relatable {
int isLarger(Relatable other);
}
2nd file in the project is Main Class RectanglePlus with helper class Point:
package rectangleplus;
public class RectanglePlus implements Relatable {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public RectanglePlus() {
origin = new Point(0, 0);
}
public RectanglePlus(Point p) {
origin = p;
}
public RectanglePlus(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public RectanglePlus(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing
// the area of the rectangle
public int getArea() {
return width * height;
}
// a method required to implement
// the Relatable interface
public int isLargerThan(Relatable other) {
RectanglePlus otherRect
= (RectanglePlus)other;
if (this.getArea() < otherRect.getArea())
return -1;
else if (this.getArea() > otherRect.getArea())
return 1;
else
return 0;
}
public static void main(String[] args) {
// TODO code application logic here
}
}
class Point {
int top;
int left;
int x;
int y;
public Point(int t, int l) {
top = t;
left = l;
}
}
Why there is nothing said about abstraction in the tutorial example? Should the tutorial example work without mitakes?
Thank you.
In the interface, you declare the method isLarger but in the class you declare isLargerThan Change one to the other name and it will go fine.
You're not correctly implementing the isLarger() method in the Relatable interface. Rename the isLargerThan(Relatable other) method so it looks like this:
#Override
int isLarger(Relatable other) {
}
It's a good idea to use the #Override annotation, it allows you to catch errors like the one in the question.