Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Stat {
- // Methods
- public static void displayList(ArrayList <Double> list) {
- for(int i=0; i<list.size(); i++) {
- System.out.println("Index " + i + ": " + list.get(i));
- }
- }
- // 1. Find the average
- public static double findAverage(ArrayList <Double> list) {
- double sum = 0;
- for(int i=0; i<list.size(); i++) {
- sum += list.get(i);
- }
- return (sum / list.size());
- }
- // 2. Find the variance
- public static double findVariance(ArrayList <Double> list){
- double sum = 0;
- for(int i=0; i<list.size(); i++) {
- sum += list.get(i) * list.get(i);
- }
- sum -= (list.size() * findAverage(list) * findAverage(list));
- sum /= (list.size() - 1);
- return sum;
- }
- // 3. Find the mean
- public static double findMean(ArrayList <Double> list) {
- if(list.size() % 2 == 0) {
- int index = (int)((list.size() - 1) / 2);
- return (list.get(index) + list.get(index+1)) / 2;
- }
- else {
- int index = (list.size() - 1) / 2;
- return list.get(index);
- }
- }
- // MAIN FUNCTION
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Start giving me values and this process will stop, when you ");
- System.out.println("type 'as value' the NUMMBER -1000000 (-1 million)");
- System.out.println();
- double value = 0;
- int counter = 0;
- ArrayList <Double> list = new ArrayList <Double> ();
- while(value != -1000000) {
- counter++;
- System.out.print("Value " + counter + ": ");
- value = scanner.nextDouble();
- list.add(value);
- }
- // I have to remove the last element, which is -1000000 and indicated the stop of the session above
- System.out.println();
- list.remove(list.size()-1);
- // 1. I find the average
- double average = findAverage(list);
- System.out.println("* Average = E[X] = " + average);
- // 2. I find the variance and the standard deviation
- double var = findVariance(list);
- System.out.println("* Variance = σ^2 = " + var);
- System.out.println("* Standard Deviation = σ = " + Math.sqrt(var));
- // 3. I find the mean
- double mean = findMean(list);
- System.out.println("* Mean = x~ = " + mean);
- } // END OF MAIN FUNCTION
- } // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement