viernes, 29 de octubre de 2010

MÉTODO DE BURBUJA MEJORADA EN JAVA

/* Metodo Burbuja Mejorada
 * Crear un programa en java que ordene datos mediante el metodo de la burbuja mejorada.
 * Fuente:   .....------             ErNeStO rOsAlEs CaRlOs              ------.....

                                      *********************************************
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  EsTrUcTuRa De DaToS                      *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/

import java.io.*;

public class BurbujaOrdenaMejorada
{

public static void main (String args[])throws IOException
{

int d,t,k;

BufferedReader erc=new BufferedReader(new InputStreamReader(System.in));

System.out.println("                  *****************************");
System.out.println("                  *                           *");
System.out.println("                  *     MeToDo De BuRbUjA     *");
System.out.println("                  *                           *");
System.out.println("                  *****************************");

System.out.println("\nIngresa el numero de datos a ingresar: ");
d=Integer.parseInt(erc.readLine());

int n[]=new int[d];

for(int i=0; i<d; i++)
{
System.out.println("Ingresa dato " +i+" :");
n[i]=Integer.parseInt(erc.readLine());
}
int tuopcion;
do
{
do
{
try
{

System.out.println("\n");
System.out.println("             **************************************");
System.out.println("             *    Elige la opcion deseada:        *");
System.out.println("             *                                    *");
System.out.println("             *     1.- Sin orden(original)        *");
System.out.println("             *     2.- Orden decendiente          *");
System.out.println("             *     3.- Orden ascendente           *");
System.out.println("             *                                    *");
System.out.println("             *     4.- Salir                      *");
System.out.println("             *                                    *");
System.out.println("             **************************************");

      
tuopcion=Integer.parseInt(erc.readLine());

    if (tuopcion< 1 || tuopcion> 4)
        System.out.println ("\nError **fuera del rango permitido**");
      }
      catch (NumberFormatException err)
      {
        tuopcion=0;
                 System.out.println("\n\n Error --no es numero--");
      }
    
      }while (tuopcion<1 || tuopcion>4);
    
    

switch(tuopcion)
{

case 1:

System.out.println("\nEl arreglo original es: ");
for(int i=0;i<n.length;i++)
{
System.out.print(" "+ n[i]);
}

break;


case 2:

boolean inter=true;

for(int j=0;j<n.length-1&&inter==true;j++)
{
inter=false;

for(int i=1;i<n.length-j;i++)
if(n[i-1]<n[i])
{
inter=true;
t=n[i-1];
n[i-1]=n[i];
n[i]=t;
}
         }
System.out.println("\n\nLos datos en orden decreciente son: ");
for(int r=0;r<n.length;r++)
System.out.print(" "+n[r]);

break;

case 3:

boolean inte=true;

for(int l=0;l<n.length-1&&inte==true;l++)
{
inte=false;

for(int i=1;i<n.length-l;i++)
if(n[i-1]>n[i])
{
inte=true;
t=n[i-1];
n[i-1]=n[i];
n[i]=t;
}
         }
System.out.println("\n\nLos datos en orden decreciente son: ");
for(int r=0;r<n.length;r++)
System.out.print(" "+n[r]);
break;

}

} while(tuopcion!=4);

}

}

jueves, 28 de octubre de 2010

MAYOR DE TRES NUMEROS SWING (JAVA)

/* MaYoR De TrEs NuMeRoS cOn SwInG (Interfaz Grafica de Usuario)(Actividad 5.3)
 * Crear un programa en java con interfaz grafica de ususario en Swing que pida 3 numeros e indique cual es el mayor.
 * Fuente:   .....------                     ErNeStO rOsAlEs CaRlOs                 ------.....

                                      *********************************************
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  ToPiCoS sElEcToS dE pRoGrAmAcIoN         *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class MayorTresNumerosSwing extends JFrame
{
private Container o;
private GridBagLayout e;
private GridBagConstraints r;
private JLabel l1,l2,l3,l4;
private JTextField t1,t2,t3,t4;
private JButton b1,b2,b3,b4;
private Double n1,n2,n3,nt;
private String ntl;

public MayorTresNumerosSwing()
{
setSize(450,300);
setTitle("Mayor y Menor de Tres Numeros");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

o= getContentPane();
e=new GridBagLayout();
o.setLayout(e);

r=new GridBagConstraints();

l1=new JLabel("Numero 1: ");
PosicionValores(0,0,1,1,0.0);
e.setConstraints(l1,r);
o.add(l1);

t1=new JTextField("",10);
PosicionValores(1,0,1,1,0.0);
e.setConstraints(t1,r);
o.add(t1);

l2=new JLabel("Numero 2: ");
PosicionValores(0,1,1,1,0.0);
e.setConstraints(l2,r);
o.add(l2);

t2=new JTextField("",10);
PosicionValores(1,1,1,1,0.0);
e.setConstraints(t2,r);
o.add(t2);

l3=new JLabel("Numero 3: ");
PosicionValores(0,2,1,1,0.0);
e.setConstraints(l3,r);
o.add(l3);

t3=new JTextField("",10);
PosicionValores(1,2,1,1,0.0);
e.setConstraints(t3,r);
o.add(t3);


 b1=new JButton("NuMeRo MaYoR");
PosicionValores(1,4,1,1,0.0);
e.setConstraints(b1,r);
o.add(b1);


l4=new JLabel("Resultado: ");
PosicionValores(0,5,1,1,0.0);
e.setConstraints(l4,r);
o.add(l4);

t4=new JTextField("",10);
PosicionValores(1,5,1,1,0.01);
e.setConstraints(t4,r);
o.add(t4);                      

ManejadorCampos manejador=new ManejadorCampos();
b1.addActionListener(manejador);
b2.addActionListener(manejador);

        }
      
         public void PosicionValores(int co,int fi, int an, int al,double wy)
         {
         r.gridx=co;
         r.gridy=fi;
         r.gridwidth=an;
         r.gridheight=al;
         r.weighty=wy;
        
         }
        
public static void main(String args[])

{
new MayorTresNumerosSwing();
}

private class ManejadorCampos implements ActionListener
{
public void actionPerformed(ActionEvent evento)
{
if (evento.getSource()==b1)
{  
n1=Double.valueOf(t1.getText());
n2=Double.valueOf(t2.getText());
n3=Double.valueOf(t3.getText());

if(n1>n2&&n1>n3)
{
ntl=String.valueOf(n1);
t4.setText(ntl);
}

else if(n2>n1&&n2>n3)
{
ntl=String.valueOf(n2);
t4.setText(ntl);
}

else if(n3>n1&&n3>n2)
{
ntl=String.valueOf(n3);
t4.setText(ntl);
}
}


}
}

}

TIPOS DE CUADRO DE DIALOGO SWING (JAVA)

/* TiPoS dE cUaDrOs DiALoGo sWiNg (Actividad 5.1)
 * Investigar cuantos tipos de cuadros de dialogos de mensajaje hay en swing de java (ejemplo showMessageDialog)
 * y crear un programa que al presionar un boton muestre un cuadro de dialogo con su informacion personal, 
 * deberan hacer un boton diferente para cada uno de los cuadros de dialogo existentes.
 * Fuente:   .....------                     ErNeStO rOsAlEs CaRlOs               ------.....

                                      *********************************************
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  ToPiCoS sElEcToS dE pRoGrAmAcIoN         *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

public class TiposCuadroDialogoSwing extends JFrame
{
private Container c;
private GridBagLayout e;
private GridBagConstraints r;
private JButton b1,b2,b3,b4,b5,b6,b7;
private String name, age, address;
private JLabel l1;
private JFrame frame;
public TiposCuadroDialogoSwing()
{
setSize(250,300);
setTitle("Tipos Cuadro Dialogo de Swing");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
this.frame=frame;
c=getContentPane();
e=new GridBagLayout();
c.setLayout(e);
r=new GridBagConstraints();
r.fill=GridBagConstraints.BOTH;
 l1=new JLabel("I'lL sHoW yOU dIaLoG bOxEs");
 establecerValores(0,0,1,1,0.2);
 e.setConstraints(l1,r);
 c.add(l1);
 b1=new JButton("Message");
 establecerValores(0,1,1,1,0.0);
 e.setConstraints(b1,r);
 c.add(b1);
 
 b2=new JButton("Information");
 establecerValores(0,2,1,1,0.0);
 e.setConstraints(b2,r);
 c.add(b2);
 b3=new JButton("Warning");                     
 establecerValores(0,3,1,1,0.0);
 e.setConstraints(b3,r);
 c.add(b3);
 
 b4=new JButton("Question");                    
 establecerValores(0,4,1,1,0.0);
 e.setConstraints(b4,r);
 c.add(b4);
 b5=new JButton("Plain");                        
 establecerValores(0,5,1,1,0.0);
 e.setConstraints(b5,r);
 c.add(b5);
 
 b6=new JButton("Error");                       
 establecerValores(0,6,1,1,0.0);
 e.setConstraints(b6,r);
 c.add(b6);
 
 b7=new JButton("Option");
 establecerValores(0,7,1,1,0.0);
 e.setConstraints(b7,r);
 c.add(b7);  
 
 
 ManejadorCamposTexto mn=new ManejadorCamposTexto();
 b1.addActionListener(mn);
 b2.addActionListener(mn);
 b3.addActionListener(mn);
 b4.addActionListener(mn);
 b5.addActionListener(mn);  
 b6.addActionListener(mn);
 b7.addActionListener(mn);
 
   
}
public void establecerValores(int co,int fi, int an, int al,double wy)
{
r.gridx=co;
r.gridy=fi;
r.gridwidth=an;
r.gridheight=al;
r.weighty=wy;
}
public static void main(String args[])
{
new TiposCuadroDialogoSwing();
}

private class ManejadorCamposTexto implements ActionListener
{
public void actionPerformed(ActionEvent et)
{
if(et.getSource()==b1)
{
JOptionPane.showMessageDialog(null,"Name: Ernesto Rosales \n Age: 21 \n Address: C. Rosales s/n");
}
if(et.getSource()==b2)
{
JOptionPane.showMessageDialog(frame,
     "Name: Ernesto Rosales \n Age: 21 \n Address: C. Rosales s/n",
     "Ernesto's Custom Dialog",
     JOptionPane.INFORMATION_MESSAGE);
}
if(et.getSource()==b3)
{
JOptionPane.showMessageDialog(frame,
     "Name: Ernesto Rosales Carlos \n Age: 21 \n C. Rosales s/n",
     "Ernesto's Warning",
     JOptionPane.WARNING_MESSAGE);
}
if(et.getSource()==b4)
{
name=JOptionPane.showInputDialog("What's your name: ");
age=JOptionPane.showInputDialog("What's your age: ");
address=JOptionPane.showInputDialog("What's your address: ");
JOptionPane.showMessageDialog(null,"Your name is: "+name+ "\n Your age is: "+age+" \n Your address is: "+address);
}
if(et.getSource()==b5)
{
JOptionPane.showMessageDialog(frame,
     "Name: Ernesto Rosales Carlos \n Age: 21 \n C. Rosales s/n",
     "Ernesto's Plain Message",
     JOptionPane.PLAIN_MESSAGE);
}
if(et.getSource()==b6)
{
JOptionPane.showMessageDialog(frame,
     "Name: Ernesto Rosales Carlos \n Age: 21 \n C. Rosales s/n",
     "Ernesto's Error",
     JOptionPane.ERROR_MESSAGE);
}
if(et.getSource()==b7)
{
JOptionPane.showConfirmDialog(null,
            "Name: Ernesto Rosales Carlos \n Age: 21 \n C. Rosales s/n", "Ernesto's Choose One", JOptionPane.YES_NO_OPTION);
}

}
}
}

MÉTODO DE BURBUJA EN JAVA

/* Metodo Burbuja
 * Crear un programa en java que ordene datos mediante el metodo de la burbuja.
 * Fuente:   .....------                     ErNeStO  rOsAlEs CaRlOs             ------.....

                                      *********************************************
                                      *  InGeNiErIa En SiStEmAs CoMpUtAcIoNaLeS   *
                                      *  InStItUtO tEcNoLoGiCo SuPeRiOr De JeReZ  *
                                      *  EsTrUcTuRa De DaToS                      *
                                      *  ErNeStO rOsAlEs CaRlOs                   *
                                      *  ernestorosales@live.com                  *
                                      *********************************************
*/

import java.io.*;

public class BurbujaOrdena
{
public static void main (String args[])throws IOException
{
int d,t;

BufferedReader erc=new BufferedReader(new InputStreamReader(System.in));
System.out.println("                  *****************************");
System.out.println("                  *                           *");
System.out.println("                  *     MeToDo De BuRbUjA     *");
System.out.println("                  *                           *");
System.out.println("                  *****************************");
System.out.println("\nIngresa el numero de datos a ingresar: ");
d=Integer.parseInt(erc.readLine());
int n[]=new int[d];
for(int i=0; i<d; i++)
{
System.out.println("Ingresa dato " +i+" :");
n[i]=Integer.parseInt(erc.readLine());
}
int tuopcion;
do
{
do
{
try
{
System.out.println("\n");
System.out.println("             **************************************");
System.out.println("             *    Elige la opcion deseada:        *");
System.out.println("             *                                    *");
System.out.println("             *     1.- Sin orden(original)        *");
System.out.println("             *     2.- Orden decendiente          *");
System.out.println("             *     3.- Orden ascendente           *");
System.out.println("             *                                    *");
System.out.println("             *     4.- Salir                      *");
System.out.println("             *                                    *");
System.out.println("             **************************************");

      
tuopcion=Integer.parseInt(erc.readLine());
    if (tuopcion< 1 || tuopcion> 4)
        System.out.println ("\nError **fuera del rango permitido**");
      }
      catch (NumberFormatException err)
      {
        tuopcion=0;
                 System.out.println("\n\n Error --no es numero--");
      }
      
      }while (tuopcion<1 || tuopcion>4);
      
      
switch(tuopcion)
{
case 1:
System.out.println("\nEl arreglo original es: ");
for(int i=0;i<n.length;i++)
{
System.out.print(" "+ n[i]);
}
break;
case 2:
for(int j=0;j<n.length;j++)
for(int k=1;k<n.length;k++)
if(n[k-1]<n[k])
{
t=n[k-1];
n[k-1]=n[k];
n[k]=t;
}

System.out.println("\n\nLos datos en orden decreciente son: ");
for(int i=0;i<n.length;i++)
System.out.print(" "+n[i]);
break;
case 3:
for(int j=0;j<n.length;j++)
for(int k=1;k<n.length;k++)
if(n[k-1]>n[k])
{
t=n[k-1];
n[k-1]=n[k];
n[k]=t;
}
System.out.println("\n\nLos datos en orden ascendente son: ");
for(int r=0;r<n.length;r++)
System.out.print(" "+n[r]);
break;

}
} while(tuopcion!=4);
}

}