Suppose firstNum secondNum and thirdNum are three integer va
Solution
1) In three ways the exactly two numbers can be equal.
firstNum equals secondNum or
firstNum equals thirdNum or
secondNum equals thirdNum
2)
firstNum = 1
secondNum = 1
thirdNum = 3
here firstNum equals secondNum.
3)
firstNum != secondNum
firstNum != thirdNum
thirdNum != secondNum
4)
firstNum = 1
secondNum = 2
thirdNum = 3
firstNum != secondNum
6)
class Main {
public static void main(String[] args) {
int firstNum=1;
int secondNum=1;
int thirdNum=3;
int nComparisons = 0;
if(firstNum == secondNum){
nComparisons = 1;
System.out.println(\"Exactly two numbers are equal\ Comparisons:\"+nComparisons);
}
else if(firstNum == thirdNum){
nComparisons = 2;
System.out.println(\"Exactly two numbers are equal\ Comparisons:\"+nComparisons);
}
else if(secondNum == thirdNum){
nComparisons = 3;
System.out.println(\"Exactly two numbers are equal\ Comparisons:\"+nComparisons);
}
else{
nComparisons = 3;
System.out.println(\"Exactly two numbers are not equal\ Comparisons:\"+nComparisons);
}
}
}
/* sample output
*/
7) 1 comparison would be the least to be performed.
8) In three ways the least number of comparisons can be made by changing the first if statement.
if(firstNumber == secondNumber) in case if both numbers are equal( firstNumber = 1, secondNumber = 1)
if(firstNumber == thirdNumber) in case if both numbers are equal.(firstNumber = 2, secondNumber = 2)
if(thirdNumber == secondNumber) in case if both numbers are equal.(thirdNumber = 3, secondNumber = 3)
9)
firstNumber = 1, secondNumber =1.
1 comparison.
Now change the if statement in the code to secondNumber == thridNumber.
secondNumber = 2, thirdNumber =2
1 comparison.
Now change the if statement in the code to thirdNumber = firstNumber
firstNumber = 3 , thirdNumber = 3
1 comparions.
If code has to be static.
Then there is only one way we can get 1 comparison i.e. firstNumber = secondNumber.

