Write a program that accepts a file name from command line, then initializes an
ID: 3873384 • Letter: W
Question
Write a 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.(I NEED JAVA CODE)
output should be like this
double_input1.txt
double_input2.txt
double_input3.txt
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayCalcs {
private int size;
String [] parts =null;
// based on number of elements in file, array will create.
double[] arr=null;
public void readFile(String file){
try{
FileReader fr = new FileReader(file+".txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
int count = 0;
while (line != null) {
parts = line.split(" ");
for( String w : parts)
{
count++;
}
line = br.readLine();
}
size=count;
arr=new double[size];
for(int i=0;i<size;i++){
arr[i]=Double.parseDouble(parts[i]);
}
// System.out.println(count);
// System.out.println(Arrays.toString(parts));
System.out.println(Arrays.toString(arr));
} catch (FileNotFoundException ex){
System.out.println("This file does not exist");
} catch (IndexOutOfBoundsException e){
System.out.println("Thie file has more than 7 lines");}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
public double getTotal(){
double sum=0.0;
for(double d:arr){
sum=sum+d;
}
return sum;
}
public double getAverage(){
double avg=getTotal()/arr.length;
return avg;
}
public double getHighest(){
double high=arr[0];
for(int i=0;i<arr.length-1;i++){
if(arr[i] > high)
high = arr[i];
}
return high;
}
public double getLowest(){
double low=arr[0];
for(int i=0;i<arr.length-1;i++){
if(arr[i] < low)
low= arr[i];
}
return low;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter file name:");
String file=s.next();
ArrayCalcs ac=new ArrayCalcs();
ac.readFile(file);
System.out.println(" Highest Element: "+ac.getHighest());
System.out.println("Lowest Element: "+ac.getLowest());
System.out.println("Total: "+ac.getTotal());
System.out.println("Average: "+ac.getAverage());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.