Question

In: Computer Science

IN JAVA Methods**: Sort three values Write a method Ascend3 with an array of integers of...

IN JAVA

Methods**: Sort three values

Write a method Ascend3 with an array of integers of size three as the parameter, that sorts the values of the array into ascending order. Ex: If the array contains [5, 2, 7], after the call Ascend3(int[] vals), the array will now hold [2, 5, 7].

Hints:

  • Return type should be void.

  • One approach puts the three values into an array, then sorts the array. We won't be describing that approach here. Instead, we'll use branches.

  • One solution approach realizes that only 6 possible orderings of xyz exist: xyz, xzy, yxz, yzx, zxy, zyx. An if-else can be used to detect which order x, y, z are initially in.

  • Once detected, three variables lowVal, midVal, and highVal can be assigned. Note: Don't assign the parameter right away, because you'll overwrite a value that is still needed.

  • After the if-else, those lowVal, midVal, and highVal variables are ready. So just set the vals[0] with lowVal, vals[1] with midVal, and vals[2] with highVal.

  • Be aware that two values could be equal. So use <= rather than < in your comparisons.

GIVEN CODE:

import java.util.Scanner;

public class Main {

// Define Ascend3() here

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] arrVals = new int[3];
  
arrVals[0] = scnr.nextInt(); // x
arrVals[1] = scnr.nextInt(); // y
arrVals[2] = scnr.nextInt(); // z
  
Ascend3(arrVals);
  
System.out.println(arrVals[0] + " " + arrVals[1] + " " + arrVals[2]);
}
}

Solutions

Expert Solution

import java.util.Scanner;

public class Main {

/**
   * method that receives an array of integers of size 3 and sorts the values into ascending order
   */
   public static void Ascend3(int vals[])
   {
       int lowVal, midVal, highVal;
      
       // if-else statements to get the low,mid and high values
      
       if(vals[0] <= vals[1]) // vals[0] <= vals[1]
       {
           if(vals[0] <= vals[2]) // if vals[0] <= vals[2]
           {
               lowVal = vals[0];
              
               // vals[0] <= vals[1] <= vals[2]
               if(vals[1] <= vals[2]) // 0,1,2
               {
                   midVal = vals[1];
                   highVal = vals[2];
               }
               // vals[0] <= vals[2] <= vals[1]
               else               // 0,2,1
               {
                   midVal = vals[2];
                   highVal = vals[1];
               }
           }
           // vals[2] <= vals[0] <= vals[1]
           else                   // 2,0,1
           {
               lowVal = vals[2];
               midVal = vals[0];
               highVal = vals[1];
           }
       }else // vals[1] <= vals[0]
       {
           // vals[1] <= vals[2]
           if(vals[1] <= vals[2])  
           {
               lowVal = vals[1];
               // vals[1] <= vals[0] <= vals[2]
               if(vals[0] <= vals[2])   // 1,0,2
               {
                   midVal = vals[0];
                   highVal = vals[2];
               }
               // vals[1] <= vals[2] <= vals[0]
               else                   // 1,2,0
               {
                   midVal = vals[2];
                   highVal = vals[0];
               }
           }
           // vals[2] <= vals[1] <= vals[0]
           else                       // 2,1,0
           {
               lowVal = vals[2];
               midVal = vals[1];
               highVal = vals[0];
           }
       }
      
       // set the values in the array in sorted order
       vals[0] =lowVal;
       vals[1] = midVal;
       vals[2] = highVal;
   }

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] arrVals = new int[3];
  
arrVals[0] = scnr.nextInt(); // x
arrVals[1] = scnr.nextInt(); // y
arrVals[2] = scnr.nextInt(); // z
  
Ascend3(arrVals);
  
System.out.println(arrVals[0] + " " + arrVals[1] + " " + arrVals[2]);
}
}

//end of program

Output:


Related Solutions

IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Write a recursive method to sum the values in an array of integers. Create a file...
Write a recursive method to sum the values in an array of integers. Create a file ArraySum.java and add the recursive method public int sumOfArray (). Use the driver class ArraySumDriver.java to populate your array and demonstrate that your method works. ////ArraySumDriver.java/////// public class ArraySumDriver { private final static int ARRAY_SIZE = 6; /** * @param args */ public static void main(String[] args) { int index = 0; Integer[] myArray = new Integer[ARRAY_SIZE]; ArraySum arraySum = new ArraySum(); myArray[index++] =...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user...
***JAVA PROGRAM Write a method called shrink that accepts an array of integers (that the user inputs) as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 15, 7, 1, 9, 10}, then the call of shrink(list) should return a new array containing {9, 17, 19, 8, 19}. The first pair from...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT