CSE 110 – Lab 10 What this Lab Is About: Basic use of the 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. •
CSE 110 – Lab 10 What this Lab Is About: Basic use of the 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). 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, 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.Array; 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, one double for 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 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 Array’s 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 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 arrays in Java are zero-indexed, so the last element of the array is stored at the n-1 th 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 array’s 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 Submission: Submit your Lab10.java file to the Submission Server. Go to the Submission Server site, https://courses.eas.asu.edu/cse110/login, then click on Lab Submissions in the left frame. Select Lab 10 from the dropdown box. Click on the browse button and find where you saved your Lab10.java file (and not the Lab10.class file) on your computer. Upload the file to the site and then click on the Submit button. Your file will be submitted and a screen will show up displaying if your program compiled and what your output is when run on some sample input. You should then check to make sure that the actual file submitted properly and is readable to the grader. To do so click on Grades in the frame on the left of the page and then click on the 0 underneath Lab10. You will again see that your program compiled and the sample output, but you should scroll down to the bottom of the screen and make sure your file is readable as well.
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
