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

I will also test it on other input files with other keys. Your program must work

ID: 3913101 • Letter: I

Question

I will also test it on other input files with other keys. Your program must work with any input file.

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

Command Prompt C: Users im Desktoplab-06>java Lab6 L6input1.txt array capacity: 30 array count: 17 array elements: 217 113 35 135 38 35 155 203 201 35 97 30 35 238 94 3 230 key 35 first occurance of 35 found at index: 2 last occurance of 35 found at index: 12 number of occurances of 35 in the array is: 4 key-17 first occurance of 17 found at index: 1 last occurance of 17 found at index: -1 number of occurances of 17 in the array is: 0 key-201 first occurance of 201 found at index: 8 last occurance of 201 found at index: 8 number of occurances of 201 in the array is: 1 C:Users imDesktoplab-06>

Explanation / Answer

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? // because index are always one less than size // so for a 1 element array, only index 0 is present. // 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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote