I am trying to read in a company vector and returns true if two companies have the same name, false otherwise, which I did using the ComapareTo method. In my method, which will call the "equals" method in the Company class. My "equals" method should override the corresponding method in the Object class.
What I want to know is that you can override CompareTo using the Equal method. If so, how can I test it basically.
The purpose of this program is to check 2 companies in a vector, if so return true else false. Here is the code that I stuck mostly to test it.
public class Company implements Comparable <Company> { private String cName; public String getName() { return cName; } public int compareTo(Company b) { if(this.cName == b.cName) { System.out.println(" from compareTo true"); return 1; } else { System.out.println(" from compareTo false"); return 0; } } public boolean equal(Object o) { if (o instanceof Company) { Company c = (Company) o; if(this.cName.equals(c.cName)) { System.out.println(" from equal true"); return true; } } System.out.println(" from equal false"); return false; } public static void main(String[] args) {
The code is a bit confused, I apologize for just trying to use different approaches.
source share