Writing your own class for names with a boolean method

I'm writing a college class and all it has to do is keep the name in the first and last middle. It must also have multiple methods, one of which is "public boolean equal to (Name otherName)"

That's 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 a problem with mapping one Name object to another Name object. the question assumes I'm using 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 the name of a class that stores people's first names, last names and last names, and provides the following methods:

Public Name (String first, String middle, String last) -constructor. The name must be retained in the specified case; dont convert to all upper or lower case.

Public String getFirst () - returns the 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, for example, "Mary Jane Smith".

Public String lastFirstMiddle () - Returns a string containing the person's full first name, followed by a comma, for example "Smith, Mary Jane".

Public boolean equals (Name otherName) - returns true if this name is the same as otherName. The comparison should not be case sensitive. (Hint: There is a String equalsIgnoreCase method, which is similar to String, except that it ignores case when comparing).

+3


source to share


4 answers


equalsIgnoreCase

is a Java String method that should be called on a String object. You are trying to call this method on a Name object that will not work. You must call the helper methods on the Name class to get the correct string and then call the method equalsIgnoreCase

.

Here are some methods for comparing the entire Name string and the three parts of the name separately.

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

      



Alternatively, you can get cleaner code using the string methods you provided.

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

      

0


source


What you want to do in a method equals

is compare all the important variables in the class. To get started:

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

}

      

Technically speaking, it should be taken Object

and discarded, but if your teacher said to take Name

, then do it I guess ..



The peer priority should actually look something 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 */)
}

      

+2


source


equalsIgnoreCase()

intended for string comparison not for object comparison. To compare objects, you need to override the equals method correctly. equals()

will compare objects for equality according to your properties and hashCode()

is a must for proper use of your objects in Collections

.

This is the standard equals implementation in the java.lang.Object class

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

      

+1


source


Just use a fully qualified String instead of a name when comparing or creating 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;
}

      

+1


source







All Articles