简体   繁体   中英

Writing My own class for names issue with boolean method

I am writing a class for College and all it has to do is store a name in first last and middle. It also has to have a few methods one of which being "public boolean equals(Name otherName)"

Here is what I have so far

public class Name 
{
 private String FirstNM, MiddleNM, LastNM,Name, otherName;
 public Name(String first, String middle, String last)
{
    FirstNM = first;
    MiddleNM = middle;
    LastNM = last;
    Name = first+middle+last;
}

public String toString()
{
    return (FirstNM+", "+MiddleNM+", "+LastNM);
}

public String getFirst()
{
    return FirstNM;
}

public String getMiddle()
{
    return MiddleNM;
}

public String getLast()
{
    return LastNM;
}

public String firstMiddleLast()
{
    return (FirstNM+", "+MiddleNM+", "+LastNM);
}

public String lastFirstMiddle()
{
    return (LastNM+", "+FirstNM+", "+MiddleNM);
}

public boolean equals(Name otherName)
{
    if (otherName.equalsIgnoreCase(Name))
    {
        return true;
    }
}

I am having an issue with the comparing of one Name object to another Name object. the question expects me to use the equalsIgnoreCase method for this. I can't seem to get this to work. What am i doing wrong\\ what can i do differently

edit: Let me clarify with the exact question from the book

  1. Write a class Name that stores a person's first, middle, and last names and provides the following methods:

· public Name(String first, String middle, String last)—constructor. The name should be stored in the case given; don't convert to all upper or lower case.

· public String getFirst()—returns the first name

· public String getMiddle()—returns the middle name

· public String getLast()—returns the last name

· public String firstMiddleLast()—returns a string containing the person's full name in order, eg, “Mary Jane Smith”.

· public String lastFirstMiddle()—returns a string containing the person's full name with the last name first followed by a comma, eg, “Smith, Mary Jane”.

· public boolean equals(Name otherName)—returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)

What you want to do in the equals method is compare all important variables in the class. To get you started:

public boolean equals(Name otherName) {
    return (this.firstNm.equalsIgnoreCase(otherName.firstNm) && /* rest of variables to compare */)

}

Technically speaking, this should be taking in an Object and casting it, but if your teacher said to take in Name then do that I guess..

Overriding equals should really look something more like this:

public boolean equals(Object other) {
    if (other == null || ! other instanceof Name) return false;
    Name otherName = (Name) other;
    return (this.firstNm.equalsIgnoreCase(otherName.firstNm) && /* rest of variables to compare */)
}

equalsIgnoreCase() is for comparing strings not for comparing objects. In order to compare objects you need to properly override equals method. equals() will compare the objects for equality according to the properties you need and hashCode() is mandatory in order for your objects to be used correctly in Collections .

This is the default implementation of equals in java.lang.Object class

 public boolean equals(Object obj) {
        return (this == obj);
    }

Just use a String with the full name, instead of Name when comparing or create a getName() method. Do this at least:

public boolean equals(Name otherName)
{
    String fullName = otherName.getFirst() + otherName.getMiddle() + otherName.getLast();
    if (fullName.get.equalsIgnoreCase(this.Name))
    {
        return true;
    }
    return false;
}

equalsIgnoreCase is a Java String method that must be invoked on a String object. You are trying to invoke this method on a Name object, which won't work. You must invoke the helper methods within the Name class to retrieve the correct String, then invoke the equalsIgnoreCase method.

Here are a couple methods for comparing the entire Name string, and the 3 parts of a Name individually.

public boolean equals(Name otherName)
{
    return (otherName.getFirst().equalsIgnoreCase(Name.getFirst()) 
            && otherName.getMiddle().equalsIgnoreCase(Name.getMiddle())
            && otherName.getLast().equalsIgnoreCase(Name.getLast()));
}

Furthermore, you could have cleaner code by using the string methods you have provided.

public boolean equals(Name otherName)
{
    return (otherName.toString().equalsIgnoreCase(Name.toString()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM