Write a program that: Promote the user to enter the path to a file and opens the
ID: 3794892 • Letter: W
Question
Write a program that: Promote the user to enter the path to a file and opens the file. Read five integers from the input file. Print each number without decimals (before table). Prints a table showing the arithmetic, geometric, and harmonic means or the 5 integers to a file. Notes Show the averages with 2 decimal places. Left-justify type and right-justify value. Include and Info-for N digits: Arithmetic mean: (x_1 + x_2 +...+ x_N)/N Geometric mean: (x_1 * x_2 *...* x_N)^1/N Harmonic mean: N/(1/x_1 + 1/x_2 +...+ 1/x_N) USE C++ LANGUAGEExplanation / Answer
import java.io.*;
import java.util.*;
/**
* **
* @author ksumanth
*
* This Java function is written to read 5 integers from a input file and calculate the Arithmetic, Geometric, Harmonic Means of the numbers and write to a output file.
*
*/
public class PythogoreanMeans {
public static void main(String[] args) throws IOException{
//FileInputStream is used to read file from a path.
FileInputStream fis = null;
//BufferedReader object is used to read from console
BufferedReader input = null;
//Provide your File input path where the input file is present
fis = new FileInputStream("C:\Users\ksumanth\Documents\pythogoreanMeans\input.txt");
input = new BufferedReader(new InputStreamReader(fis));
//Provide your File output path to write the output
FileWriter writer = new FileWriter("C:\Users\ksumanth\Documents\pythogoreanMeans\output.txt");
String line = null;
int lineNo = 1;
//Exactly Five lines are present in the input
ArrayList<Integer> numbers = new ArrayList<>();
while(lineNo <= 5){
line = input.readLine().toLowerCase();
numbers.add(Integer.parseInt(line));
lineNo++;
}
//Printing all the 5 numbers at once in the console
for(Integer i : numbers){
System.out.print(i+" ");
}
System.out.println();
Double[] arr = calculatepythogoreanMeans(numbers);
//Writ the Arithmetic, Geometric and Harmonic Means to a file.
writer.write("Arithmetic Mean is " + arr[0]+" ");
writer.write("Goemetric Mean is " + arr[1]+" ");
writer.write("Harmonic Mean is " + arr[2]+" ");
//close the FileWriter object
writer.flush();
writer.close();
}
//Calculate the Arithmetic Mean, Harmonic Mean, Geometric Mean of the 5 numbers and store the results in a double Array.
private static Double[] calculatepythogoreanMeans(ArrayList<Integer> numbers) {
// TODO Auto-generated method stub
Double arr[] = new Double[3];
//Calculating the arithmetic Mean
arr[0] = (double) ((numbers.get(0) + numbers.get(1) + numbers.get(2) + numbers.get(3) + numbers.get(4))/5);
//calculating the geometric Mean
arr[1] = Math.pow((numbers.get(0)*numbers.get(1)*numbers.get(2)*numbers.get(3)*numbers.get(4)), 1/5);
//Calculating the geometric Mean
arr[2] = (double) 5/(1/numbers.get(0) + 1/numbers.get(1) + 1/numbers.get(2) + 1/numbers.get(3) + 1/numbers.get(4));
return arr;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.