Prueba regresion lineal

26
Fecha de Entrega: 01 de diciembre de 2011. Integrantes: -Robert Calbul Yevilao. -Camilo Friz Gómez. -Jonathan Lincoqueo. -Diego Tapia .

Transcript of Prueba regresion lineal

Page 1: Prueba regresion lineal

Fecha de Entrega: 01 de diciembre de 2011.

Integrantes: -Robert Calbul Yevilao.

-Camilo Friz Gómez.

-Jonathan Lincoqueo.

-Diego Tapia .

Page 2: Prueba regresion lineal

Actividades de Aplicación:

Objetivos: Desarrollar una aplicación basada en la arquitectura Cliente-Servidor

de tres niveles (MVC: Modelo-Vista-Controlador). Debe presentar dos alternativas

de solución al problema de ayudar a ajustar una curva, usando regresión lineal,

para lo cual, la aplicación debe permitir al usuario ingresar los puntos (x,y) en una

base de datos MySql.

Paso 1: Ejemplos Entrada/Salida:

ENTRADA > [PROGRAMA] > Salida

12.5,1.3 > [PROGRAMA] > Éxito al ingresar. 12,10 > [PROGRAMA] > Éxito al ingresar.

1.0,1.5 > [PROGRAMA] > Éxito al ingresar. Abc,1.5 A > [PROGRAMA] > Error en el ingreso de datos. ABC,d-c > [PROGRAMA] > Error en el ingreso de datos. 1-5,1-2 > [PROGRAMA] > Error en el ingreso de datos. @1,# > [PROGRAMA] > Error en el ingreso de datos.

12.1,10.1 > [PROGRAMA] > Éxito al ingresar. 10 > [PROGRAMA] > Error en el ingreso de datos. 1.2 > [PROGRAMA] > Error en el ingreso de datos.

> [PROGRAMA] > Error en el ingreso de datos. 2,45,67 > [PROGRAMA] > Error en el ingreso de datos.

0012,0010 > [PROGRAMA] > Éxito al ingresar. 1 2,1,2 > [PROGRAMA] > Error en el ingreso de datos.

Tres,cuatro > [PROGRAMA] > Error en el ingreso de datos.

Page 3: Prueba regresion lineal

Diagramas de Clases:

Page 4: Prueba regresion lineal

Puebla en swing.

Diagrama Clases

Page 5: Prueba regresion lineal

Prueba en consola

Page 6: Prueba regresion lineal

Diagrama flujos programa swing

Diagrama de flujos- programa consola

Page 7: Prueba regresion lineal

Codigo Fuente Programa swing:

Page 8: Prueba regresion lineal

com.mysql.jdbc.PreparedStatement; import

conexion.conexion; import

java.awt.Color; import

java.sql.SQLException; import

java.util.logging.Level; import

java.util.logging.Logger; import

javax.swing.*; import

java.awt.event.*; import

java.sql.ResultSet; import

java.util.*; import

javax.swing.table.DefaultTableModel; import

Prueba21 ActionListener{ public class implements

String tabla;

conexion con;

JFrame ventana= JFrame( new ); "Regresion lineal"

javax.swing.JScrollPane jScrollPane1; private

javax.swing.JTable jTable1; private

DefaultTableModel modelo;

List <x_e_y> lista= ArrayList <x_e_y>(); new

JScrollPane j = JScrollPane(); new

JPanel p1= JPanel(); new

Page 9: Prueba regresion lineal

JPanel p2= JPanel(); new

JPanel p3= JPanel(); new

contador=0; int

String funcion;

JButton consulta; private

JLabel jLabel2; private

JLabel jLabel3; private

JLabel jLabel4; private

JTextField texto_a; private

JTextField texto_b; private

JTextField texto_r; private

JPanel panel1(JPanel jPanel1){ public

jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel4 = JLabel(); new

texto_r = JTextField(); new

texto_b = JTextField(); new

jLabel3 = JLabel(); new

jLabel2 = JLabel(); new

texto_a = JTextField(); new

consulta = JButton(); new

jPanel1.setLayout( ; null)

Page 10: Prueba regresion lineal

jLabel4.setText( ); "R²:"

consulta.setText( ); "consultar datos"

jLabel3.setText( ); "b: "

jLabel2.setText( ); "a: "

jPanel1.add(consulta);

jPanel1.add(jLabel4);

jPanel1.add(texto_r);

jPanel1.add(texto_b);

jPanel1.add(jLabel3);

jPanel1.add(jLabel2);

jPanel1.add(texto_a);

consulta.addActionListener( ); this

jPanel1.setBackground(Color.white);

jPanel1.setBounds(380, 20, 300, 140);

texto_a.setBounds(50, 80, 230, 20);

texto_r.setBounds(50, 110, 230, 20);

consulta.setBounds(20, 10, 130, 23);

jLabel3.setBounds(20, 80, 47, 20);

texto_b.setBounds(50, 50, 230, 20);

jLabel2.setBounds(20, 50, 47, 14);

jLabel4.setBounds(20, 110, 190, 14);

jPanel1; return

}

JButton eliminar_fila; private

Page 11: Prueba regresion lineal

JButton eliminar_fila_n; private

JTextField fila_ingreso; private

JLabel jLabel10; private

JLabel jLabel11; private

JLabel jLabel9; private

JPanel panel2(JPanel jPanel2){ public

jPanel2 = JPanel(); new

jLabel9 = JLabel(); new

eliminar_fila = JButton(); new

eliminar_fila_n = JButton(); new

fila_ingreso = JTextField(); new

jLabel10 = JLabel(); new

jLabel11 = JLabel(); new

jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jPanel2.setLayout( ); null

jLabel9.setText( ); "Eliminar fila anterior"

eliminar_fila.setText( ); "OK"

eliminar_fila_n.setText( ); "OK"

jLabel10.setText( ); "ingrese n° de fila"

jLabel11.setText( ); "Eliminar fila "

jPanel2.add(jLabel9);

Page 12: Prueba regresion lineal

jPanel2.add(eliminar_fila);

jPanel2.add(eliminar_fila_n);

jPanel2.add(jLabel11);

jPanel2.add(jLabel10);

jPanel2.setLayout( ); null

jPanel2.add(fila_ingreso);

eliminar_fila.addActionListener( ); this

eliminar_fila_n .addActionListener( ); this

jPanel2.setBackground(Color.white);

jPanel2.setBounds(380, 170, 300, 130);

jLabel11.setBounds(20, 20, 100, 14);

jLabel10.setBounds(20, 40, 110, 20);

fila_ingreso.setBounds(130, 40, 50, 20);

eliminar_fila_n.setBounds(200, 40, 80, 20);

eliminar_fila.setBounds(80, 100, 130, 23);

jLabel9.setBounds(50, 80, 180, 14);

jPanel2; return

}

JButton guardar_salir; private

JLabel jLabel1; private

JLabel jLabel8; private

JPanel jPanel3; private

JPanel panel3(JPanel jPanel3,String e[],Object[][] matriz){ public

Page 13: Prueba regresion lineal

jScrollPane1 = JScrollPane(); new

jTable1 = JTable(); new

jTable1.setModel( DefaultTableModel(matriz,e)); new

modelo=(DefaultTableModel) jTable1.getModel();

jScrollPane1.setViewportView(jTable1);

guardar_salir = JButton(); new

jLabel1 = JLabel(); new

jLabel8 = JLabel(); new

jPanel3.setLayout( ); null

guardar_salir.setText( ); "guardar y salir"

jLabel1.setText( ); "tabla de datos"

jLabel8.setText( "jonathan Lincoqueo - Camilo Friz - Robert Calbul - Diego ); Tapia"

jPanel3.add(guardar_salir);

jPanel3.add(jLabel1);

jPanel3.add(jLabel8);

jPanel3.add(jScrollPane1);

guardar_salir.addActionListener( ); this

jTable1.addMouseListener( MouseAdapter() new

{

mouseClicked(MouseEvent e) public void

Page 14: Prueba regresion lineal

{

fila = jTable1.rowAtPoint(e.getPoint()); int

fila_ingreso.setText( +fila); ""

}

});

jPanel3.setBackground(Color.white);

jPanel3.setBounds(0, 0, 690, 450);

jLabel1.setBounds(21, 5, 144, 19);

jScrollPane1.setBounds(10, 32, 340, 390);

jLabel8.setBounds(60, 430, 620, 14);

guardar_salir.setBounds(420, 330, 230, 70);

jPanel3; return

}

Prueba21(String tabla,String funcion) SQLException{ throws

.funcion=funcion; this

.tabla=tabla; this

Object[][] filas= {{ , }}; null null

String enunciados[]={ , }; " x " " y "

panel3(p1,enunciados,filas);

p1.add(panel1(p2));

p1.add(panel2(p3));

(funcion.equals( if )){ "select "

llenar_tabla();

}

ventana.setLayout( ); null

Page 15: Prueba regresion lineal

ventana.add(p1);

ventana.setSize(700,500);

ventana.setVisible( ); true

jTable1.addKeyListener( KeyAdapter() { new

@Override

keyPressed(KeyEvent evt) { public void

(modelo.getValueAt(contador,0)!= && if null modelo.getValueAt(contador,1)!= ){ null

modelo.addRow( Object[0]); new

contador++;

}

}

});

}

llenar_tabla() SQLException{ public void throws

con= conexion(); new

PreparedStatement select= (PreparedStatement) con.getConexionMysql().prepareStatement( ""

+ +"select * from " .tabla); this

ResultSet rs = select.executeQuery();

(rs.next()) { while

Page 16: Prueba regresion lineal

System.out.println(rs.getDouble(2)+ +rs.getDouble(3)); " "

modelo.addRow( Object[0]); new

modelo.setValueAt(rs.getDouble(2)+ ,contador,0); ""

modelo.setValueAt(rs.getDouble(3)+ ,contador,1); ""

contador++;

}

modelo.removeRow(modelo.getRowCount()-1);

contador--;

con.closeConexionMysql();

}

insertar() throws SQLException{ public void

con= conexion(); new

PreparedStatement delete= (PreparedStatement) con.getConexionMysql().prepareStatement( ""

+ +"delete from " .tabla+this ); ";"

delete.executeUpdate();

PreparedStatement insert= (PreparedStatement) con.getConexionMysql().prepareStatement( ""

+ +"INSERT INTO " .tabla+ this ); "(x,y)values(?,?);"

(Iterator <x_e_y>datos= lista.iterator(); datos.hasNext();){ for

x_e_y e= datos.next();

insert.setDouble(1,e.getX());

insert.setDouble(2,e.getY());

insert.executeUpdate();

}

}

generar_listas(){ public void

Page 17: Prueba regresion lineal

{ try

( filas=0;filas<=contador;filas++){ for int

datos_x=Double.parseDouble(modelo.getValueAt(filas,0)+ double ); ""

datos_y=Double.parseDouble(modelo.getValueAt(filas,1)+double ); ""

lista.add(new x_e_y(filas,datos_x,datos_y));

System.out.println( ); "raro muy raro"

}

} (NumberFormatException e){ catch

JOptionPane.showMessageDialog(null,"ingrese solo numeros y/o valide los

JOptionPane.ERROR_MESSAGE); numeros\n validacion == enter","Error",}

}

}

actionPerformed(ActionEvent e) { public void

Object fuente = e.getSource();

(fuente==eliminar_fila && modelo.getRowCount()!=0){ if

modelo.removeRow(modelo.getRowCount()-1);

contador--;

}

(fuente==eliminar_fila_n && modelo.getRowCount()!=0){ if

modelo.removeRow(Integer.parseInt(fila_ingreso.getText()));

fila_ingreso.setText ); ( ""

contador--;

}

(fuente==guardar_salir) { if

Page 18: Prueba regresion lineal

contador--;

lista. (); clear

generar_listas();

{ try

insertar();

} (SQLException ex) { catch

Logger.getLogger(Prueba21.class.getName()).log(Level.SEVERE, , nullex); }

}

(fuente==consulta){ if

lista.clear();

generar_listas();

(!funcion.equals( if )) { "select"

contador --;

}

calculo();

}

}

calculo(){ public void

promedio_x=0,promedio_y=0,suma_x=0,suma_y=0,suma_x_e_y=0; double

contador++;

double x_menos_promedio_x[]=new double[contador];

double y_menos_promedio_y[]=new double[contador];

Page 19: Prueba regresion lineal

(Iterator <x_e_y>datos= lista.iterator(); datos.hasNext();){ for

x_e_y e= datos.next();

System.out.println( +e.getX()); "x"

suma_x+=e.getX();

suma_y+=e.getY();

}

promedio_x=suma_x/contador;

promedio_y=suma_y/contador;

i=0; int

(Iterator <x_e_y>datos= lista.iterator(); datos.hasNext();){ for

x_e_y e= datos.next();

suma_x_e_y+=(e.getX()-promedio_x)*(e.getY()-promedio_y);

x_menos_promedio_x[i]=(e.getX()-promedio_x);

y_menos_promedio_y[i]=(e.getY()-promedio_y);

i++;

}

double suma_x_cuadrado=0,suma_y_cuadrado=0;

( jj=0;jj<contador;jj++){ for int

suma_x_cuadrado+=((x_menos_promedio_x[jj])*(x_menos_promedio_x[jj]));

suma_y_cuadrado+= y_menos_promedio_y[jj]* y_menos_promedio_y[jj];

}

Page 20: Prueba regresion lineal

resultado_b=(suma_x_e_y/suma_x_cuadrado); double

double resultado_r=((suma_x_e_y*suma_x_e_y)/(suma_x_cuadrado*suma_y_cuadrado));

resultado_a=(promedio_y-(resultado_b*promedio_x)); double

(suma_x_e_y==0 ||suma_x_cuadrado==0){ if

resultado_a=promedio_y;

resultado_r=1;

}

texto_b.setText( +resultado_b); ""

texto_a.setText( +resultado_a); ""

texto_r.setText( +resultado_r); ""

}

}

Page 21: Prueba regresion lineal

FUENTE PROGRAMA CONSOLA:

Page 22: Prueba regresion lineal
Page 23: Prueba regresion lineal
Page 24: Prueba regresion lineal

MODELADO BASE DE DATOS:

Page 25: Prueba regresion lineal

CAPTURAS:

Programa en swing

Page 26: Prueba regresion lineal

Programa consola