Program #4 will require you to pass two command args to your program on the comm
ID: 3652311 • Letter: P
Question
Program #4 will require you to pass two command args to your program on the command line like this:10 = 67 23 49 27 51 57 55 12 10 3
15 = 13 98 14
72 73 81 64 53 2 21
33 19 35
26 1
20 =
99
88
77
66
55
44
33
22
11
12
23
34
45
56
67
78
89
91
21
32
C:> java Program4 15-ints.txt 30
The first command arg represents the file you are reading from. The second represents the capacify (.length) of the array you are allocating to hold all the ints from the file.
After you open the file for reading and allocate an array of length specified by the second command arg, you will read in all the ints from that file and store them in your array. Some of the input files will have all the ints all on one line. Another will have multiple ints on multiple lines. Another will have each int on its own line. The format of the input file makes no difference. A space, TAB or NEWLINE is treated as whitespace by the Scanner and any kind of whitespace serves to delimit one int from the next.
It makes sense that if the input file contains 15 ints, you better pass a capcity value on the command line that is at least 15. When I test your program I will always pass a capacity value on the command line that is larger than the actual number if ints in the file. This will ensure that the array you allocate to hold the ints has more than enough capcity. This will also force you to keep a count of the actual number of values you store into the array. There will be empty space at the back of your array and you must never assume that the array is full. Always go by count rather than the .length of the array. All your operations on the array will only look at the elements from index 0 through count-1.
After you read all the ints from the file into your array, and you have the actual count of values stored in your count variable, you are ready to do some array operations
You only need to fill in the code below main for the following methods:
private static void printArray( int array[], int cnt ); // prints all values in the array on one line with a space after each number
private static void printAllBelow( int array[], int cnt, double ave ); // prints all values that are less than the aveerage on one line
private static void printAllAbove( int array[], int cnt, double ave ); // prints all values that are greater than the average on one line
private static int findMin( int array[], int cnt ) // return the smallest int in the array
private static int findMax( int array[], int cnt ) // return the largest
private static double findAve( int array[], int cnt ) // return the average
*/
import java.io.*;
import java.util.*;
public class Program4
{
public static void main( String args[] ) throws Exception
{
if (args.length < 2 )
{
System.out.println("CMD INPUT ERROR: Must put an int and a filename on the comand line ");
System.exit(0);
}
// ONE ARRAY IS ALL YOU NEED. YOU WILL FILL IT WITH THE INTS IN THE FILE
Scanner infile = new Scanner( new File(args[0]) ); // A file of ints - you will read all into the array
int capacity = Integer.parseInt( args[1] );
int arr[] = new int[ capacity ];
int count=0;
// READ ALL THE INTS FROM THE FILE INTO THE ARRAY
// KEEP A COUNT OF HOW MANY YOU PUT INTO THE ARRAY
while( infile.hasNextInt() )
arr[count++] = infile.nextInt(); // READ AND STORE AN INT INTO THE ARRAY
// PRINT THE ARRAY
printArray( arr, count );
// PRINT MIN, MAX, AVE
int minVal = findMin( arr,count );
int maxVal = findMax( arr,count );
double aveVal = findAve( arr, count );
System.out.printf("min: %d max: %d ave: %.2f ", minVal, maxVal, aveVal);
// PRINT ELEMENTS < AVE
System.out.print("All vals below ave: ");
printAllBelow( arr, count, aveVal);
// PRINT ELEMENTS > AVE
System.out.print("All vals above ave: ");
printAllAbove( arr, count, aveVal );
} // END MAIN
// ............................................................................
// A L L M E T H O D S G O B E L O W M A I N
// ............................................................................
// printArray:
// print each value of the array one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printArray(int array[], int cnt )
{
// YOUR CODE HERE
}
// printAllBelow:
// print all value of the array that are BELOW the average one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printAllBelow(int array[], int cnt, double ave )
{
// YOUR CODE HERE
}
// printAllAbove
// print all value of the array that are ABOVE the average one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printAllAbove( int array[], int cnt, double ave )
{
// YOUR CODE HERE
}
// findMinVal
// loop over the array values and return the smalles value found
private static int findMin( int array[], int cnt )
{
// YOUR CODE HERE
return 0; // just to make it compile - you change as needed
}
// findMaxVal
// loop over the array values and return the largest value found
private static int findMax( int array[], int cnt )
{
// YOUR CODE HERE
return 0; // just to make it compile - you change as needed
}
// findAve
// Loop over the array values and return the average of the values
private static double findAve( int array[], int cnt )
{
// YOUR CODE HERE
return 0.0; // just to make it compile - you change as needed
}
} // END class
Explanation / Answer
Please rate...
Program:
===========================================================
import java.io.*;
import java.util.*;
public class Program4
{
public static void main( String args[] ) throws Exception
{
if (args.length < 2 )
{
System.out.println("CMD INPUT ERROR: Must put an int and a filename on the comand line ");
System.exit(0);
}
// ONE ARRAY IS ALL YOU NEED. YOU WILL FILL IT WITH THE INTS IN THE FILE
Scanner infile = new Scanner( new File(args[0]) ); // A file of ints - you will read all into the array
int capacity = Integer.parseInt( args[1] );
int arr[] = new int[ capacity ];
int count=0;
// READ ALL THE INTS FROM THE FILE INTO THE ARRAY
// KEEP A COUNT OF HOW MANY YOU PUT INTO THE ARRAY
while( infile.hasNextInt() )
arr[count++] = infile.nextInt(); // READ AND STORE AN INT INTO THE ARRAY
// PRINT THE ARRAY
printArray( arr, count );
// PRINT MIN, MAX, AVE
int minVal = findMin( arr,count );
int maxVal = findMax( arr,count );
double aveVal = findAve( arr, count );
System.out.printf("min: %d max: %d ave: %.2f ", minVal, maxVal, aveVal);
// PRINT ELEMENTS < AVE
System.out.print("All vals below ave: ");
printAllBelow( arr, count, aveVal);
// PRINT ELEMENTS > AVE
System.out.print("All vals above ave: ");
printAllAbove( arr, count, aveVal );
} // END MAIN
// ............................................................................
// A L L M E T H O D S G O B E L O W M A I N
// ............................................................................
// printArray:
// print each value of the array one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printArray(int array[], int cnt )
{
// YOUR CODE HERE
int i;
for(i=0;i<cnt;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
// printAllBelow:
// print all value of the array that are BELOW the average one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printAllBelow(int array[], int cnt, double ave )
{
// YOUR CODE HERE
int i;
for(i=0;i<cnt;i++)
{
if(array[i]<ave)System.out.print(array[i]+" ");
}
System.out.println();
}
// printAllAbove
// print all value of the array that are ABOVE the average one the same line with a space after each value
// then print a new line at the end SEE OUTPUT SCREENSHOT
private static void printAllAbove( int array[], int cnt, double ave )
{
// YOUR CODE HERE
int i;
for(i=0;i<cnt;i++)
{
if(array[i]>ave)System.out.print(array[i]+" ");
}
System.out.println();
}
// findMinVal
// loop over the array values and return the smalles value found
private static int findMin( int array[], int cnt )
{
// YOUR CODE HERE
int i,min=array[0];
for(i=0;i<cnt;i++)
{
if(array[i]<min)min=array[i];
}
return min; // just to make it compile - you change as needed
}
// findMaxVal
// loop over the array values and return the largest value found
private static int findMax( int array[], int cnt )
{
// YOUR CODE HERE
int i,max=array[0];
for(i=0;i<cnt;i++)
{
if(array[i]<max)max=array[i];
}
return max; // just to make it compile - you change as needed
}
// findAve
// Loop over the array values and return the average of the values
private static double findAve( int array[], int cnt )
{
// YOUR CODE HERE
int i,sum=0;
double ave;
for(i=0;i<cnt;i++)
{
sum=sum+array[i];
}
ave=(double)sum/cnt;
return ave; // just to make it compile - you change as needed
}
} // END class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.