Hi I need some help with this file. The world depends on its successful compilat
ID: 3745069 • Letter: H
Question
Hi I need some help with this file. The world depends on its successful compilation.
/*
Lab5.java Arrays, File input and methods
Read the comments and insert your code where indicated.
Do not add/modify any output statements
*/
import java.io.*;
import java.util.*;
public class Lab5
{
public static void main (String[] args) throws Exception
{
final int ARRAY_MAX = 30;
// "args" is the list of tokens you put after "java Project3" on command line
if (args.length == 0 ) // i.e If you did not type anything after "java Project4" on command line
{
System.out.println("FATAL ERROR: Must type a filename on cmd line " +
"Like this -> C:\Users\tim\Desktop>java Project4 P4input.txt");
System.exit(0); //ABORT program. Make user try again with a filename this time.
}
Scanner infile = new Scanner( new File(args[0]) );
int[] array = new int[ARRAY_MAX];
int count=0;
while ( infile.hasNextInt() )
array[count++] = infile.nextInt(); // POST increment NOT pre. Do you see why?
System.out.println( "array capacity: " + array.length + " array count: " + count);
printArray( array, count ); // ECHO ALL (count) ELEMENTS ON ONE LINE
System.out.println("The sum of the numbers in array is: " + calcSum( array, count ) );
System.out.println("The INDEX of the minimum value in array is: " + indOfMin( array, count ) );
System.out.println("The minimum value in array is: " + minVal( array, count ) );
System.out.println("The INDEX of the maximum value in array is: " + indOfMax( array, count ) );
System.out.println("The maximum value in array is: " + maxVal( array, count ) );
System.out.format("The average of the numbers in array is: %.2f ", calcAverage( array, count ) );
} // END main
// GIVEN AS IS: DO MOT MODIFY Or DELETE
private static void printArray( int[] a, int cnt )
{
System.out.print( "array elements: ");
for ( int i=0 ; i<cnt ;i++)
System.out.print( a[i] + " " );
System.out.println();
}
// #############################################################################################
// JUST LIKE IN LAB5. YOU MUST WRITE THE DEFINTIONS OF THE METHODS ABOVE THAT ARE CALLED IN MAIN
// #############################################################################################
// CALCSUM: (I'LL GIVE YOU THE TEMPLATE OF THE FIRST METHOD TO FOLLOW FOR THE REST)
private static int calcSum( int[] arr, int cnt)
{
int sum=0; // declare a var to hold the running total as you add each one increment
// HERE: write a for loop on i that adds each a[i] into sum
return sum; // DONE;
}
// INDOFMIN: You must examine each element and return index position
// of the smallest number in the array
// MINVAL: TRY TO RE-USE indOfMin in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
// INDOFMAX: You must examine each element and return index position
// of the largest number in the array
// MAXVAL: TRY TO RE-USE indOfMax in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
// CALCAVERAGE: TRY TO RE-USE calcSum in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
} //END OF LAB5 CLASS
Explanation / Answer
Hi Student Please copy the method implemented here in LAB5 class and paste it into your LAB5 class and then run the Application
-------------------------------------------------------------------------
public class Lab5 {
// CALCSUM: (I'LL GIVE YOU THE TEMPLATE OF THE FIRST METHOD TO FOLLOW FOR THE REST)
private static int calcSum( int[] arr, int cnt)
{
int sum=0;
// declare a var to hold the running total as you add each one increment
// HERE: write a for loop on i that adds each a[i] into sum
for(int a:arr)
sum=sum +a;
return sum; // DONE;
}
// INDOFMIN: You must examine each element and return index position
// of the smallest number in the array
private static int indOfMin(int[] arr,int cnt){
int index=0;
//In each iteration find the minimal value
//Compare the value at index to valu at i
for(int i=1;i<cnt;i++){
//If Value at i is less then store i into index
if(arr[i]<arr[index])
index=i;
}
return index;
}
// MINVAL: TRY TO RE-USE indOfMin in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
private static int minVal(int[] arr,int cnt){
//Re-use indOfMin to find the index of minimum element
//Then return it
return arr[indOfMin(arr,cnt)];
}
// INDOFMAX: You must examine each element and return index position
// of the largest number in the array
private static int indOfMax(int[] arr,int cnt){
int index=0;
//In each iteration find the max value
//Compare the value at index to valu at i
for(int i=1;i<cnt;i++){
//If Value at i is more then store i into index
if(arr[i]>arr[index])
index=i;
}
return index;
}
// MAXVAL: TRY TO RE-USE indOfMax in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
private static int maxVal(int[] arr,int cnt){
//Re-use indOfMax to find the index of maximum element
//Then return it
return arr[indOfMax(arr,cnt)];
}
// CALCAVERAGE: TRY TO RE-USE calcSum in your calculation
// If you do then this whole method is 1 liner return statement
// and you don't need any loops ;)
private static float calcAverage(int[] arr,int cnt){
//Re-use calcSum to find the total sum of array
//Then divide totalsum by number of element in array
//return that
return ((float)calcSum(arr,cnt)/cnt);
}
}//END OF LAB5 CLASS