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, 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 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) {

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 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.
// -->
// 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 Lab10 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 Lab10 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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.