Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Description: ASU Home My ASU Colleges For this lab, you will create a basic Array of numbers, fill in the elements of that array
Problem Description: ASU Home My ASU Colleges For this lab, you will create a basic Array of numbers, fill in the elements of that array by prompting the user for input, display the elements of the array back to the user, and then calculate and display the sum of those array elements. Step 1: Getting Started Create a new .java file named Lab9.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 Lab9 class and the main function. public class Lab9 { public static void main(String[] args) { Step 2: Declaring variables For this section of the lab, you will need to declare four (4) local variables: one integer for the length of the array, two doubles (one for the current array element value and one for the sum of the array elements), and one scanner to read input from the user. Declare these variable immediately inside your main method. // An integer for the array size. // --> // A double for the current element value Step 2: Declaring variables ASU Home My ASU Colleges For this section of the lab, you will need to declare four (4) local variables: one integer for the length of the array, two doubles (one for the current array element value and one for the sum of the array elements, and one scanner to read input from the user. Declare these variable immediately inside your main method. // An integer for the array size. // --> // A double for the current element value. // --> // A double for the sum of elements. // --> // A scanner object for requesting input from System.in. // --> Step 3: Request Array size from 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 and collect an integer from the user using the Scanner object // and store the inputted value in the integer variable declared above. // --> Step 3: Request Array size from user ASU Home My ASU Colleges & 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 and collect an integer from the user using the Scanner object // and store the inputted value in the integer variable declared above. // --> Step 4: Declare and Instantiate 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 and instantiate a new array of size equal to the size requested in Step 3. // --> // For reference, the following is an EXAMPLE declaration of an // integer array of a fixed size. DO NOT USE THIS ARRAY. // int[] integerArray = new int[25]; Step 5: Fill in the array ASU Home My ASU Colleges 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 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 ith position of the array // created in Step 4. // --> ASU Home My ASU Colleges & S Step 6: Display the array's element values Again using a for loop, we will now display back to the user the elements of the array in reverse order of their input and sum up the array's values. The array's elements should be printed with 8 values on each line with a tab between each element, and a single newline separating each line. Again, the construction of the for loop must respect that array indexes in Java are zero-based, so the last element of the array is stored at the n - 1 index position, where n is the length of the array. // Construct a for loop that runs backwards through the array, // starting at the last element and ending at the first. for (/* Loop logic */) { // Inside this for loop, print the ith element of the array // and a tab, with NO newline characters. // --> // If this element is the 8th one on its line, print a // newline character to advance to the next line. // Also inside this for loop, add the value of the ith // element to the current value of the double for the sum // of elements declared in Step 2. // --> Step 7: Display the sum ASU Home My ASU Colleges & Lastly, print out the sum of the array's elements to the user and close the main function and Lab9 class. Even though the sum is a double, you do not need to worry about formatting the output of the double. Note that your print statement should including a preceding newline character to ensure that this message appears on a different line than the list of elements. Also note that as usual, indicates that the message should show the value of the variable used to store the sum of elements, not the word sum". // Print the following message to the user, preceded by a // newline character. // "The sum of the array's elements is: " // --> } // Close the main function. } // Close the Lab9 class. Below is an example of what your output should roughly look like when this lab is completed. All text in bold red represents user input. Sample Output 1: How many elements in the array? 10 Please enter the next value. 0.11 Please enter the next value. 0.10 Please enter the next value. 0.9 Please enter the next value. 0.8 Please enter the next value. 0.7 Please enter the next value. Please enter the next value. 0.5 Please enter the next value. 0.4 Please enter the next value. 0.3 Please enter the next value. 0.2 0.6 Sample Output 1: ASU Home My ASU College: How many elements in the array? 10 Please enter the next value. 0.11 Please enter the next value. 0.10 Please enter the next value. 0.9 Please enter the next value. 0.8 Please enter the next value. 0.7 Please enter the next value. 0.6 Please enter the next value. 0.5 Please enter the next value. 0.4 Please enter the next value. 0.3 Please enter the next value. 0.2 0.5 0.6 0.7 0.8 0.9 0.2 0.3 0.4 0.1 0.11 The sum of the array's elements is: 4.61 Sample Output 2: How many elements in the array? 3 Please enter the next value. 3.14 Please enter the next value. 1.414 Please enter the next value. 2.718 2.718 1.414 3.14 The sum of the array's elements is: 7.272
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