Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need some help on this lab. The world depends on its successful compilation

ID: 3745023 • Letter: H

Question

Hi I need some help on this lab. The world depends on its successful compilation.

/*

Lab6.java Arrays, File input and methods

Read the comments and insert your code where indicated.

Do not add/modify any output statements. Do not modify main.

*/

import java.io.*;

import java.util.*;

public class Lab6

{

static final int NOT_FOUND = -1;

public static void main (String[] args) throws Exception

{

final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Lab6" on command line

if (args.length == 0 ) // i.e If you did not type anything after "java Lab6" on command line

{

System.out.println("FATAL ERROR: Must type a filename on cmd line " +

"Like this -> C:\Users\tim\Desktop>java Lab6 L6input1.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;

int[] keys = { 35, 17, 201 };

// LOAD THE ARRAY FROM FILE

while ( infile.hasNextInt() ) array[count++] = infile.nextInt(); // POST increment NOT pre. Do you see why?

// ECHO COUNT AND LENGTH

System.out.println( "array capacity: " + array.length + " array count: " + count);

printArray( "array elements: ", array, count ); // ECHO ALL (count) ELEMENTS ON ONE LINE

// TEST YOUR THREE METHODS ON THREE DIFFERENT KEYS

for ( int key : keys ) // same as writing for(int i=0 ; i < keys.length; ++i) key = keys[i]

{ System.out.println(" key="+key );

int first = indexOf( array, count, key );

System.out.println("first occurance of " + key + " found at index: " + first );

int last = lastIndexOf( array, count, key );

System.out.println("last occurance of " + key + " found at index: " + last );

System.out.println("number of occurances of " + key + " in the array is: " + countOccurances( array, count, key, first, last ) );

}

} // END main

// GIVEN AS IS: DO MOT MODIFY OR DELETE

private static void printArray( String caption, int[] a, int cnt )

{

System.out.print( caption );

for ( int i=0 ; i<cnt ;i++)

System.out.print( a[i] + " " );

System.out.println();

}

// #############################################################################################

// YOU MUST WRITE THE DEFINTIONS OF THE METHODS BELOW THAT ARE CALLED IN MAIN

// #############################################################################################

// INDEXOF: RETURNS INDEX OF FIRST OCCURANCE OF KEY IN THE ARRAY -OR- RETURNS NOT_FOUND

private static int indexOf( int []arr, int count, int key )

{

return NOT_FOUND; // JUST TO MAKE IT COMPILE. YOU CHANGE AS NEEDED;

}

// LASTINDEXOF: RETURNS INDEX OF LAST OCCURANCE OF KEY IN THE ARRAY -OR- RETURNS NOT_FOUND

private static int lastIndexOf( int []arr, int count, int key )

{

return NOT_FOUND; // JUST TO MAKE IT COMPILE. YOU CHANGE AS NEEDED;

}

// COUNTOCCURANCES: RETURNS THE NUMBER OF TIMES KEY APPEARS IN THE ARRAY

// USE THE FIRST & LAST VALUES TO MINIMIZE THE RANGE INSIDE THE ARRAY THAT IS SEARCHED

private static int countOccurances( int []arr, int count, int key, int first, int last )

{

return 0; // JUST TO MAKE IT COMPILE. YOU CHANGE AS NEEDED;

}

} //END OF LAB6 CLASS

Explanation / Answer


Given below is the code for the question. You will need to pass the name of the input file to the program. Otherwise it will generate error.
Let me know if you have any issues. Please use the input file P4input.txt provided by your instructor.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


/*
Lab4.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 Lab4
{
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 minIndex = 0; //assume first element is smallest
for(int i = 1; i < cnt; i++){
if(arr[i] < arr[minIndex])
minIndex = i;
}
return minIndex;
}
// 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)
{
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 maxIndex = 0; //assume first element is greatest
for(int i = 1; i < cnt; i++){
if(arr[i] > arr[maxIndex])
maxIndex = i;
}
return maxIndex;
}
// 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)
{
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 int calcAverage( int[] arr, int cnt)
{
return calcSum(arr,cnt) / cnt;
}

} //END OF LAB CLASS