Suppose firstNum, secondNum and thirdNum are three integer variables. In how man
ID: 3926167 • Letter: S
Question
Suppose firstNum, secondNum and thirdNum are three integer variables. In how many ways can exactly two of these numbers be equal? Be very precise about all the ways in which this can happen. Give specific examples of numeric values for the variables for each of the ways in which exactly two of the numbers are equal. Relate the numeric examples to the ways given in 1. In how many ways can exactly two of these numbers not be equal? Be very precise about the ways in which this can happen. Give specific examples of numeric values for the variables for each of the ways in which exactly two of the numbers are not equal. Relate the numeric examples to the ways given in 3. Write a code segment that prints "Exactly two number are equals." when exactly two of the numbers are equal and "Exactly two numbers are not equal." when exactly two of the numbers are not equal. The code segment should use no more than 3 comparisons and should perform no unnecessary comparison in giving the correct output. Write a modified version of the code segment by adding an integer variable numComparisons that always has the correct tally of the number of comparisons performed during the execution of the code segment for any three integers. What is the least number of comparisons that the code segment will perform? In how many ways will the code segment perform the least number of comparisons? Give specific examples of numeric values for the variables for each way in which the code segment will perform the least number of comparisons. Relate the numeric examples to the ways given in 8.Explanation / Answer
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.