Question

Hi I need some help on this lab. The world depends on its successfull compilation. /*...

Hi I need some help on this lab. The world depends on its successfull 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\n" +

"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 + "\narray 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\n", 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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\n" +
"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 + "\narray 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\n", 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

Add a comment
Know the answer?
Add Answer to:
Hi I need some help on this lab. The world depends on its successfull compilation. /*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE....

    JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to...

    JAVA: array return types May you please help me fix this coding. Sum is SUPPOSE to be 1253 import java.util.Scanner; public class Array { public static int sum(int[] arr) { int i; int total = 0; for ( i =0; i < arr.length; ++i) { total = total + arr[i]; } return total; } public static void main(String[] args) { int[] myArray = {45, 22, 18, 89, 82, 79, 15, 69, 100, 55, 48, 72, 16, 98, 57, 75, 44,...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT