/**
* Write a description of class GetStatistics here.
* A small application to compute basic statistics and display the statistics.
* Project Group: Raquel Blue, Sierra Cross, Trang Ngo
* 09 February 2016
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.text.DecimalFormat;
public class GetStatistics
{
public static void main(String[] args){
//Create an ArrayList of Integer using the getNums() method
ArrayList<Integer> nums = getNums();
double mean = calMean(nums); //calculate the mean using method calMean()
double stanDev = calStanDev(nums); //calculate standard deviation using method calStanDev()
int max = getMax(nums); //get the max value of the list using method getMax()
int min = getMin(nums); //get the min value of the list using method getMin()
//Create a formatting object to format
DecimalFormat formatter = new DecimalFormat("0.00");
//Print the statisctics to screen:
System.out.println("The mean of the data is " + formatter.format(mean) + ".");
System.out.println("The standard deviation of the data is " + formatter.format(stanDev) + ".");
System.out.println("The maximum value of the data is " + max + ".");
System.out.println("The minimum value of the data is " + min + ".");
}
public static ArrayList<Integer> getNums(){
//Create an ArrayList to hold the values from user's input
ArrayList<Integer> nums = new ArrayList<Integer>();
//Create a scanner object to get user's input
Scanner scan = new Scanner(System.in);
//Get user's input and initialize while loop until user enter -1
System.out.println("\nEnter number (Enter -1 again to stop): ");
Integer num = scan.nextInt();
while (num != -1){
//check if the entered number is a positive integer
if (num == 0 || num < -1){
System.out.println("Please enter only positive integers: ");
num = scan.nextInt();
} else{
nums.add(num);
System.out.println("Enter number (Enter -1 again to stop): ");
num = scan.nextInt();
}
}
return nums;
}
public static double calMean(ArrayList<Integer> nums){
double sum = 0;
//Use the for each loop to run through the values of the list nums
for (int num: nums){
sum += num;
}
return (sum/nums.size()); //return the mean
}
public static double calStanDev(ArrayList<Integer> nums){
double mean = calMean(nums); //get the mean of the list nums
int sumSquare = 0;
//for each loop to calculate standard deviation
for (int num:nums){
//subtract the number in the list by the mean and square each difference
sumSquare += Math.pow((num - mean),2); //add the squared differences together
}
double stanDev = Math.sqrt(sumSquare/nums.size()); //get the square root and put it into StanDev
return stanDev; //return the standard deviation
}
public static int getMax(ArrayList<Integer> nums){
int max = nums.get(0);//assign the first element of nums to max
//for each loop to go through the elements of nums
for(int num:nums){
//compare the value of max to the current value of num
//and then assign the larger value to max
if (max < num){
max = num;
}
}
return max; //return the max
}
public static int getMin(ArrayList<Integer> nums){
int min = nums.get(0); //assign the first element of nums to min
//for each loop to go through the elements of nums
for(int num:nums){
//compare the value of min to the current value of num
//and then assign the larger value to min
if (min > num){
min = num;
}
}
return min;//return the min
}
}