Assume that you have a data file that includes an arbitrary number of integers (
ID: 668203 • Letter: A
Question
Assume that you have a data file that includes an arbitrary number of integers (a sample of values from a population). Each line in the file will include 0 or more integers, separated with commas. Your script should read the file, extract the integers, compute the sample mean and sample standard deviation of the sample, and print the number of integers in the sample along with the computed sample mean and standard deviation to the screen. Make sure that the printed output is labeled so that the user knows which values are which. The sample data file is named ‘data1.txt’. Note that I do not want you to use numpy or another package to compute the mean and standard deviation – I want you to compute these yourself (in the code). Use 3 decimal points of accuracy when printing the results.
Explanation / Answer
Stats.java
public class Stats {
public static void main(String[] args) {
// read in numbers
Bag<Double> numbers = new Bag<Double>();
while (!StdIn.isEmpty()) {
numbers.add(StdIn.readDouble());
}
int N = numbers.size();
// compute mean
double sum = 0.0;
for (double x : numbers)
sum += x;
double mean = sum / N;
// compute standard deviation
sum = 0.0;
for (double x : numbers) {
sum += (x - mean) * (x - mean);
}
double std = Math.sqrt(sum / (N - 1));
StdOut.printf("Mean: %.2f ", mean);
StdOut.printf("Std dev: %.2f ", std);
}
}
bag.java
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Bag<Item> implements Iterable<Item> {
private int N; // number of elements in bag
private Node first; // beginning of bag
/**
* Create an empty stack.
*/
public Bag() {
first = null;
N = 0;
}
/**
* Is the BAG empty?
*/
public boolean isEmpty() {
return first == null;
}
/**
* Return the number of items in the bag.
*/
public int size() {
return N;
}
/**
* Add the item to the bag.
*/
public void add(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
N++;
}
/**
* Return an iterator that iterates over the items in the
bag.
*/
public Iterator<Item> iterator() {
return new ListIterator();
}
// helper linked list class
private class Node {
private Item item;
private Node next;
}
// an iterator, doesn't implement remove() since it's
optional
private class ListIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException
();
Item item = current.item;
current = current.next;
return item;
}
}
}
XStats.java
//package algs13;
import stdlib.*;
public class XStats {
public static void main(String[] args) {
// read in numbers
final Bag<Double> numbers = new Bag<>();
while (!StdIn.isEmpty()) {
numbers.add(StdIn.readDouble());
}
final int N = numbers.size();
// compute mean
double sum = 0.0;
for (double x : numbers)
sum += x;
final double mean = sum/N;
// compute standard deviation
sum = 0.0;
for (double x : numbers) {
sum += (x - mean) * (x - mean);
}
final double std = Math.sqrt(sum/(N-1));
StdOut.printf("Mean: %.2f ", mean);
StdOut.printf("Std dev: %.2f ", std);
}
}
input.txt
100 99 101 120 98 107 109 81 101 90
output
100 99 101 120 98 107 109 81 101 90
Mean: 100.60
Std dev: 10.51
Java Stats < input.txt100 99 101 120 98 107 109 81 101 90
Mean: 100.60
Std dev: 10.51