Question

In: Computer Science

Comprehension and Coding 12. Nadha Skolar needs to write a method smallToBig() that will accept 3...

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

Solutions

Expert Solution

/*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


Related Solutions

IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
5. Write a method to accept five int parameters and return the largest of the five.  ...
5. Write a method to accept five int parameters and return the largest of the five.         Example: largest(45, 13, 65, 19, 23) returns 65. Java Program (METHOD ONLY!!!)
how to write coding in matlab using gauss elimination method for equation 10a + 50b +...
how to write coding in matlab using gauss elimination method for equation 10a + 50b + 20c + 10d = 100 5a + 15b + 75c - 25d=200 25a -15c - 5d = 300 10a + 20b - 30c + 100d = 400
Assume that an English comprehension test is given to a random sample of 12 students before...
Assume that an English comprehension test is given to a random sample of 12 students before and after they complete an English course. The tests taken before the course had   x? =105.75 points and s = 4.07 points.  The tests were taken after the course had x? = 113.08 points and s = 3.63 points. Complete a hypothesis test to determine if the course improved the test score using a significance level ? = 0.05.
Write a method named area with one double parameter name radius. The method needs to return...
Write a method named area with one double parameter name radius. The method needs to return a double value that represents the area of a circle. If the parameter radius is negative , then return -1.0 to present an invalid value Write another overload method with 2 parameters side1 and side2 (both double) where side1 and side2 represent the sides of a rectangle. The method needs to return an area of a rectangle . If either or both parameters is/...
Write a method named area with one double parameter name radius, the method needs to return...
Write a method named area with one double parameter name radius, the method needs to return a double value that represents the area of a circle. If the parameter radius is negative, then return -1.0 to represents an invalid value write another overload method with 2 parameter side 1 and side 2 (double) where side 1 and side 2 represents the sides of a rectangle. The method needs to return an area of a rectangle. If either or both parameters...
3. Write a program that will accept only 5 numbers from 50 to 100. The program...
3. Write a program that will accept only 5 numbers from 50 to 100. The program should remind the user if an inputted number is not on the range. Compute the sum and average of the 1st and the 5th inputted numbers.
Write a comprehension on the diversity of polymers used in the medical field and explain how...
Write a comprehension on the diversity of polymers used in the medical field and explain how and why they are suited to the specific applications you are discussing.
Write a Python program to implement one studied entropy coding method, including two separate functions for...
Write a Python program to implement one studied entropy coding method, including two separate functions for encoding and decoding. Use the lyrics of your favorite song as the message to be processed, and test the program on the message. Include the source code, and detail the program design and execution procedure in this section.
PA12-3 Preparing a Statement of Cash Flows (Indirect Method) [LO 12-2, LO 12-3, LO 12-4, LO...
PA12-3 Preparing a Statement of Cash Flows (Indirect Method) [LO 12-2, LO 12-3, LO 12-4, LO 12-5] XS Supply Company is developing its annual financial statements at December 31. The statements are complete except for the statement of cash flows. The completed comparative balance sheets and income statement are summarized:     Current Year Previous Year Balance Sheet at December 31             Cash $ 35,370   $ 30,450   Accounts Receivable   36,600     28,800   Inventory   42,600     38,800   Equipment   133,000     108,000   Accumulated Depreciation—Equipment   (31,600 )   (25,800 )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT