Question

In: Computer Science

For input you can either a console Scanner or a dialog box (a modal dialog) and...

For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble()

---

Write a java ”Statistics program” to calculate the mean, variance and standard deviation for a given set of numbers. The user in entering the data interactively. The size of the data set will be determined at run time. Test it with all methods taught in chapter 8 (user input, random, loop, redirection). Recall the following formulas for mean, variance, and Standard deviation.

Solutions

Expert Solution


/*Importing packages will help in making classes and interface available in our class, so we dont have to make it again*/

import java.util.ArrayList;

import javax.swing.JOptionPane;

public class Statistics_program {

   public static void main(String[] args) {
   JOptionPane.showMessageDialog(null,"Please enter your data by clicking OK button:");
   String s1=JOptionPane.showInputDialog("Please enter the number of values you want to enter:");
  
   int n=Integer.parseInt(s1);
  
  
   /*Here we have to use ArrayList in order to store
   * any number of values we want for finding their mean,
   * variance and standard deviation*/
   ArrayList<Integer> al=new ArrayList<Integer>();
  
  
  
  
   /*This for loop is for taking input from user interactively by asking values one by one
   * for which mean,variance and standard deviation has to be calculated*/
   for(int i=0;i<n;i++)
   {
       String k=JOptionPane.showInputDialog("enter "+(i+1)+" value");
       int parse=Integer.parseInt(k);
               al.add(parse);
   }
  

   /****This is a method name, "printing_mean_variance_and_Standard_deviation(al);"*************/
   /*Method which will call and print all the values, that are
   * MEAN
   * VARIANCE
   * and STANDARD DEVIATION*/
   printing_mean_variance_and_Standard_deviation(al);

  
  
   }/*Main method ends*/


  
  
   /*Calculating and Returning Mean from this method*/
   static double Mean(ArrayList<Integer> al)
   {
       int sum=0;
       int arraylist_size=al.size();
       for(int i=0;i<arraylist_size;i++)
       {
           sum+=al.get(i);
       }
       double mean=(double)sum/(double)arraylist_size;
      
           JOptionPane.showMessageDialog( null, "mean is: "+mean);
   return mean;
   }
  
  
  
  
  
  
  
   /*Calculating and Returning Variance from this method*/
   static double Variance(ArrayList<Integer> al)
   {
       double variance_sum=0;
       double mean=Mean(al);
    int arraylist_size=al.size();

           JOptionPane.showMessageDialog(null, "We know that variance is defined as the"
                   + " Sum_Of_( (al.get(i)-mean) * (al.get(i)-mean) ) for each value of i");
          
          
           /*Here we have used for each loop for taking input
           * from array list easily and then storing it in
           * variance_sum variable by firstly subtracting mean
           * from each value and then multiplying it another time
           * with the same (x-mean)*/
           for(int x:al)
           {
               variance_sum+=(x-mean)*(x-mean);
           }
          
          
          
           /*variance will be calculated by dividing variance_sum with
           * the number of elements present here inside array list,entered by user */
           double variance=(double)variance_sum/arraylist_size;
          
          
          
           /*Printing variance on JOptionPane*/
           JOptionPane.showMessageDialog( null, "Variance is: "+variance);
          
          
           return variance;
          
   }/*Variance method ends*/
  
  
  
  
  
  
  
  
   /*Calculating and printing all the three from this method*/
   static void printing_mean_variance_and_Standard_deviation(ArrayList al)
   {
      
       /*Here standard deviation can be calculated by a single line
       * and using inbuilt math function for square root " sqrt "
       * which is available in java. As we know that standard deviation
       * is the square root of variance, so we are passing variance inside
       * sqrt function which will call "Variance method"
       * and will return variance inside sqrt function*/
       double standard_dev=Math.sqrt(Variance(al));
      
      
       JOptionPane.showMessageDialog(null, "standard_deviation= "+standard_dev);      
   }/*Method for printing and calling ends*/  

}/*CLASS ENDS*/



Related Solutions

For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() ---- Write a java the ”Letter grade” program that inputs several test scores from user and displays for each the corresponding letter grade using a scale 90 – 80...
For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() ----- . If an amount of A dollars is borrowed at an interest rate (expressed as a decimal) r for y years, and n is the number of annual...
For input you can either a console Scanner or a dialog box (a modal dialog) and...
For input you can either a console Scanner or a dialog box (a modal dialog) and the static method showInputDialog() of the JOptionPane class which is within the javax.swing package like this String name= newJOptionPane.showInputDialog(” Please enter your data:”). Then convert the String object to a number using Integer.parseInt() or Double.parseDouble() ----- Write a Java program to calculate the first n ( the user enters the integer n) rows of the Pascal triangle. On demand display the first n rows...
7) Write a program to open an input dialog box and read a string value. Write...
7) Write a program to open an input dialog box and read a string value. Write the string back to the user using a message box. USING MIPS ASSEMBLY LANGUAGE
Please write a java program (using dialog box statements for user input) that has the following...
Please write a java program (using dialog box statements for user input) that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled,...
The Views options of the insert component dialog box allows you to insert components symbols in...
The Views options of the insert component dialog box allows you to insert components symbols in a drawing A. inline with connectors B. above or below the wire C. with or without text D. with or without JIC symbols
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a...
You can purchase an optical scanner today for $450. The scanner provides benefits worth $66 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today? Yes No When is the best purchase time? Wait for 4 years Wait for 5 years Wait for 6 years
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a...
You can purchase an optical scanner today for $400. The scanner provides benefits worth $60 a year. The expected life of the scanner is 10 years. Scanners are expected to decrease in price by 20% per year. Suppose the discount rate is 10%. Should you purchase the scanner today or wait to purchase? When is the best purchase time?
Text effects can play an important role in polishing a document, but they also can overwhelm a document and distract readers. Consider the text effects available in the Font Dialog Box.
Microsoft Word ClassText effects can play an important role in polishing a document, but they also can overwhelm a document and distract readers. Consider the text effects available in the Font Dialog Box.How many effects are too many/just right for use in a document?What is the result of using too many effects?At what point is the document overwhelmed?Should the type of effect used vary according to the type of document?Please share your experiences in which content was undermined by animated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT