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
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
private static final double[] SALES = {569456.36, 845397.99, 123456.36, 745213.67,
569698.80, 548712.53, 555555.12, 994545.99, 467558.1, 777777.65};
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
ComboBox<Integer> comboBox = new ComboBox<>();
for (int i = 2017; i >= 2008; i--) {
comboBox.getItems().add(i);
}
comboBox.getSelectionModel().select(0);
Label result = new Label("Choose year");
Button calculateSD = new Button("Standard Deviation");
calculateSD.setOnMouseClicked((MouseEvent me) -> {
double total = 0;
int index = comboBox.getSelectionModel().getSelectedIndex();
for (int i = index; i < SALES.length; i++) {
total += SALES[i];
}
double average = total / (SALES.length - index);
total = 0;
for (int i = index; i < SALES.length; i++) {
total += Math.pow(SALES[i] - average, 2);
}
result.setText(String.format("Standard Deviation from 2008 to %d is %.2f",
(2017 - index), Math.sqrt(total / (SALES.length - index))));
});
VBox vBox = new VBox(comboBox, result, calculateSD);
vBox.setSpacing(10);
vBox.setAlignment(Pos.CENTER);
stackPane.getChildren().add(vBox);
primaryStage.setScene(new Scene(stackPane));
primaryStage.setWidth(400);
primaryStage.setHeight(200);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
Comments
Leave a comment