Wednesday, January 11, 2012

Tutorial: Simple Statistics Calculator in Java

by eturo


Today, I will show you a simple Statistics Calculator Java program that allows the user to input any numbers then the program will display the total counts or number inputted, sum of all the numbers,  the smallest and largest number, the average and its Standard Deviation.

Here, I used the implementation of  polymorphism using different methods, with or without parameters (setting and getting), passing of variables, creating of object and the relationship of classes (inheritance).

The Design:

In designing the program, I create two classes: SimpleStatistics (the main program/class) and StatisticsCalculator (the subclass). The main program will served as the trigger to display the output of the program while the subclass is where we place the algorithm in setting and getting the data by passing it to the different methods we are going to create. The broken line indicates a relationship of each two classes where the subclass inheriting some attributes of the main class.




The image below is an example of the output dialog of the program. Lets see how this program works.































First, the program asked from the user to input any number (as many as you can).



Numbers were entered by the user.

 

Now, it also says here, that if you want to stop or quit entering numbers and see the output, you just press or enter the number 0 (see the image below).





Here, the user enter integer number 0, then the program quits and at the same time, it displays the statistical results based from the inputted numbers by the user.


The Program Source Code:

Here is the complete source code of both the main class and the subclass. Feel free to copy and paste it into your programming text editor. Compile it and see what the program actually do...


The StatisticCalculator class (the subclass)


public class StatisticCalculator
{
    private int count;    // the number of numbers that have been entered
    private double computeSum;    // The sum of all the items that have been entered.
    private double squareSum;  // The sum of the squares of all the items.
       
    private double maximum = Double.NEGATIVE_INFINITY;  // Largest item to be found or seen.
    private double minimum = Double.POSITIVE_INFINITY;  // Smallest item be found or seen.
    
    public void enterNumber(double num)
    {
        // Add the number to the dataset.
        count++;
        computeSum += num;
        squareSum += num*num;
       
        if (num > maximum)
        {
            maximum = num;
        }
        if (num < minimum)
        {
            minimum = num;
        }
        }
    
        public int getCount()
    { 
           return count; // Return number of items that have been entered.
        }
    
        public double getSum()
    {
             
           return computeSum; // Return the sum of all the items that have been entered.
        }
    
        public double getMean()
    {
              // Return average of all the items that have been entered.
              // Value is Double.NaN if count == 0.
           return computeSum / count; 
        }
    
        public double getStandardDeviation()
    { 
             // Return standard deviation of all the items that have been entered.
             // Value will be Double.NaN if count == 0.
           double mean = getMean();
           return Math.sqrt( squareSum/count - mean*mean );
        }
       
        public double getMinimum()
    {
             // Return the smallest item that has been entered.
             // Value will be infinity if no items have been entered.
           return minimum;
        }
       
        public double getMaximum()
    {
             // Return the largest item that has been entered.
             // Value will be -infinity if no items have been entered.
           return maximum;
        }
    
     }  // end class StaticCalculator




The SimpleStatistics class (the main class/program)

import java.util.Scanner;    //import statement to use the Scanner class to call input console (System.in)
    
public class SimpleStatistics {
    
        public static void main(String[] args)   //the main() method
    {
            Scanner read = new Scanner(System.in);  //declaration and instantiate the class Scanner
  
    // Computes statistics for numbers entered by user.   
    StatisticCalculator calculator = new StatisticCalculator ();
    //instantiate or create an object of the StatisticCalculator class
          
           double item;    // One number entered by the user.
           System.out.println ("Please enter any numbers.  Press 0 to quit.");
           System.out.println();
          
               do {       
            System.out.print("");
            item = read.nextDouble();//the program will accept double data/items from the keyboad
            if (item != 0)
            calculator.enterNumber(item);//call the enterNumber() method if the item is not zero
               } while ( item != 0 ); //if the user do not enter 0 the user can continue to enter items
          
        System.out.println ("\nStatistics about your calculator:\n");
               //display the total items entered
        System.out.println ("Total Count of the number: " + calculator.getCount());
        //display the sum
        System.out.println ("Sum is : " + calculator.getSum());
               //display the minimum or smallest number or item entered by the user
        System.out.println ("Minimum is : " + calculator.getMinimum());
        //display the maximum or largest number or item entered by the user
        System.out.println ("Maximum is: " + calculator.getMaximum());
        //display the mean or average of all the items or number entered by the user
        System.out.println ("The Average is: " + calculator.getMean());
            //display the standard deviation
        System.out.println ("The Standard Deviation is: " + calculator.getStandardDeviation());
          
        }  // end of main() method
     } // end SimpleStatistics
 


Note: Again, I'm reminding you guys that the line with two forward slashes are known as comment (single-line comment). If you see lines in Java source code with this symbols /* */, again this is another type of inserting comment in Java known as the multi-line comment, same with this symbol /** known as the javadoc comment. That's it. Another point of knowledge you have learned today. Happy coding...





Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...