elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  listas enlazada
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: listas enlazada  (Leído 1,988 veces)
Kendalii

Desconectado Desconectado

Mensajes: 1


Ver Perfil
listas enlazada
« en: 15 Septiembre 2018, 21:24 pm »

como hago un programa utilizando likedlist que calcule la media y la desviación estándar


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: listas enlazada
« Respuesta #1 en: 15 Septiembre 2018, 21:29 pm »

Google, y colabora con parte de tu código XD para ayudarte ...

 >:D >:D >:D

según la formula desviación estándar   ver linea 118 a 132 dos maneras de tantas  >:D


Código
  1. package foro;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.border.TitledBorder;
  5. import javax.swing.text.*;
  6. import java.awt.*;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10. import java.util.LinkedList;
  11. import java.util.stream.IntStream;
  12.  
  13. public class MediaDesviacionEstandar extends JFrame {
  14.  
  15.    private final LinkedList<Double> linkedList = new LinkedList<>();
  16.    private final JTextPane jTextPane = new JTextPane();
  17.    private final JScrollPane scrollPane = new JScrollPane(jTextPane);
  18.    private final JButton buttonSum  = new JButton("+");
  19.    private final JTextField jTextField = new JTextField(10);
  20.    private final JButton buttonProcesar = new JButton("Procesar");
  21.    private static final String TITLE = "Calcular Media y Desviación Estándar...";
  22.  
  23.    public MediaDesviacionEstandar() {
  24.        initFrame();
  25.        initBehaviour();
  26.    }
  27.  
  28.    private void initFrame() {
  29.        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  30.        scrollPane.setPreferredSize(new Dimension(30, 10));
  31.        scrollPane.setMinimumSize(new Dimension(30, 10));
  32.  
  33.        final TitledBorder border = new TitledBorder(TITLE);
  34.        border.setTitleJustification(TitledBorder.CENTER);
  35.        border.setTitlePosition(TitledBorder.TOP);
  36.  
  37.        final JPanel jPanel = new JPanel();
  38.        jPanel.setBorder(border);
  39.        jPanel.setLayout(new BorderLayout());
  40.  
  41.        final JPanel panelNort = new JPanel();
  42.        panelNort.setLayout(new FlowLayout());
  43.        panelNort.add(new JLabel());
  44.        panelNort.add(jTextField);
  45.        panelNort.add(buttonSum);
  46.        panelNort.add(buttonProcesar);
  47.        jPanel.add(panelNort, BorderLayout.NORTH);
  48.        jPanel.add(scrollPane);
  49.        jPanel.setPreferredSize(new Dimension(450, 250));
  50.  
  51.        setPreferredSize(new Dimension(500,500));
  52.        add(jPanel);
  53.        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.        pack();
  55.        setLocationRelativeTo(null);
  56.        setVisible(true);
  57.    }
  58.  
  59.    private void initBehaviour() {
  60.       buttonSum.addActionListener( e -> {
  61.           jTextField.requestFocus();
  62.           try {
  63.               linkedList.add(Double.valueOf(jTextField.getText().replace(",",".")));
  64.               media();
  65.               jTextField.setText("");
  66.           }catch (Exception ex) {
  67.               JOptionPane.showMessageDialog(this,"valor incorrecto","Error",0);
  68.               jTextField.requestFocus();
  69.           }
  70.       });
  71.       buttonProcesar.addActionListener( e -> {
  72.           jTextField.requestFocus();
  73.           if(linkedList.size() != 0) {
  74.               procesar();
  75.           }else {
  76.               JOptionPane.showMessageDialog(this,"lista vacia loco :X","Error",0);
  77.           }
  78.       });
  79.    }
  80.  
  81.    private void procesar() {
  82.        final StringBuilder sb = new StringBuilder();
  83.        final StringBuilder elementos = new StringBuilder();
  84.        linkedList.forEach(p -> elementos.append(p + " \\ "));
  85.        sb.append("Números procesados: " +  elementos  +"\n")
  86.                .append("La media es: " +media() + "\n")
  87.                .append("Desviación estándar método 1: " + desviacionEstandar1()+"\n")
  88.                .append("Desviación estándar método 2: " + desviacion2()+"\n")
  89.                .append("___________________________________\n");
  90.        linkedList.clear();
  91.        createJTextPane(sb.toString());
  92.    }
  93.  
  94.    private void createJTextPane(final String text) {
  95.        // Se inserta
  96.        final StyledDocument doc = jTextPane.getStyledDocument();
  97.        // Atributos para la frase, en negrita
  98.        final Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
  99.        final Style regular = doc.addStyle("regular", def);
  100.        StyleConstants.setFontFamily(regular, Font.SANS_SERIF);
  101.        StyleConstants.setFontSize(regular, 16);
  102.        try {
  103.            jTextPane.getStyledDocument().insertString(jTextPane.getStyledDocument().getLength(), text, doc.getStyle("regular"));
  104.        } catch (BadLocationException e) {
  105.            e.printStackTrace();
  106.        }
  107.  
  108.    }
  109.  
  110.    private double media() {
  111.        return linkedList.stream()
  112.                .mapToDouble(Double::doubleValue)
  113.                .average()
  114.                .orElse(Double.NaN);
  115.    }
  116.  
  117.    //con algo de java 8
  118.    private double desviacionEstandar1() {
  119.       final double sum =  IntStream.range(0 , linkedList.size()) // recorremos el array
  120.                .mapToDouble(p -> Math.pow(linkedList.get(p) - media() , 2)) //
  121.                .sum();
  122.        return Math.sqrt(sum / linkedList.size());
  123.    }
  124.  
  125.    //java 7
  126.    private double desviacion2() {
  127.        double  sum = 0;
  128.        for ( int f = 0; f < linkedList.size(); f++ )
  129.            sum += Math.pow ( linkedList.get(f) - media() , 2 );
  130.        return Math.sqrt ( sum / ( double ) linkedList.size() );
  131.    }
  132.  
  133.    public static void main(String... blabla) {
  134.        final String osType = System.getProperty("os.name");
  135.        try {
  136.          if(osType.contains("Win")) {
  137.              UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  138.          }else if(osType.contains("Linux")) {
  139.              UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  140.          }
  141.        }catch(Exception ex) {}
  142.        new Thread(() -> {
  143.            new MediaDesviacionEstandar();
  144.        }).start();
  145.    }
  146.  
  147. }
  148.  



« Última modificación: 14 Octubre 2018, 09:19 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Uso de Listas: Subprograma que lea 2 listas y forme una.
Dudas Generales
hbenitez 2 3,304 Último mensaje 8 Agosto 2010, 20:11 pm
por hbenitez
Ayuda Acerca De Listas y Listas Circulares (Revienta Memoria :S)
Programación C/C++
Gerik 0 5,150 Último mensaje 12 Septiembre 2010, 01:49 am
por Gerik
Problema con lista enlazada
Programación C/C++
Lain0x 2 3,417 Último mensaje 8 Julio 2011, 13:20 pm
por Valkyr
Lista enlazada simple – listas ligadas [C]
Programación C/C++
DanielPy 3 2,827 Último mensaje 9 Junio 2015, 17:38 pm
por ivancea96
Liberar memoria en listas simplemente enlazada [C]
Programación C/C++
DanielPy 4 3,703 Último mensaje 26 Junio 2015, 23:27 pm
por DanielPy
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines