bool isEqualTo(const OrderedPair & other) ; bool isLessThan(const OrderedPair &
ID: 3569636 • Letter: B
Question
bool isEqualTo(const OrderedPair & other) ;
bool isLessThan(const OrderedPair & other) ;
Notice those functions have return type bool. So for the first function you should return true if the current object ( *this ) is equal to other and false otherwise. For the second function you should return true if the current object is less than other. To determine whether one OrderedPair is less than another, use lexicographic ordering e.g. (1,3) is less than (1,5) and (2,0) but not less than (1,2) or (0,2).
Also include a void swap() member function that switches the order of the two values.
Explanation / Answer
class OrderedPair
{
int x,y;
OrderedPair()
{
x = 0 ; y= 0;
}
OrderedPair(int a,int b)
{
x = a; y = b;
}
bool isEqualTo(OrderedPair & other)
{
if(this.x == other.x && this.y == other.y)
return true;
else
return false;
}
bool isLessThan(OrderedPair & other)
{
if(this.x < other.x)
return true;
else if(this.x > other.x)
return false;
else
{
if(this.y < other.y)
return true;
else
return false;
}
}
}
public class Driver(String args[])
{
OrderedPair a = new OrderedPair(1,3);
OrderedPair a = new OrderedPair(1,5);
OrderedPair a = new OrderedPair(2,0);
OrderedPair a = new OrderedPair(1,2);
OrderedPair a = new OrderedPair(0,2);
System.out.println("1,3 < 1,5" + a.isLessThan(b) );
System.out.println("1,3 = 1,5" + a.isEqualTo(b));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.