Question: C# Coding: The following IsEqual method, for the Name class of Example 6.3, attempts to check two Name objects for equality. They should be equal

C# Coding:

The following IsEqual method, for the Name class of Example 6.3, attempts to check two Name objects for equality. They should be equal if they have the same first name, middle initial, and last name. Find and correct any errors in IsEqual.

public bool IsEqual (Name name)

{

if (first != name.first)

return false;

else if(initial != name.initial)

return false;

else return last == name.last;

result = true;

}

public class Name

{

private String first;

private char initial;

private String last;

public Name(String f, String l)

{

first = f;

last = l;

}

public Name(String f, char i, String l) : this(f,l)

{

initial = i;

}

public override String ToString()

{

if (initial == '\u0000')

return first + " " + last;

else

return first + " " + initial + " " + last;

}

}

Use this method to test your fix.

public static void test()

{

String s1 = "Wolfgang";

String s2 = "Wolfgang";

Name n1 = new Name(s1, 'A', "Mozart");

Name n2 = n1;

Name n3 = new Name(s2, 'A', "Mozart");

Name n4 = new Name("Abraham", "Lincoln");

Name n5 = new Name("Abraham", "Lincoln");

Console.WriteLine("N1 = {0}", n1.ToString());

Console.WriteLine("N2 = {0}", n2.ToString());

Console.WriteLine("N3 = {0}", n3.ToString());

Console.WriteLine("N4 = {0}", n4.ToString());

Console.WriteLine("N5 = {0}", n5.ToString());

Console.WriteLine();

Console.WriteLine(n1.IsEqual(n2));

Console.WriteLine(n1.IsEqual(n3));

Console.WriteLine(n1.IsEqual(n4));

Console.WriteLine(n1.IsEqual(n1));

Console.WriteLine(n4.IsEqual(n5));

Console.ReadLine();

Console.Clear();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!