PrepAway - Latest Free Exam Questions & Answers

What should you do?

Domain.com has instructed you to create a class named MetricFormula.
This class will be used to compare MetricUnit and EnglishUnit objects. The MetricFormula is currently defined as follows (Line numbers are used for reference purposes only):
1. public class MetricFormula
2. {
3.
4. }
You need to ensure that the MetricFormula class can be used to compare the required objects.
What should you do? (Choose two)

PrepAway - Latest Free Exam Questions & Answers

A.
Add the following code on line 1:
: IComparable
{

B.
Add the following code on line 1:
: IComparer
{

C.
Add the following code on line 3:
public int Compare (object x, object y){
// implementation code
}

D.
Add the following code on line 3:
public int CompareTo (object obj){
// implementation code
}

Explanation:
You should add the code so that it reads as follows:
1. public class MetricFormula : IComparer
2. {
3. public int Compare (object x, object y)
4. {
5. // implementation code
5. }
6. }
You have to implement the IComparer interface to create a comparer class for MetricUnit and EnglishUnit objects.
The IComparer interface provides only one method named Compare. The Compare method takes two objects and returns an integer value representing whether those objects are equal, greater than, or less than the other.
If the return value is negative, then the first object is less than the second.
The objects are equal if the return value is zero.
The first object is greater than the first if the return value is positive. The IComparer interface is typically used if you want to implement comparison across objects of different classes without having to provide implementation in each comparable class.
Incorrect Answers:
A, D: You should use these two options because this should be implemented by the MetricUnit and EnglishUnit classes.

One Comment on “What should you do?


Leave a Reply