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

Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a Java program that

ID: 3737537 • Letter: L

Question

Lab #2. Chapter 7. PC #11. Array Operations (Page 491)

Write a Java program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods:

* getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array.

* getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array.

* getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array.

* getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array.

Explanation / Answer

ArrayOperations.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class ArrayOperations

{

/**

STEP1: getTotal method.

@param array An array of doubles.

@return The total of the values stored in the

argument array.

*/

public double getTotal(double[] array){

double sum=0.0;

for(int i=0; i<array.length; i++)

{sum+=array[i];}

return sum;}

/**

STEP2: getAverage method

@param array An array of doubles.

@return The average of the values stored in the

argument array.

*/

public double getAverage(double[] array1){

double average=getTotal(array1)/array1.length;

return average;

}

/**

STEP3: getHighest method

@param array An array of doubles.

@return The highest value in the argument array.

*/

public double getHighest(double[] array2){

double max = array2[0];

for(int i=0; i<array2.length; i++){

if(max < array2[i]){

max = array2[i];

}

}

return max;

  

}

/**

STEP4: getHighest method

@param array An array of doubles.

@return The highest value in the argument array.

*/

public double getLowest(double[] array2){

double min = array2[0];

for(int i=0; i<array2.length; i++){

if(min > array2[i]){

min = array2[i];

}

}

return min;

}

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

{

String fileName = args[0];

File file = new File(fileName);

if(file.exists()) {

Scanner fileInput = new Scanner(file);

int n = 0;

while(fileInput.hasNextDouble()) {

n++;

fileInput.nextDouble();

}

double arr[] = new double[n];

Scanner scan = new Scanner(file);

// Process the array.

for(int i=0; i<arr.length; i++){

arr[i] = scan.nextDouble();

}

//step5: declare an object for array operation

ArrayOperations obj = new ArrayOperations();

//step6: find the sum of the above array and print the result.

System.out.println("Total: "+obj.getTotal(arr));

System.out.println("Average: "+obj.getAverage(arr));

//step7: find the highest value in the above array and print the result.

System.out.println("Highest: "+obj.getHighest(arr));

System.out.println("Lowest: "+obj.getLowest(arr));

}

else {

System.out.println("File: "+args[0]+" does not exist");

System.out.println("Usage: java ArrayOperations <filename>");

}

}

}

Output:

Total: -24.0
Average: -2.4
Highest: 55.0
Lowest: -90.0

numbers.txt

2 4
-90 10 55 7
6 -20 11 -9