ooo AT&T; 11:18 public asu.edu ASU CSE 110 Lab Due date/Time: Friday, Mar. 31, 2
ID: 3807866 • Letter: O
Question
ooo AT&T; 11:18 public asu.edu ASU CSE 110 Lab Due date/Time: Friday, Mar. 31, 2017 at 5:30pm What this Lab Is About: Learn how to define, intialize and use an array. Coding Guidelines for AILLabsAssignments faouwilLbe graded on this) Give identifiers semantic meaning and make them easy to read (examples numStudents. gross Pay, etc). Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). 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 properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs. 1.Lab Description For this lab, you will create a basic array of numbers, fill in the elements ofthat array by prompting the user for input, display the elements of the array back to the user, and then: Display the array elements in backwards order Calculate the sum of all the array elements. Count how many array elements are negative Find the largest array element. Create a class called Lab8. Use the same setup for setting up your class and main method as you did in previous labs and assignments. Be sure to name your file Lab&java; (again, no empty spaces and special characters are allowed, follow the naming conventions), For documentation purpose, at the beginning of each programming labassignment, you must have a comment block with the following infomation inside: AUTHOR: your name ASU ID: your 10 digits ASU ID FILENAME: title of the source file SPECIFICATION: description of the program TIME SPENT: how long it took you to complete the labExplanation / Answer
import java.util.Scanner;
/*------------------------------------------------------
// AUTHOR: Name
// ASU ID: <id>
// FILENAME:Lab8.java
// SPECIFICATION: How to define, utilize and use an array
// TIME SPENT:
*/
public class Lab8 {
public static void main(String[] args){
int arraySize;
double currentElement;
double sumOfElements=0;
double largestElement;
int numOfNegative=0;
Scanner scanner = new Scanner(System.in);
//get size of array from user
System.out.println("Enter size of the array :");
arraySize = scanner.nextInt();
//create array of elements of given size
double elements[] = new double[arraySize];
//filling array
for(int i=0; i<arraySize; i++){
System.out.println("Enter array element # "+i+" : ");
elements[i] = scanner.nextDouble();
}
//printing element in reverse and calculate sum
for(int i=arraySize-1; i>=0; i--){
currentElement = elements[i];
System.out.print(" "+currentElement);
sumOfElements =+ currentElement;
}
numOfNegative = 0;
largestElement = elements[0];
//count negative and find largest
for(int i=0; i<arraySize; i++){
currentElement = elements[i];
if(currentElement<0.0){
numOfNegative++;
}
if(currentElement > largestElement){
largestElement = currentElement;
}
}
//printing result
System.out.print(" Sum of array elements is : "+sumOfElements+" ");
System.out.print(" "+numOfNegative+" array elements are negative ");
System.out.print(" The Largest array elements is "+largestElement+" ");
System.out.print("");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.