como hago un programa utilizando likedlist que calcule la media y la desviación estándar
Google, y colabora con parte de tu código XD para ayudarte ...
>:D >:D >:D
según la formula desviación estándar(http://2.bp.blogspot.com/-ezEC6TlAaYg/W8Ltw5P6QOI/AAAAAAAADHs/_W3BJDuV2nswZHzaRN14HMlwI7F5FO5OwCK4BGAYYCw/s400/desviacionEstandar.png)
ver linea 118 a 132 dos maneras de tantas >:D
package foro;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;
import java.util.stream.IntStream;
public class MediaDesviacionEstandar extends JFrame {
private final LinkedList<Double> linkedList = new LinkedList<>();
private final JTextPane jTextPane = new JTextPane();
private final JScrollPane scrollPane = new JScrollPane(jTextPane);
private final JButton buttonSum = new JButton("+");
private final JTextField jTextField = new JTextField(10);
private final JButton buttonProcesar = new JButton("Procesar");
private static final String TITLE = "Calcular Media y Desviación Estándar...";
public MediaDesviacionEstandar() {
initFrame();
initBehaviour();
}
private void initFrame() {
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(30, 10));
scrollPane.setMinimumSize(new Dimension(30, 10));
final TitledBorder border = new TitledBorder(TITLE);
border.setTitleJustification(TitledBorder.CENTER);
border.setTitlePosition(TitledBorder.TOP);
final JPanel jPanel = new JPanel();
jPanel.setBorder(border);
jPanel.setLayout(new BorderLayout());
final JPanel panelNort = new JPanel();
panelNort.setLayout(new FlowLayout());
panelNort.add(new JLabel());
panelNort.add(jTextField);
panelNort.add(buttonSum);
panelNort.add(buttonProcesar);
jPanel.add(panelNort, BorderLayout.NORTH);
jPanel.add(scrollPane);
jPanel.setPreferredSize(new Dimension(450, 250));
setPreferredSize(new Dimension(500,500));
add(jPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void initBehaviour() {
buttonSum.addActionListener( e -> {
jTextField.requestFocus();
try {
linkedList.add(Double.valueOf(jTextField.getText().replace(",",".")));
media();
jTextField.setText("");
}catch (Exception ex) {
JOptionPane.showMessageDialog(this,"valor incorrecto","Error",0);
jTextField.requestFocus();
}
});
buttonProcesar.addActionListener( e -> {
jTextField.requestFocus();
if(linkedList.size() != 0) {
procesar();
}else {
JOptionPane.showMessageDialog(this,"lista vacia loco :X","Error",0);
}
});
}
private void procesar() {
final StringBuilder sb = new StringBuilder();
final StringBuilder elementos = new StringBuilder();
linkedList.forEach(p -> elementos.append(p + " \\ "));
sb.append("Números procesados: " + elementos +"\n")
.append("La media es: " +media() + "\n")
.append("Desviación estándar método 1: " + desviacionEstandar1()+"\n")
.append("Desviación estándar método 2: " + desviacion2()+"\n")
.append("___________________________________\n");
linkedList.clear();
createJTextPane(sb.toString());
}
private void createJTextPane(final String text) {
// Se inserta
final StyledDocument doc = jTextPane.getStyledDocument();
// Atributos para la frase, en negrita
final Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
final Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(regular, Font.SANS_SERIF);
StyleConstants.setFontSize(regular, 16);
try {
jTextPane.getStyledDocument().insertString(jTextPane.getStyledDocument().getLength(), text, doc.getStyle("regular"));
} catch (BadLocationException e) {
e.printStackTrace();
}
}
private double media() {
return linkedList.stream()
.mapToDouble(Double::doubleValue)
.average()
.orElse(Double.NaN);
}
//con algo de java 8
private double desviacionEstandar1() {
final double sum = IntStream.range(0 , linkedList.size()) // recorremos el array
.mapToDouble(p -> Math.pow(linkedList.get(p) - media() , 2)) //
.sum();
return Math.sqrt(sum / linkedList.size());
}
//java 7
private double desviacion2() {
double sum = 0;
for ( int f = 0; f < linkedList.size(); f++ )
sum += Math.pow ( linkedList.get(f) - media() , 2 );
return Math.sqrt ( sum / ( double ) linkedList.size() );
}
public static void main(String... blabla) {
final String osType = System.getProperty("os.name");
try {
if(osType.contains("Win")) {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}else if(osType.contains("Linux")) {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}
}catch(Exception ex) {}
new Thread(() -> {
new MediaDesviacionEstandar();
}).start();
}
}
(http://4.bp.blogspot.com/-ll5mdLdeIUA/W78DEcUBaAI/AAAAAAAADHE/ZMzPeBYoCH87ATHuEyI9_W0WmrdPE9QlQCK4BGAYYCw/s1600/mediaYDesviacionEstandar.jpg)