Currently I am working on a project in which I have to use this UML diagram to create and additional class I understand everything but I am baffled by the last line.
compareTo(wordToCompare : Word) : Integer
Since Word is the name of the class how would I insert an argument? I tried inputting an Object as an argument but it says:
Object is an incompatible type with Word.
I researched if Word was a non primitive data-type but could not find any information. I am rather inexperienced and quite confused if anyone could lend some assistance I would be greatly appreciative.
Word class UML
-wordCharacters : String
-count : integer
+ CONSTRUCTOR (word : String)
+ getWord() : String
+ getCount() : Integer
+ incrementCount() : void
+ toString() : String
+ equals(wordtoCompare : Object) : Boolean
+ compareTo(wordtoCompare : Word) : Integer
@Override
public boolean equals(Object wordtoCompare) {
boolean flag = false;
String currentWord = wordtoCompare.getClass().getName();
this.compareTo(wordtoCompare);
return flag;
}
public Integer compareTo(Word wordtoCompare) {
return 0;
}
you are overriding the equals method and it should look a bit like this.
You need to check if the Object
wordtoCompare
is of the type Word
or if it´s the current object. After you did check if it´s a type or subtype of Word
you can cast the wordtoCompare
to an actuall Word
object and do your stuff with it.
@Override
public boolean equals(Object wordtoCompare) {
boolean flag = false;
if(wordtoCompare == this) return true;
if(!(wordtoCompare instanceof Word)) return false;
Word word = (Word)wordtoCompare;
this.compareTo(word);
// Whatever you do with your flag
return flag;
}