Question

please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...

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 /**

  1.   7     This program reads numbers from a file, calculates the
    
  2.   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

  1.  20        double sum = 0;
    
  2.  21        int count = 0;
    
  3.  22        double mean = 0;
    
  4.  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
  1.  24        String line;         //To hold a line from the file
    
  2.  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
  1.  30        //Prompt the user and read in the file name
    
  2.  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
  1.  35        FileReader freader = new FileReader(filename);
    
  2.  36        //FileReader is meant for reading streams of
    
characters from a file
 37       //For reading streams of raw bytes, use
FileInputStream

38

  1.  39        BufferedReader input = new BufferedReader(freader);
    
  2.  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
  1.  45        //Priming read to read the first line of the file
    
  2.  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 {

  1.  54           //convert the line into a double value
    
  2.  55           //add the value to the sum.
    
  3.  56           sum = sum + Double.parseDouble(line);
    

57

  1.  58           //Increment the counter
    
  2.  59           count++; //count = count + 1
    

60

  1.  61           line = input.readLine();
    
  2.  62           System.out.println("Printing the line just read to
    
the monitor " + line);
 63       }
 64
  1.  65        //Close the input file
    
  2.  66        input.close();
    

67

  1.  68        //Store the calculated mean
    
  2.  69        mean = sum / count;
    

70

 71       //Reconnect to the FileReader object passing the
filename
 72       freader = new FileReader(filename);
 73
  1.  74        //Reconnect to the BufferedReader object passing it
    
  2.  75        //the FileReader object
    
  3.  76        input = new BufferedReader(freader);
    

77

  1.  78        sum = 0; //Reinitialize the sum
    
  2.  79        count = 0 ; //Reinitialize the count
    

80

  1.  81        //Priming read to read the first line of the file
    
  2.  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 {

  1.  87           //convert the line into a double
    
  2.  88           difference = Double.parseDouble(line) - mean;
    

89

  1.  90           //Add the square of the difference to the mean
    
  2.  91           sum = sum + Math.pow(difference, 2);
    

92

  1.  93           //Increment the counter
    
  2.  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
  1. 101        //Close the input file
    
  2. 102        input.close();
    

103

  1. 104        //Store the calculated standard deviation
    
  2. 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
  1. 113        //Print the results to the output file
    
  2. 114        output.printf("mean = %.3f\r\n", mean);
    
  3. 115        output.printf("standard deviation = %.3f", stdDev);
    

116

  1. 117        //close the output file
    
  2. 118        output.close();
    
119
120   }// end of main
121 } //end of class
122
123
124
125
126
127
128
129
130
131
132

Homework Answers

Answer #1

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........

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
This is a java assignment on repl.it my code works but I keep failing the tests....
This is a java assignment on repl.it my code works but I keep failing the tests. Can anyone help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place) For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage()...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method....
Do the TODOs in SongFileAccessor.java. It inherits from FileAccessor class. TODO 1: Implement the processLine method. When the text file is processed, each line of text will be passed to processLine . Each line contains 4 fields: title, album, artist, and play time. The album field is optional. Each field is separated by a comma. TODO 2: Implement the songToCSVString method. This method takes a Song object as a parameter and returns a String which is the csv representation of...
Write a program reverse-order.cpp which asks the user to input two dates (earlier date then later...
Write a program reverse-order.cpp which asks the user to input two dates (earlier date then later date). The program should report the West basin elevationfor all days in the interval in the reverse chronological order (from the later date to the earlier). Example: $ ./reverse-order Enter earlier date: 05/29/2018 Enter later date: 06/02/2018 06/02/2018 590.22 ft 06/01/2018 590.23 ft 05/31/2018 590.24 ft 05/30/2018 590.26 ft 05/29/2018 590.32 ft Hint: If for the previous tasks you did not use arrays, here...