Question
What this Lab Is About: Use of the Array and classes Sorting a group of numbers and shifting the array Use the following Coding Guidelines:
What this Lab Is About: Use of the Array and classes Sorting a group of numbers and shifting the array Use the following Coding Guidelines: When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*------------------------------------------------------------------------- // AUTHOR: your name // FILENAME: Lab10.java // SPECIFICATION: // FOR: CSE 110- Lab #10 // TIME SPENT: how long it took you to complete the assignment //----------------------------------------------------------------------*/ Problem Description 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. This exact name is important, as the grading system requires your file to be named exactly as such in order to test your code. At the beginning of this file, copy the assignment documentation code block. After the documentation block, 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) { For this section of the lab, you will need to declare four (3) local variables: one integer for the length of the array, one doubles for the exchange elements, and one scanner to read input from the user. Declare these variable immediately after the function definition: // An integer for the array size. // --> // A double for the exchange element. // --> // A scanner object for requesting input from System.in. // --> Step 3: Request Array Size from the User: 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 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. // --> // integer array of a fixed size. // int [] integerArray = new int [array size]; Step 5: Fill in the Array: Using a for loop, we will now fill in the elements of the array. This loop will iterate as many times as we have elements in the array. for(int i = 0; i < ; i++) { Note that this loop starts at 0 and its final iteration occur when i is equal to the number of elements minus 1. This is important because Arrays in Java are zero-indexed, which means that the first element in the array is stored at the 0th position in the array. Inside the for loop, display a message to the user, request the next element, 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 ith position of the array // created in Step 4. // --> } Step 6: Sort the Arrays Elements: Using two for loops to compare each element of array to all elements that are after this element in the array. If one element after this element is less than it then exchanges them. For example, if the element 4 is less than element 2 exchange these two elements. //The first loop for choosing each element of the array For (int i2=0; i2< number of elements; i2++) // The second loop to compare elements // this loop start from i+1 For (int j2=i2+1; J2 array[j2]){ //exchange Exchange= array[i2] array[i2]=array[j2] array[j2]=exchange }//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 Lastly, print out the elements of the sorted array to the user and close the main function and Lab10 class // Print the following message to the user, preceded by a // newline character. // "The array's elements after sorting : // --> // use a for loop For (int k=0; K
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