Advertisement
makispaiktis

Statistics Programme

Apr 2nd, 2020 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class Stat {
  5.    
  6.     // Methods
  7.     public static void displayList(ArrayList <Double> list) {
  8.         for(int i=0; i<list.size(); i++) {
  9.             System.out.println("Index " + i + ": " + list.get(i));
  10.         }
  11.     }
  12.    
  13.     // 1. Find the average
  14.     public static double findAverage(ArrayList <Double> list) {
  15.         double sum = 0;
  16.         for(int i=0; i<list.size(); i++) {
  17.             sum += list.get(i);
  18.         }
  19.         return (sum / list.size());
  20.     }
  21.    
  22.     // 2. Find the variance
  23.     public static double findVariance(ArrayList <Double> list){
  24.         double sum = 0;
  25.         for(int i=0; i<list.size(); i++) {
  26.             sum += list.get(i) * list.get(i);
  27.         }
  28.         sum -= (list.size() * findAverage(list) * findAverage(list));
  29.         sum /= (list.size() - 1);
  30.         return sum;
  31.     }
  32.    
  33.     // 3. Find the mean
  34.     public static double findMean(ArrayList <Double> list) {
  35.         if(list.size() % 2 == 0) {
  36.             int index = (int)((list.size() - 1) / 2);
  37.             return (list.get(index) + list.get(index+1)) / 2;
  38.         }
  39.         else {
  40.             int index = (list.size() - 1) / 2;
  41.             return list.get(index);
  42.         }
  43.     }
  44.    
  45.    
  46.    
  47.     // MAIN FUNCTION
  48.     public static void main(String[] args) {
  49.        
  50.         Scanner scanner = new Scanner(System.in);
  51.         System.out.println("Start giving me values and this process will stop, when you ");
  52.         System.out.println("type 'as value' the NUMMBER -1000000 (-1 million)");
  53.         System.out.println();
  54.         double value = 0;
  55.         int counter = 0;
  56.         ArrayList <Double> list = new ArrayList <Double> ();
  57.         while(value != -1000000) {
  58.             counter++;
  59.             System.out.print("Value " + counter + ": ");
  60.             value = scanner.nextDouble();
  61.             list.add(value);
  62.         }
  63.         // I have to remove the last element, which is -1000000 and indicated the stop of the session above
  64.         System.out.println();
  65.         list.remove(list.size()-1);
  66.         // 1. I find the average
  67.         double average = findAverage(list);
  68.         System.out.println("* Average = E[X] = " + average);
  69.         // 2. I find the variance and the standard deviation
  70.         double var = findVariance(list);
  71.         System.out.println("* Variance = σ^2 = " + var);
  72.         System.out.println("* Standard Deviation = σ = " + Math.sqrt(var));
  73.         // 3. I find the mean
  74.         double mean = findMean(list);
  75.         System.out.println("* Mean = x~ = " + mean);
  76.        
  77.     }   // END OF MAIN FUNCTION
  78.    
  79. }   // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement