Write a very small Java program that consist of: 1. A single class named Asn1BAp
ID: 3541177 • Letter: W
Question
Write a very small Java program that consist of:
1. A single class named Asn1BApp. Save the definition of this class in a file named Asn1BApp.java
2. The single class Asn1BApp should contain only the main method, and should read a file of numbers (of type double) from a file named asn1b.in. Should write the sum of those numbers (with appropriate label) and average of those numbers (with appropriate label). The sum and average should be displayed using 2 decimal points.
3. All output should be written to a file named asn1b.out. Use Scanner class for input and PrintWriter class for output.
4. No packages.
Please see the provided linked example files:
https://www.dropbox.com/s/okohny4f4u53iw6/infabric.dat
https://www.dropbox.com/s/23t62h43qtj0hid/PieceOfFabric.java
Explanation / Answer
import java.io.*;
import java.util.*;
public class Asn1BApp {
public static void main(String[] args) throws FileNotFoundException {
Scanner inputFile;
PrintWriter outputFile;
double value;
inputFile = new Scanner (new FileReader("C:\Users\Dhanraj Purbiya\Documents\NetBeansProjects\train\src\chegg\asn1b.dat"));
outputFile = new PrintWriter("C:\Users\Dhanraj Purbiya\Documents\NetBeansProjects\train\src\chegg\asn1b.out");
int i = 0; // index into fabricArray
value=0;
while (inputFile.hasNext()) {
value = value+inputFile.nextDouble();
i++;
}
inputFile.close();
Double average=value/i;
outputFile.println("Sum of all number is: "
+ value+" ");
outputFile.println("Average of all numbers is "
+ average);
outputFile.close();
} // end main
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.