LABORATORY ASSIGNMENT No 6 Dr. Duncan, CSC 1350, Louisiana State University 10/2
ID: 3602474 • Letter: L
Question
LABORATORY ASSIGNMENT No 6 Dr. Duncan, CSC 1350, Louisiana State University 10/26/2017 Working with Arrays The AverageFinder Program In class we discussed common array algorithms such as finding minimummaximum, doing summation and computing an average when the data is stored in an array. Those algorithms generally involve using a loop to traverse the array. We also discussed how a program can be modularized by including user-defined methods other than the main method. Up to this point, all of our programs consisted of only the main method. Al times better by viewing the overarching task of the program as a series of sublasks. In today's lab, some of the subtasks of the program will be performed in methods other than the main. a program can be structured Preliminary Version This version of the program will consist of only one method, the main. The main method does the following A. Prompts the end-user for the number students (at least 5) and then allocates an integer array, testScores, with the capacity to store each student's test score B. Using a for-loop, reads each test score, an integer between 0 - 100, and indicates the number of test scores still left to be read C. Prints the contents of the testScores array after the loop using standard Java API Ar- rays.toString method. Intermediate Version Define a method similar to the Arrays.toString method of the Java API. However, unlke Ar rays. toString, your user-define toString method will display the array using braces (0). . Gives a string representation of the contents of an array in the fornat ted, e1, e2, en-1. where eo is the first element of the array and each ei represents successive elements of the array e Oparan data a filled array of integers ereturn a string representing the array in the specified format. public static String toString(int(] data) 11 provide the missing code Page 1Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
/**
*
* This class is written for take input from user in the for of array and
* then perform operations like display array,find average of all student,average of average mark and above,
* find average of D of higher scores ,maximum score and number of C scorer
* @author Sachin
*
*/
public class AverageFinder {
public static void displayInputArray(int arr[]){
System.out.println(" List of Scores:");
for (int i = 0; i < arr.length; i++) {
System.out.print("["+arr[i]+"] ");
}
}
public static double findAverage(int arr[],int numers){
double avg;
int total=0;
for (int i = 0; i < arr.length; i++) {
total=total+arr[i];
}
avg=total/numers;
System.out.println("Average Test Score= "+total+"/"+numers+"="+avg);
return avg;
}
public static void findAverageOfDScore(int arr[]){
double avg;
int total=0;
int number=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>=60){
total=total+arr[i];
number++;
}
}
avg=total/number;
System.out.println("Average 'D' or Higher Test Score= "+total+"/"+number+"="+avg);
}
public static void findAverageOfCScore(int arr[]){
int number=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>=90){
number++;
}
}
System.out.println("Number of 'C' Test Scorer "+number);
}
public static void findMax(int arr[]){
int max=0;
Arrays.sort(arr);
max=arr[arr.length-1];
System.out.println("Maximum Test Score = "+max);
}
public static void findAverageOf(int[] arr, double avg) {
double avg2;
int total=0;
int number=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]>=avg){
total=total+arr[i];
number++;
}
}
avg2=total/number;
System.out.println("Average of Test Scores ("+avg+") "+total+"/"+number+"="+avg2);
}
public static void main(String[] args) {
System.out.println("Enter number of student(not less than 5)-->");
Scanner sc=new Scanner(System.in);
int numers=Integer.parseInt(sc.nextLine());
int arr[]= new int[numers];
if(numers>=5){
int cnt=numers;
System.out.println("Enter "+numers+" Test Score between 0-100");
System.out.println("============================================================");
for(int i=0;i<numers;i++){
System.out.println("Enter The Test score [" +cnt-- +" Remainig]-->");
arr[i]=Integer.parseInt(sc.nextLine());
}
System.out.println("============================================================");
}
else{
System.out.println("Try again with atleast 5 numbers");
}
ArrayOperation.displayInputArray(arr);
System.out.println();
double avg=ArrayOperation.findAverage(arr,numers);
ArrayOperation.findAverageOf(arr,avg);
ArrayOperation.findAverageOfDScore(arr);
ArrayOperation.findMax(arr);
ArrayOperation.findAverageOfCScore(arr);
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.