In: Computer Science
Comprehension and Coding 12. Nadha Skolar needs to write a method smallToBig() that will accept 3 integers as parameters and print out the sequence of numbers smallest to largest. For example, regardless of whether the sequence is 3 5 7, 5 7 3, 3 7 5, the method should print out 3 5 7. Nadha writes the following in IntelliJ and feels pretty confident until he submits the code to zyBooks… public static void smallToBig (int num1, int num2, int num3){ int min, middle, max; if (num1 <= num2){ min = num1; middle = num2; if (num1 <= num3) { middle = num1; max = num3; } else { middle = num3; max = num1; } } else { min = num2; middle = num1; if (num2 <= num3) { middle = num2; max = num3; } else { middle = num3; max = num2; } } System.out.println("Min = " + min + " Mid = " + middle + " Max = " + max); } Describe the error in Nadha’s program and how you could correct it. You do not have to code the fix
/*Fixed the code to sort and print the num1 and num2 and num3 in
ascending order*/
   public static void smallToBig (int num1, int num2, int
num3)
   {
       //Declare three variables to store
minimum, middle and maximum values
       int min, middle, max;
       /*case 1: Checking if num1 is less
than num2 and less than num3*/
       if (num1 < num2 &&
num1<num3)
       {
           //num1 is
minimum
           min =
num1;
           //check if num2
is less than num3
           if (num2 <
num3)
           {
          
    //set num3 is maximum
          
    max = num3;
          
    //set num2 is middle value
          
    middle =num2 ;   
           }
           else
           {
          
    //otherwise num2 is maximum
          
    max = num2;
          
    //num3 is middle value
          
    middle =num3 ;   
           }
       }
       /*case 2: Checking if num2 is less
than num1 and less than num3*/
       else if (num2 < num1 &&
num2<num3)
       {
           //num2 is
minimum
           min =
num2;
           //find the num3
is
           if (num3 <
num1)
           {
          
    max = num1;
          
    middle = num3;
           }
           else
           {
          
    max = num3;
          
    middle = num1;
           }
       }
       else
       {
           //If not case 1
and case 2, then num3 is minimum
           min=num3;
           //check if num1
is less than num2
           if (num1 <
num2)
           {
          
    //set num2 is max
          
    max = num2;
          
    //set num1 is middle
          
    middle = num1;
           }
           else
           {
          
    //Otherwise set num1 to maximum
          
    max = num1;
          
    //set num2 to middle
          
    middle = num2;
           }
       }
       //Print min middle and max
values
       System.out.println("Min = " + min +
" Mid = " + middle + " Max = " + max);
   }
}
------------------------------------*------------------------------------*------------------------------------*
Sample test program :
/*Test the java program for the method , smallToBig*/
//Ascending.java
public class Ascending
{
   public static void main(String[] args)
   {
       int num1=7;
       int num2=3;
       int num3=5;
       //calling method smallToBig
       smallToBig(num1,num2,num3);
   }
   /*Fixed the code to sort and print the num1 and num2
and num3 in ascending order*/
   public static void smallToBig (int num1, int num2, int
num3)
   {
       //Declare three variables to store
minimum, middle and maximum values
       int min, middle, max;
       /*case 1: Checking if num1 is less
than num2 and less than num3*/
       if (num1 < num2 &&
num1<num3)
       {
           //num1 is
minimum
           min =
num1;
           //check if num2
is less than num3
           if (num2 <
num3)
           {
          
    //set num3 is maximum
          
    max = num3;
          
    //set num2 is middle value
          
    middle =num2 ;   
           }
           else
           {
          
    //otherwise num2 is maximum
          
    max = num2;
          
    //num3 is middle value
          
    middle =num3 ;   
           }
       }
       /*case 2: Checking if num2 is less
than num1 and less than num3*/
       else if (num2 < num1 &&
num2<num3)
       {
           //num2 is
minimum
           min =
num2;
           //find the num3
is
           if (num3 <
num1)
           {
          
    max = num1;
          
    middle = num3;
           }
           else
           {
          
    max = num3;
          
    middle = num1;
           }
       }
       else
       {
           //If not case 1
and case 2, then num3 is minimum
           min=num3;
           //check if num1
is less than num2
           if (num1 <
num2)
           {
          
    //set num2 is max
          
    max = num2;
          
    //set num1 is middle
          
    middle = num1;
           }
           else
           {
          
    //Otherwise set num1 to maximum
          
    max = num1;
          
    //set num2 to middle
          
    middle = num2;
           }
       }
       //Print min middle and max
values
       System.out.println("Min = " + min +
" Mid = " + middle + " Max = " + max);
   }
}
Sample output:
Min = 3 Mid = 5 Max = 7