Task #4 Calculating the Mean
Task #5 Calculating the Standard Deviation
Code Listing 4.1 (DiceSimulation.java)
import java.util.Random; // Needed for the Random class
/**
This class simulates rolling a pair of dice 10,000 times and counts the number of times doubles of are rolled for each different pair of doubles.
*/
public class DiceSimulation
{
public static void main(String[] args)
{
final int NUMBER = 10000; // Number of dice rolls
// A random number generator used in
// simulating the rolling of dice
Random generator = new Random();
int die1Value; // Value of the first die int die2Value; // Value of the second die int count = 0; // Total number of dice rolls int snakeEyes = 0; // Number of snake eyes rolls int twos = 0; // Number of double two rolls int threes = 0; // Number of double three rolls int fours = 0; // Number of double four rolls int fives = 0; // Number of double five rolls int sixes = 0; // Number of double six rolls
// TASK #1 Enter your code for the algorithm here // Display the results
System.out.println ("You rolled snake eyes " + snakeEyes + " out of " + count + " rolls."); System.out.println ("You rolled double twos " + twos + " out of " + count +
" rolls.");
System.out.println ("You rolled double threes " + threes + " out of " + count +
" rolls.");
System.out.println ("You rolled double fours " + fours + " out of " + count + " rolls.");
System.out.println ("You rolled double fives " + fives + " out of " + count + " rolls.");
System.out.println ("You rolled double sixes " + sixes + " out of " + count +
" rolls.");
}
}
Code Listing 4.2 (StatsDemo.java)
import java.util.Scanner;
// TASK #3 Add the file I/O import statement here
/**
This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file.
*/
public class StatsDemo
{
// TASK #3 Add the throws clause public static void main(String[] args)
{
double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the numbers double stdDev = 0; // The standard deviation String line; // To hold a line from the file double difference; // The value and mean difference // Create an object of type Scanner
Scanner keyboard = new Scanner (System.in);
String filename; // The user input file name
// Prompt the user and read in the file name
System.out.println("This program calculates " +
"statistics on a file " +
"containing a series of numbers"); System.out.print("Enter the file name: "); filename = keyboard.nextLine();
// ADD LINES FOR TASK #4 HERE
// Create a FileReader object passing it the filename // Create a BufferedReader object passing FileReader
// object
// Perform a priming read to read the first line of
// the file
// Loop until you are at the end of the file
// Convert the line to a double value and add the
// value to sum
// Increment the counter
// Read a new line from the file
// Close the input file
// Store the calculated mean
// ADD LINES FOR TASK #5 HERE
// Reconnect FileReader object passing it the
// filename
// Reconnect BufferedReader object passing
// FileReader object
// Reinitialize the sum of the numbers
// Reinitialize the number of numbers added
// Perform a priming read to read the first line of
// the file
// Loop until you are at the end of the file // Convert the line into a double value and
// subtract the mean
// Add the square of the difference to the sum
// Increment the counter
// Read a new line from the file
// Close the input file // Store the calculated standard deviation
// ADD LINES FOR TASK #3 HERE
// Create a FileWriter object using "Results.txt"
// Create a PrintWriter object passing the
// FileWriter object
// Print the results to the output file
// Close the output file
}
}
CODE:
StatsDemo.java
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
// TASK #3 Add the file I/O import statement here
/**
*
* This class reads numbers from a file, calculates the mean and standard
* deviation, and writes the results to a file.
*
*/
public class StatsDemo {
// TASK #3 Add the throws clause
public static void main(String[] args) throws IOException {
double sum = 0; // The sum of the numbers
int count = 0; // The number of numbers added
double mean = 0; // The average of the numbers
double stdDev = 0; // The standard deviation
String line; // To hold a line from the file
double difference; // The value and mean difference
DecimalFormat f = new DecimalFormat("##.000"); // To format decimal value to 3 places
Scanner keyboard = new Scanner(System.in); // Create an object of type Scanner
String filename; // The user input file name
System.out.println("This program calculates statistics on a file containing a series of numbers");
System.out.print("Enter the file name: ");
filename = keyboard.nextLine(); // Prompt the user and read in the file name
keyboard.close();
// ADD LINES FOR TASK #4 HERE
FileReader file = new FileReader(filename); // Create a FileReader object passing it the filename
BufferedReader in = new BufferedReader(file); // Create a BufferedReader object passing FileReader
line = in.readLine(); // Perform a priming read to read the first line of the file
while (null != line) { // Loop until you are at the end of the file
sum += Double.parseDouble(line); // Convert the line to a double value and add the value to sum
count++; // Increment the counter
line = in.readLine(); // Read a new line from the file
}
in.close(); // Close the input file
mean = sum / count; // Store the calculated mean
// ADD LINES FOR TASK #5 HERE
file = new FileReader(filename); // Reconnect FileReader object passing it the filename
in = new BufferedReader(file); // Reconnect BufferedReader object passing FileReader object
sum = 0; // Reinitialize the sum of the numbers
count = 0; // Reinitialize the number of numbers added
line = in.readLine(); // Perform a priming read to read the first line of the file
while (null != line) { // Loop until you are at the end of the file
difference = Double.parseDouble(line) - mean; // Convert the line to a double value and subtract the mean
sum += Math.pow(difference, 2); // Add the square of the difference to the sum
count++; // Increment the counter
line = in.readLine(); // Read a new line from the file
}
in.close(); // Close the input file
stdDev = Math.sqrt(sum / count); // Store the calculated standard deviation
// ADD LINES FOR TASK #3 HERE
FileWriter fileOut = new FileWriter("Results.txt");// Create a FileWriter object using "Results.txt"
PrintWriter result = new PrintWriter(fileOut);// Create a PrintWriter object passing the FileWriter object
result.print(f.format(mean) + "\n" + f.format(stdDev)); // Print the results to the output file
result.close();
file.close(); // Close the output file
System.out.println("Successful"); // To show the operation was successful
}
}
Please be careful while writing the filename. It should be full file name. For eg - sample.txt
Also if you are entering only the filename without the path then the file should be in the same project ( and not the same package ) as the StatsDemo.java class. The file Results.txt will also be generated in the same project folder.
THANKS,
PLEASE UPVOTE THE ANSWER.
Get Answers For Free
Most questions answered within 1 hours.