Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Description ASU Home My ASU College For this lab, you will create a basic Array of numbers, fill in the elements of that array
Problem Description ASU Home My ASU College For this lab, you will create a basic Array of numbers, fill in the elements of that array by prompting the user for input, sort the elements of array and display the sorted elements to the user. Step 1: Getting Started Create a new .java file named Lab10.java. You will need two import statements, one to import the Scanner class and one to import the Array class: import java.util.Scanner; import java.util.Arrays; Lastly, declare the Lab10 class and the main function: public class Lab10 { public static void main(String[] args) { Step 2: Declaring variables For this section of the lab, you will need to declare three local variables in main: one scanner to read input from the user, one integer for the length of the array, and one double to temporarily store a value so that we can sort the elements of the array. Declare these variable immediately inside your main function definition. // A scanner object for requesting input from System.in. // --> // An integer for the array size. // --> // A double for the exchange element. // --> Step 3: Request Array Size from the User ASU Home My ASU College: Next, use the Scanner object declared in the previous step to request from the user the number of elements the array should have. Remember that you should always print a message indicating what is to be input before requesting information from the user. // Print this message "How many elements in the array?" // --> // Request an integer from the user using the Scanner object // and store the inputted value in the integer declared above. // --> Step 4: Declare the Array Next, use the integer value previously requested from the user to declare and instantiate a new array of a length specified by the user. This is very similar to the way we have declared Scanner, Person, Student, and Employee objects in the past. Make sure to use a double array. // Declare a new array of size equal to the size requested in Step 3. // --> Step 5: Fill in the Array ASU Home My ASU Colleges Using a for loop, we will now fill in the array by storing a value in each element of the array. This loop will iterate as many times as we have elements in the array. for(int index = 0; index ; index ++) { Note that this loop starts at 0 and its final iteration occurs when index is equal to the number of elements minus 1. This is important because Arrays indexes in Java are zero-based, which means that the first element in the array is stored at index 0 in the array. Inside the for loop, display a message to the user, request the next element value, and store that value in the appropriate position of the array. // Display the message: "Please enter the next value." // --> // Request the next element (double) from the user using // the Scanner object declared in Step 2. // --> // Store this element at the correct index in the array created in Step 4. // --> Step 6: Sort the Array's Elements ASU Home My ASU Colleges Using two for loops to compare each element of array to each other element that comes after this element in the array. If one element after this element is less than it then swap their values. For example, if the element at index 4 is less than element at index 2, then swap these two elements so that the lesser value is stored at index 2 and the greater value is stored at index 4. // OUTER LOOP: loop through each element of the array, from index 0 through index (length - 2) // --> // INNER LOOP: loop through each element of the array that comes after // the index specified by the outer loop // Compare the value at the outer loop index with the value at the inner loop index // if the value at the outer loop index is greater than the value at the inner loop index if (array(outerIndex] > array[innerIndex]) { // then swap these two values temp = array[outerIndex]; array[outer Index] = array[inner Index]; array[innerIndex] = temp; } //end of if } // end of the second loop //we find the correct place of one element in the array } //end of the first loop //we find the correct place of all elements after finishing the first loop Step 7: Display the Array Contents ASU Home My ASU Colleges Lastly, print out the elements of the sorted array to the user. // Print the following message to the user, preceded by a newline character. // "The array's elements after SORT: // --> // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Step 8: Remove the minimum In this step shift all the elements to the next lower index to remove the minimum (first) element in the sorted array. For this part use a for loop and in each step, you need to copy the value of element at index + 1 and store that value at index. Put the zero in the last element of array/ for (index = 0; index // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Step 10: Insert an element In this step, Ask the user for a new value to insert into the sorted array. System.out.println("Enter a new value to insert in the Array: "); double newValue = scan.nextDouble(); Use a for loop to iterate backwards through the array indexes, and copy each element value that is greater than the newValue to the next larger index. This will make a spot in the array at the correct index for us to insert the newValue. for (index = array.length - 2; index >= 0 && array[index] > newValue; index--) { array[index + 1] = array[index]; // the newValue goes at index + 1 array[index + 1] = newValue; Step 11: Display the Final Array ASU Home My ASU Colleges Lastly, print out the elements of array // Print the following message to the user, preceded by a newline character. // "The array's elements after INSERT :" // --> // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Sample Output: with user input in red. How many elements is in the array? 5 Enter the next Value: 3 Enter the next Value: 6 Enter the next Value: 2 Enter the next Value: 8 Enter the next Value: 4 The array after SORT: 2.0,3.0 ,4.0 ,6.0 ,8.0, The array after LEFT SHIFT: 3.0 ,4.0 ,6.0 ,8.0 ,0.0, Enter a new value to insert in the Array: 7 The array's elements after INSERT : 3.0 ,4.0 ,6.0 ,7.0 ,8.0 , Problem Description ASU Home My ASU College For this lab, you will create a basic Array of numbers, fill in the elements of that array by prompting the user for input, sort the elements of array and display the sorted elements to the user. Step 1: Getting Started Create a new .java file named Lab10.java. You will need two import statements, one to import the Scanner class and one to import the Array class: import java.util.Scanner; import java.util.Arrays; Lastly, declare the Lab10 class and the main function: public class Lab10 { public static void main(String[] args) { Step 2: Declaring variables For this section of the lab, you will need to declare three local variables in main: one scanner to read input from the user, one integer for the length of the array, and one double to temporarily store a value so that we can sort the elements of the array. Declare these variable immediately inside your main function definition. // A scanner object for requesting input from System.in. // --> // An integer for the array size. // --> // A double for the exchange element. // --> Step 3: Request Array Size from the User ASU Home My ASU College: Next, use the Scanner object declared in the previous step to request from the user the number of elements the array should have. Remember that you should always print a message indicating what is to be input before requesting information from the user. // Print this message "How many elements in the array?" // --> // Request an integer from the user using the Scanner object // and store the inputted value in the integer declared above. // --> Step 4: Declare the Array Next, use the integer value previously requested from the user to declare and instantiate a new array of a length specified by the user. This is very similar to the way we have declared Scanner, Person, Student, and Employee objects in the past. Make sure to use a double array. // Declare a new array of size equal to the size requested in Step 3. // --> Step 5: Fill in the Array ASU Home My ASU Colleges Using a for loop, we will now fill in the array by storing a value in each element of the array. This loop will iterate as many times as we have elements in the array. for(int index = 0; index ; index ++) { Note that this loop starts at 0 and its final iteration occurs when index is equal to the number of elements minus 1. This is important because Arrays indexes in Java are zero-based, which means that the first element in the array is stored at index 0 in the array. Inside the for loop, display a message to the user, request the next element value, and store that value in the appropriate position of the array. // Display the message: "Please enter the next value." // --> // Request the next element (double) from the user using // the Scanner object declared in Step 2. // --> // Store this element at the correct index in the array created in Step 4. // --> Step 6: Sort the Array's Elements ASU Home My ASU Colleges Using two for loops to compare each element of array to each other element that comes after this element in the array. If one element after this element is less than it then swap their values. For example, if the element at index 4 is less than element at index 2, then swap these two elements so that the lesser value is stored at index 2 and the greater value is stored at index 4. // OUTER LOOP: loop through each element of the array, from index 0 through index (length - 2) // --> // INNER LOOP: loop through each element of the array that comes after // the index specified by the outer loop // Compare the value at the outer loop index with the value at the inner loop index // if the value at the outer loop index is greater than the value at the inner loop index if (array(outerIndex] > array[innerIndex]) { // then swap these two values temp = array[outerIndex]; array[outer Index] = array[inner Index]; array[innerIndex] = temp; } //end of if } // end of the second loop //we find the correct place of one element in the array } //end of the first loop //we find the correct place of all elements after finishing the first loop Step 7: Display the Array Contents ASU Home My ASU Colleges Lastly, print out the elements of the sorted array to the user. // Print the following message to the user, preceded by a newline character. // "The array's elements after SORT: // --> // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Step 8: Remove the minimum In this step shift all the elements to the next lower index to remove the minimum (first) element in the sorted array. For this part use a for loop and in each step, you need to copy the value of element at index + 1 and store that value at index. Put the zero in the last element of array/ for (index = 0; index // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Step 10: Insert an element In this step, Ask the user for a new value to insert into the sorted array. System.out.println("Enter a new value to insert in the Array: "); double newValue = scan.nextDouble(); Use a for loop to iterate backwards through the array indexes, and copy each element value that is greater than the newValue to the next larger index. This will make a spot in the array at the correct index for us to insert the newValue. for (index = array.length - 2; index >= 0 && array[index] > newValue; index--) { array[index + 1] = array[index]; // the newValue goes at index + 1 array[index + 1] = newValue; Step 11: Display the Final Array ASU Home My ASU Colleges Lastly, print out the elements of array // Print the following message to the user, preceded by a newline character. // "The array's elements after INSERT :" // --> // use a for loop to loop through all array indexes // --> // print the value of the element at this index, followed by a comma and a space Sample Output: with user input in red. How many elements is in the array? 5 Enter the next Value: 3 Enter the next Value: 6 Enter the next Value: 2 Enter the next Value: 8 Enter the next Value: 4 The array after SORT: 2.0,3.0 ,4.0 ,6.0 ,8.0, The array after LEFT SHIFT: 3.0 ,4.0 ,6.0 ,8.0 ,0.0, Enter a new value to insert in the Array: 7 The array's elements after INSERT : 3.0 ,4.0 ,6.0 ,7.0 ,8.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started