Question
What this Lab Is About: Basic use of Array class. Use the following Coding Guidelines: When declaring a variable, you usually want to initialize it.
What this Lab Is About: Basic use of Array class. 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). 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. Assignment Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*------------------------------------------------------------------------- // AUTHOR: your name. // FILENAME: title of the source file. // SPECIFICATION: your own description of the program. // FOR: CSE 110- Lab #9 // TIME SPENT: how long it took you to complete the assignment. //-----------------------------------------------------------*/ Problem Description: Expand the Student class. 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 for the current array element and sum of array 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 current element. // --> // 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 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. // --> // 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: 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: Display and sum the arrays elements: 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 arrays values. The arrays 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 arrays in Java are zero-indexed, so the last element of the array is stored at the n-1th positon, 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: Lastly, print out the sum of the arrays 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. Sample Output: Bolded values represent user input. 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.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.1 0.11 The sum of the array's elements is: 4.61 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