/* Lab5.java Arrays, File input and methods Read the comments and insert your co
ID: 3906727 • Letter: #
Question
/*
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
Command Prompt C:Users im Desktopab-05 solution>java Lab5 L5inputl.txt array capacity: 30 array count: 17 array elements 217 113 159 135 38 31 155 203 201 197 97 30 107 238 94 3 230 The sum of the numbers in array is: 2248 The INDEX of the minimum value in array is: 15 The minimum value in array is: 3 The INDEX of the maximum value in array is: 13 The maximum value in array is: 238 he average of the numbers in array is: 132.24 C:Users imDesktop lab-05 solution>java Lab5 L5input2.txt array capacity: 30 array count: 18 array elements 77 3 14 177 36 216 27 250 143 79 200 123 64 202 205 225 85 88 he sum of the numbers in array is: 2214 he INDEX of the minimum value in array is: 1 he minimum value in array is: 3 he INDEX of the maximum value in array is: 7 he maximum value in array is: 250 The average of the numbers in array is: 123.00 C: Users im Desktoplab-05 solution>java Lab5 L5input3.txt array capacity: 30 array count: 10 array elements 225 141 159 127 192 22 120 241 101 170 The sum of the numbers in array is: 1498 The INDEX of the minimum value in array is: 5 The minimum value in array is: 22 The INDEX of the maximum value in array is: 7 The maximum value in array is: 241 The average of the numbers in array is: 149.80 C: Users imDesktoplab-05solution>_Explanation / Answer
PROGRAM
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
for(int i=0;i<cnt;i++)
sum+=arr[i];
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 low=arr[0]; // declare integer low with first array element
int index=0; // initialize integer index=0
for(int i=0;i<cnt;i++){ // create for loop until count(cnt) value
if(arr[i]<low) { // check condition lowest element
low=arr[i]; // assign lowest element into low
index=i; // set the position of lowest element into index
}
}
return index; // return index value
}
// 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 )
{
if(cnt==1) return arr[0]; // check condition counter(cnt) value equal to 1 then return first element of array
return Math.min(arr[cnt-1],minVal(arr,cnt-1)); // else check recursively to find minimum element in the array
}
// 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 high=0,index=0; // initialize 0 to two integer variables high and index
for(int i=0;i<cnt;i++) // create for loop until counter(cnt) value
{
if(arr[i]>high) { // check condition for highest element
high=arr[i]; // assign highest element of an array to high
index=i; // assign position of highest element into index
}
}
return index; // return highest position of index element
}
// 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 )
{
if(cnt==1) return arr[0]; // check condition counter(cnt) equal 1 then return first element of an array
return Math.max(arr[cnt-1],maxVal(arr,cnt-1)); // else return maximum element recursively
}
// 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 double calcAverage( int [] arr, int cnt )
{
int sum=calcSum(arr,cnt); // call calcSum() function and receive sum of array element into sum
return (double)sum/cnt; // then, calculate average element of an array and return
}
} //END OF LAB5 CLASS
OUTPUT-1:
F:>javac Lab5.java
F:>java Lab5 f:\L5input1.txt
array capacity: 30
array count: 17
array elements: 217 113 159 135 38 31 155 203 201 197 97 30 107 238 94 3 230
The sum of the numbers in array is: 2248
The INDEX of the minimum value in array is: 15
The minimum value in array is: 3
The INDEX of the maximum value in array is: 13
The maximum value in array is: 238
The average of the numbers in array is: 132.24
OUTPUT-2
F:>java Lab5 f:\L5input2.txt
array capacity: 30
array count: 18
array elements: 77 3 14 177 36 216 27 250 143 79 200 123 64 202 205 225 85 88
The sum of the numbers in array is: 2214
The INDEX of the minimum value in array is: 1
The minimum value in array is: 3
The INDEX of the maximum value in array is: 7
The maximum value in array is: 250
The average of the numbers in array is: 123.00
OUTPUT-3
F:>java Lab5 f:\L5input3.txt
array capacity: 30
array count: 10
array elements: 225 141 159 127 192 22 120 241 101 170
The sum of the numbers in array is: 1498
The INDEX of the minimum value in array is: 5
The minimum value in array is: 22
The INDEX of the maximum value in array is: 7
The maximum value in array is: 241
The average of the numbers in array is: 149.80
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.