Which code segment should you use?

You are creating a class named Age. You need to ensure that the Age class is written such that collections of Age objects can be sorted. Which code segment should you use?

You are creating a class named Age. You need to ensure that the Age class is written such that collections of Age objects can be sorted. Which code segment should you use?

A.
public class Age
{
public int Value;
public object CompareTo(object obj)
{
if (obj is Age)
{
Age_age = (Age) obj;
return Value.ComapreTo(obj);
}
throw new ArgumentException(“object not an Age”);
}
}

B.
public class Age
{
public int Value;
public object CompareTo(int iValue)
{
try { return Value.ComapreTo(iValue); }
catch { throw new ArgumentException(“object not an Age”); }
}
}

C.
public class Age : IComparable
{
  public int Value;
  public int CompareTo(object obj)
  {
    if (obj is Age)
    {
      Age_age = (Age) obj;
      return Value.ComapreTo(age.Value);
    }
    throw new ArgumentException(“object not an Age”);
  }
}

D.
public class Age : IComparable
{
public int Value;
public int CompareTo(object obj)
{
try { return Value.ComapreTo(((Age) obj).Value); }
catch { return -1; }
}
}



Leave a Reply 1

Your email address will not be published. Required fields are marked *