please write the code in java so it can run on jGRASP import java.util.Scanner;
2 import java.io.*; //This imports input and output (io) classes that we use
3 //to read and write to files. The * is the wildcard that will
4 //make all of the io classes available if I need them
5 //It saves me from having to import each io class separately.
6 /**
7 This program reads numbers from a file, calculates the
8 mean (average) and standard deviation, and writes the
results 9 to a file.
10 */ 11 12 public class StatsDemo 13 { 14 public static void main(String[] args) throws IOException 15 { 16 //the throws IOException means that if we are unable
to read from or write 17 //to our files, we need to throw an exception which
means the program can 18 //not continue until we fix the problem causing the
exception to throw.
19
20 double sum = 0;
21 int count = 0;
22 double mean = 0;
23 double stdDev = 0;
//The sum of the numbers //The count of numbers added //The average of the numbers //The standard deviation - a
measure of how widely dispersed the numbers are
24 String line; //To hold a line from the file
25 double difference = 0; //The value and mean
difference 26 String filename; //Store the name of the file that
is being read
27 28 Scanner keyboard = new Scanner (System.in); 29
30 //Prompt the user and read in the file name
31 System.out.println("This program calculates statistics
on a file containing numbers."); 32 System.out.println("Enter the file name: ");
33 filename = keyboard.nextLine(); 34
35 FileReader freader = new FileReader(filename);
36 //FileReader is meant for reading streams of
characters from a file 37 //For reading streams of raw bytes, use
FileInputStream
38
39 BufferedReader input = new BufferedReader(freader);
40 //Creates a BufferedReader object passing it to the
FileReader object 41 //Java.io.BufferedReader class reads from a character-
input stream 42 //buffering characters so as to provide for the
efficient reading 43 //of a sequence of characters 44
45 //Priming read to read the first line of the file
46 line = input.readLine(); //readLine is a method to
read a line of text.
47 System.out.println("Printing the line just read to the monitor " + line);
48
49 //Loop continues reading the Numbers.txt until it hits the end of the file
50 //This loop is summing the numbers as it reads them in so that we can calc. the mean/avg
51 //This loop is counting how many numbers it has read in from the file
52 while (line != null) //null is nothing. We are reading until we find nothing
53 {
54 //convert the line into a double value
55 //add the value to the sum.
56 sum = sum + Double.parseDouble(line);
57
58 //Increment the counter
59 count++; //count = count + 1
60
61 line = input.readLine();
62 System.out.println("Printing the line just read to
the monitor " + line); 63 } 64
65 //Close the input file
66 input.close();
67
68 //Store the calculated mean
69 mean = sum / count;
70
71 //Reconnect to the FileReader object passing the filename
72 freader = new FileReader(filename); 73
74 //Reconnect to the BufferedReader object passing it
75 //the FileReader object
76 input = new BufferedReader(freader);
77
78 sum = 0; //Reinitialize the sum
79 count = 0 ; //Reinitialize the count
80
81 //Priming read to read the first line of the file
82 line = input.readLine(); //readLine is a method to
read a line of text.
83 System.out.println("Printing the line just read to the monitor " + line);
84
85 while (line != null) //null is nothing. We are reading until we find nothing
86 {
87 //convert the line into a double
88 difference = Double.parseDouble(line) - mean;
89
90 //Add the square of the difference to the mean
91 sum = sum + Math.pow(difference, 2);
92
93 //Increment the counter
94 count++; //count = count + 1
95 96 line = input.readLine(); //readLine is a method
to read a line of text.
97 System.out.println("Printing the line just read to the monitor " + line);
98
99 } //end of 2nd while loop 100
101 //Close the input file
102 input.close();
103
104 //Store the calculated standard deviation
105 stdDev = Math.sqrt(sum / count);
106 107 //Create the object of type FileWriter using MyResults.txt 108 FileWriter fwriter = new FileWriter("MyResults.txt"); 109 110 //Create an object of PrintWriter passing it to the FileWriter object 111 PrintWriter output = new PrintWriter(fwriter); 112
113 //Print the results to the output file
114 output.printf("mean = %.3f\r\n", mean);
115 output.printf("standard deviation = %.3f", stdDev);
116
117 //close the output file
118 output.close();
119 120 }// end of main 121 } //end of class 122 123 124 125 126 127 128 129 130 131 132
Code:
package assignment;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Count {
public static void main(String[] args) throws IOException {
double sum = 0;
int count = 0;
double mean = 0;
double stdDev = 0;
//The sum of the numbers
//The count of numbers added
//The average of the numbers
//The standard deviation - a
String line=null; //To hold a line
from the file
double difference = 0; //The value
and mean difference
String filename="location path of
the file"; //Store the name of the file that is being read
Scanner keyboard = new Scanner
(System.in);
//Prompt the user and read in the
file name
System.out.println("This program
calculates statistics on a file containing numbers.");
System.out.println("Enter the file
name: ");
filename =
keyboard.nextLine();
FileReader freader = new
FileReader(filename);
//FileReader is meant for reading
streams of characters from a file
//For reading streams of raw bytes,
useFileInputStream
BufferedReader input = new
BufferedReader(freader);
//Creates a BufferedReader object
passing it to the FileReader object
//Java.io.BufferedReader class
reads from a character input stream
//buffering characters so as to
provide for the efficient reading
//of a sequence of characters
//Priming read to read the first
line of the file
line = input.readLine(); //readLine
is a method to read a line of text.
System.out.println("Printing the
line just read to the monitor " + line);
//Loop continues reading the
Numbers.txt until it hits the end of the file
//This loop is summing the numbers
as it reads them in so that we can calc. the mean/avg
//This loop is counting how many
numbers it has read in from the file
while (line != null) //null is
nothing. We are reading until we find nothing
{
//convert the line into a double
value
//add the value to the sum.
sum = sum +
Double.parseDouble(line);
//Increment the counter
count++; //count = count + 1
line = input.readLine();
System.out.println("Printing the
line just read to the monitor " + line);
}
System.out.println("sum:
"+sum);
System.out.println("count:
"+count);
//Close the input file
input.close();
//Store the calculated
mean
mean = sum / count;
//Reconnect to the FileReader
object passing the filename
freader = new
FileReader(filename);
//Reconnect to the BufferedReader
object passing it
//the FileReader object
input = new
BufferedReader(freader);
sum = 0; //Reinitialize the
sum
count = 0 ; //Reinitialize the
count
//Priming read to read the first
line of the file
line = input.readLine(); //readLine
is a method to read a line of text.
System.out.println("Printing the
line just read to the monitor " + line);
while (line != null) //null is
nothing. We are reading until we find nothing
{
//convert the line into a
double
difference =
Double.parseDouble(line) - mean;
//Add the square of the
difference to the mean
sum = sum + Math.pow(difference,
2);
//Increment the counter
count++; //count = count + 1
line = input.readLine(); //readLine
is a method to read a line of text.
System.out.println("Printing the
line just read to the monitor " + line);
} //end of 2nd while loop
//Close the input file
input.close();
//Store the calculated standard
deviation
stdDev = Math.sqrt(sum /
count);
//Create the object of type
FileWriter using MyResults.txt
FileWriter fwriter = new
FileWriter("Numbers.txt");
//Create an object of PrintWriter
passing it to the FileWriter object
PrintWriter output = new
PrintWriter(fwriter);
//Print the results to the output
file
output.printf("mean = %.3f\r\n",
mean);
output.printf("standard deviation =
%.3f", stdDev);
//close the output file
output.close();
}// end of main
}//end of class
Note:***I hope your happy with my answer***If you have any doubts please comment me*****Thank you........
Get Answers For Free
Most questions answered within 1 hours.