(a). Write a code for the booleaan method isOrderedUp. Method receives a referen
ID: 3773387 • Letter: #
Question
(a). Write a code for the booleaan method isOrderedUp. Method receives a reference to a PairOfInts object and returns true if the value of the instance int1 is less than the value of the instance variable int2. If this condition is not met, the method should return false.
(b). Write a boolean method isOrderedDown. The method also receives a reference to a PairOfInts object. It returns true if the value of the instance variable int1 is greater than the value of the instance variable int2. If this condition is not met, the method should return false.
(c). Write a boolean method isOrderedEqual. The method also receives a reference to a PairOfInts object (pair). It returns true if the value of the instance variable int1 is equal to the value of the instance variable int2 provided that pair is not a null pair. If the values of the instance variables are not equal or if the pair is a null pair, the method should return false.
Explanation / Answer
(a)
Method isOrderedUp
/*The method isOrderedUp that takes an reference
* of PairOfInts class and checks if the int1 is
* less than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedUp(PairOfInts otherPair)
{
//The less than condition on int1 instance is less
//than the int2 returns true otherwise return false
return otherPair.getX()<otherPair.getY();
}
-----------------------------------------------------------------------------------
(b)
Method isOrderedDown
/*The method isOrderedDown that takes an reference
* of PairOfInts class and checks if the int1 is
* greater than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedDown(PairOfInts otherPair)
{
//The less than condition on int1 instance is greater
//than the int2 returns true otherwise return false
return otherPair.getX()>otherPair.getY();
}
-----------------------------------------------------------------------------------
(c)
/*The method isOrderedEqual that takes an reference
* of PairOfInts class and checks if the int1 is
* greater than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedEqual(PairOfInts otherPair)
{
//Check if otherPair is null
if(otherPair==null)
//return false
return false;
/*Return true if the int1 instance variable is equal to
int2
* instance variable otherwise return false */
else if(otherPair.getX() == otherPair.getY())
return true;
else
//otherwise returns false
return false;
}
--------------------------------------------------------------------------------------
Test program to test the methods isOrderedUp, isOrderedDown
and isOrderedEqual
/**
* The java class PairOfInts that has instance
* variables int1 and int2.
* The class has setter and getter methods for instance
* variables. The class contains methods isOrderUp ,isOrderDown
* and isOrderEqual
* */
//PairOfInts.java
public class PairOfInts
{
//instance variables
public int int1;
public int int2;
//Constructor
public PairOfInts()
{
int1 = 0;
int2 = 0;
}
//Parameter constructor
public PairOfInts(int int1, int int2)
{
this.int1 = int1;
this.int2 = int2;
}
//Set int1
public void setX(int int1)
{
this.int1=int1;
}
//Set int2
public void setY(int int2)
{
this.int2=int2;
}
//Returns int1
public int getX()
{
return int1;
}
//Returns int2
public int getY()
{
return int2;
}
/*The method isOrderedUp that takes an reference
* of PairOfInts class and checks if the int1 is
* less than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedUp(PairOfInts otherPair)
{
//The less than condition on int1 instance is less
//than the int2 returns true otherwise return false
return otherPair.getX()<otherPair.getY();
}
/*The method isOrderedDown that takes an reference
* of PairOfInts class and checks if the int1 is
* greater than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedDown(PairOfInts otherPair)
{
//The less than condition on int1 instance is greater
//than the int2 returns true otherwise return false
return otherPair.getX()>otherPair.getY();
}
/*The method isOrderedEqual that takes an reference
* of PairOfInts class and checks if the int1 is
* greater than the int2 then returns true
* otherwise returns false*/
public boolean isOrderedEqual(PairOfInts otherPair)
{
//Check if otherPair is null
if(otherPair==null)
//return false
return false;
/*Return true if the int1 instance variable is equal to int2
* instance variable otherwise return false */
else if(otherPair.getX() == otherPair.getY())
return true;
else
//otherwise returns false
return false;
}
@Override
public String toString()
{
return "int1 : "+int1+" int2 : "+int2;
}
}//end of PairOfInts class
------------------------------------------------------------------------------------------------------------------------------------------------------
/**The java program that demonstrates methods isOrderUp , isOrderDown
* ,isOrderEqual and test the metods with reference objects
* and print results*/
//DriverClass.java
public class DriverClass
{
public static void main(String[] args)
{
//Create object of PairOfInts
PairOfInts intpair=new PairOfInts();
//Create reference object of PairOfInts with intx and inty
PairOfInts reference1=new PairOfInts(5,10);
//print objects
System.out.println("pair");
if(intpair.isOrderedUp(reference1))
System.out.println(reference1.getX()+"<"+reference1.getY());
else
System.out.println(reference1.getX()+">"+reference1.getY());
//Create reference object of PairOfInts with intx and inty
PairOfInts reference2=new PairOfInts(10,5);
if(intpair.isOrderedDown(reference2))
System.out.println(reference2.getX()+">"+reference2.getY());
else
System.out.println(reference2.getX()+"<"+reference2.getY());
//Create reference object of PairOfInts with intx and inty
PairOfInts reference3=new PairOfInts(10,10);
if(intpair.isOrderedEqual(reference3))
System.out.println(reference2.getX()+"=="+reference2.getY());
else
System.out.println(reference2.getX()+"!="+reference2.getY());
//Create reference object of PairOfInts with intx and inty
PairOfInts reference4=new PairOfInts(10,5);
if(intpair.isOrderedEqual(reference4))
System.out.println(reference2.getX()+"=="+reference2.getY());
else
System.out.println(reference2.getX()+"!="+reference2.getY());
}
}
-----------------------------------------------------------------------------------------------------------------------
Sample Output:
pair
5<10
10>5
10==5
10!=5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.