Primitives Hacks
Demonstrates learning on primitives.
Statistics Calculator
Calculates minimum, maximum, mean, and standard deviation from user-inputted data.
Demonstrates key learning on Primitives .
import java.util.Scanner;
import java.lang.Math;
public class StatsCalculator {
ArrayList<Double> data;
public StatsCalculator() {
this.data = new ArrayList<>();
this.enterData();
}
private void enterData() {
Scanner input = new Scanner(System.in);
System.out.print("Enter an numerical datapoint, type a non-numerical value to exit: \n");
while(true) {
try {
double sampleInput = input.nextDouble();
System.out.println(sampleInput);
this.data.add(sampleInput);
} catch (Exception e) {
input.close();
break;
}
}
}
public double findMax() {
double max = this.data.get(0);
for (double num : this.data) {
if (num > max) {
max = num;
}
}
return max;
}
public double findMin() {
double min = this.data.get(0);
for (double num : this.data) {
if (num < min) {
min = num;
}
}
return min;
}
public double findAverage() {
double total = 0;
for (double num : this.data) {
total += num;
}
return total/this.data.size();
}
public double findStandardDeviation() {
double sum = 0;
for (double num : this.data) {
sum += num;
}
double mean = sum/this.data.size();
double standardDeviation = 0;
for (double num : this.data) {
standardDeviation += Math.pow(num - mean, 2);
}
return Math.sqrt(standardDeviation/(this.data.size() - 1));
}
public static void main(String[] args) {
StatsCalculator newData = new StatsCalculator();
System.out.println("\n----- summary -----");
System.out.println("data: " + newData.data);
System.out.println("maximum: " + newData.findMax());
System.out.println("minimum: " + newData.findMin());
System.out.println("mean: " + newData.findAverage());
System.out.println("standard deviation: " + newData.findStandardDeviation());
}
}
StatsCalculator.main(null);