Answer to Question #179335 in Java | JSP | JSF for Ramla Awil

Question #179335

In this assignment you have to write a program in JAVA to create the Standard Deviation (SD) of the sales for the past 10 years. The standard deviation gives the sales manager a full idea of how the market changed in the past 10 years. 1. Design an Input dialog box window which accepts last year’s sale from a drop down menu: Year 2017 2016 2015 For example, if last year’s sale selected is 2017, the SD will be calculated from 2008 to 2017.


1
Expert's answer
2021-04-08T02:58:37-0400
import javax.swing.JOptionPane;

public class Q179335 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //get year
        String[] yearsStr  = new String[]{"2015", "2016", "2017"};
        String choice = (String) JOptionPane.showInputDialog(
                null,
                "Select the year:",
                "Standard Deviation (SD)",
                JOptionPane.PLAIN_MESSAGE,
                null,
                yearsStr,
                yearsStr[0]);
        
        int selectedYear=Integer.parseInt(choice);
        double sales[] =new double[10];
        int counter=0;
        //read sales for each year
        for(int y=selectedYear-9;y<=selectedYear;y++){
             sales[counter]=Double.parseDouble(JOptionPane.showInputDialog("Ehter the sales for "+y+" year: "));
             counter++;
        }
        double sum = 0.0;
        double mean = 0.0;
        double meanSum = 0.0;
        //calculate sales sum
        for (int i = 0; i < 10; i++) {
            sum += sales[i];
        }
        //calculate mean
        mean = sum /10.0;
        //calculate mean sum
        for (int i = 0; i < 10; i++) {
            meanSum+=  Math.pow((sales[i] - mean), 2);        
        }
        //calculate standardDeviation
        double standardDeviation = Math.sqrt(meanSum / 10.0);
        //display standard deviation
        JOptionPane.showMessageDialog(null,"Standard Deviation = "+String.format("%.2f", standardDeviation));
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog