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...
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...
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...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...