I have this uml diagram and I don't understand a part of a code that I have from my friend . I don't understand the "equals" method.. What is used for and why should I use it ?
This is the UML :
But I don't understand some part of the code .
this is a part of the code so far :
class Artist {
private String name;
Artist(String name) { // constructor
this.name = name;
}
public String getName() { // name getter
return name;
}
public String toString() { // toString
return name;
}
}
class CD {
private String title;
private Artist name;
CD(String title) {
this.title = title;
}
CD(String title, Artist name) {
this.title = title;
this.name = name;
}
public Artist getArtist() {
return name;
}
public String getTitle() {
return title;
}
public String toString() {
return title + " by " + getArtist();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CD other = (CD) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}