Write a method ascend3() with an array of integers of size three as the parameter, that sorts the values of the array in
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Write a method ascend3() with an array of integers of size three as the parameter, that sorts the values of the array in
*JAVA*
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. 405078.1478210.qx3zqy7 . LAB ACTIVITY 1 import java.util.Scanner; 2 3 public class LabProgram { 4 // Define Ascend3 () here 5 6 700 8 9 972222222 10 11 12 13 14 15 16 17 18 19} 20 N 6.12.1: PRACTICE: Methods**: Sort three values 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] 11 I LabProgram.java + arrvals [1] 11 arrvals[2]); 0/6 Load default template...