viernes, 30 de noviembre de 2012

CONTADOR DE VOCALES EN JTEXTAREA


  • EJERCICIO

Tengo que hacer un programa, en la interfaz hay que digitar dentro de una EDIT(caja de texto) una palabra, y cuando le doy CALCULAR, que me calcule las vocales, también calcular si el total de cada vocal es par o impar

EJEMPLO si el usuario digita SHAMIRJAVAI o (shamirjavai) el resultado tiene que ser:

    SHAMIRJAVAI = 11

    A=3 PAR
    E=O PAR
    I=2 PAR
    O=0 PAR
    U=0 PAR
 
CLARO SUPONIENDO QUE EL NUMERO "0" LO CONSIDEREN NUMERO "PAR" PERO MAS ABAJO LE PONGO PARA LOS DOS CASOS, PAR O IMPAR.

  • RESULTADO



  • COD. FUENTE

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

/* 
 * tengo que hacer un programa, en la interfaz hay que digitar dentro de una 
 * EDIT(caja de texto) una palabra, y cuando le doy CALCULAR, que me calcule 
 * las vocales, tambien calcular si el total de cada vocal es par o impar
 
 * EJEMPLO si el usuario digita SHAMIRJAVAI o (shamirjavai) el resultado tiene que ser:

    SHAMIRJAVAI = 11 

    A=3 PAR
    E=O PAR
    I=2 PAR
    O=0 PAR
    U=0 PAR
    
  * CLARO SUPONIENDO QUE EL NUMERO "0" LO CONSIDEREN NUMERO "PAR"
  * PERO MAS ABAJO LE PONGO PARA LOS DOS CASOS, PAR O IMPAR
*/

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ContadorVocales extends JFrame implements ActionListener
{
    public JTextArea txtMiCaja = new JTextArea(20,30);
    public JButton btnCalcular = new JButton("CALC. VOCALES");

    public ContadorVocales()
    {
        super("CONTADOR DE VOCALES");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400,430);
        
        FlowLayout DISTRIBUIDOR = new FlowLayout(FlowLayout.CENTER,10,10);
        this.setLayout(DISTRIBUIDOR);
        
        this.btnCalcular.addActionListener(this);
        
        this.add(this.txtMiCaja);
        this.add(this.btnCalcular);
        
        this.setVisible(true);
    }    
    
    @Override
    public void actionPerformed(ActionEvent AE) 
    {
        char[] ArrayTexto = this.txtMiCaja.getText().toCharArray();
        int[] Vocales = new int[5];
        
        for(int i=0 ; i < ArrayTexto.length() ; i++)
        {
            switch(ArrayTexto[i])
            {
                case 'a': Vocales[0]++;break;
                case 'A': Vocales[0]++;break;
                case 'e': Vocales[1]++;break;
                case 'E': Vocales[1]++;break;
                case 'i': Vocales[2]++;break;
                case 'I': Vocales[2]++;break;
                case 'o': Vocales[3]++;break;
                case 'O': Vocales[3]++;break;
                case 'u': Vocales[4]++;break;  
                case 'U': Vocales[4]++;break;    
            }
        }   
        
        JOptionPane.showMessageDialog(null,"EL TEXTO ESCRITO ES : \n\n" + this.txtMiCaja.getText()
                                            + "\n\n TOTAL : " + ArrayTexto.length
                                            + "\n\n\n VOCALES : \n\n"
                                            + "\nVOCAL 'A' = " + Vocales[0] + " ES " + ParImpar(Vocales[0])
                                            + "\nVOCAL 'E' = " + Vocales[1] + " ES " + ParImpar(Vocales[1])
                                            + "\nVOCAL 'I' = " + Vocales[2] + " ES " + ParImpar(Vocales[2])
                                            + "\nVOCAL 'O' = " + Vocales[3] + " ES " + ParImpar(Vocales[3])
                                            + "\nVOCAL 'U' = " + Vocales[4] + " ES " + ParImpar(Vocales[4]));                
    }
    
    public String ParImpar(int Num)
    {
        //-- SI NO CONSIDERAS QUE EL NUMERO 0 ES "PAR" ENTONCES
        //-- CAMBIA ESTA PARTE "if(Num % 2 == 0)" POR "if(Num % 2 == 0 && Num != 0)"
        if(Num % 2 == 0)
        {           
            return "PAR";
        }
        else
        {
            return "IMPAR";
        }
    }
    
    public static void main(String[] args) 
    {
        ContadorVocales ContVocales = new ContadorVocales();
    }   
}

DALE CLICK EN LA IMAGEN PARA BAJARTE EL PROYECTO CON LOS 2 EJERCICIOS


IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI PERSONA, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

JUEGO DE DADOS CON RANDOM EN JFRAME

Este Ejercicio la verdad esta muy simple, hubiera echo con imágenes de dados, incluso simulando el lanzamiento del dado, pero la verdad no me alcanzo el tiempo, para otra sera.
  • EJERCICIO

Programa que simule el lanzamiento de dos dados. El programa deberá usar Math.ramdom para lanzar ambos dados, calculándose después la suma de los dos valores. Implementar un juego en el cual participen dos jugadores (uno podría ser la maquina) y lance los dados, de acuerdo al valor obtenido por los dados realizar su avance, a través de un tablero o escenario de juego.

  • RESULTADO



  • COD. FUENTE

/**
 *
 *  E-Mail : shamirdhc31@gmail.com
 *  Blog   : http://javadhc.blogspot.com
 *
 */

/**
 * programa que simule el lanzamiento de dos dados. 
 * El programa debera usar Math.ramdom para lanzar ambos dados, 
 * calculandose despues la suma de los dos valores. 
 * Implementar un juego en el cual participen dos jugadores 
 * (uno podria ser la maquina)y lance los dados, 
 * de acuerdo al valor obtenido por los dados realizar su avance,
 * a traves de un tablero o escenario de juego.
 */

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;


public class JuegoDados extends JFrame implements ActionListener
{
    public JTextArea txtJugador01 = new JTextArea(20,22);
    public JTextArea txtJugador02 = new JTextArea(20,22);
    public JComboBox cbxModoJuego = new JComboBox();
    public JButton btnJugar = new JButton("JUGAR"); 
    public JLabel lblJugador01 = new JLabel("          JUGADOR 01");
    public JLabel lblJugador02 = new JLabel("          JUGADOR 02");
    public JScrollPane spnJugador01 = new JScrollPane(txtJugador01);
    public JScrollPane spnJugador02 = new JScrollPane(txtJugador02);
    public int NumPartida = 0;

    public JuegoDados()
    {
        super("JUEGO DE DADOS");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600,500);
        this.setResizable(false);
        
        FlowLayout DISTRIBUIDOR = new FlowLayout(FlowLayout.CENTER,50,20);
        this.setLayout(DISTRIBUIDOR);
        
        this.btnJugar.addActionListener(this);
        
        this.txtJugador01.setEditable(false);
        this.txtJugador02.setEditable(false);
        
        this.cbxModoJuego.addItem("JUGADOR VS JUGADOR");
        this.cbxModoJuego.addItem("JUGADOR VS MAQUINA");
        this.txtJugador01.setSize(100,200);
        this.add(this.lblJugador01);
        this.add(this.lblJugador02);
        this.add(this.spnJugador01);
        this.add(this.spnJugador02);
        this.add(this.cbxModoJuego);
        this.add(this.btnJugar);        
    }

    @Override
    public void actionPerformed(ActionEvent AE) 
    {       
        int Resultado01,Resultado02;
        
        this.NumPartida++;
        if(this.cbxModoJuego.getSelectedIndex() == 0)
        {
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 01");
            Resultado01 = TirarDadoJugador(1,"JUGADOR 01");
            
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 02");
            Resultado02 = TirarDadoJugador(2,"JUGADOR 02");    
            
            if(Resultado01 > Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 01\n\n TOTAL = " + Resultado01);
            }
            else if(Resultado01 < Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 02\n\n TOTAL = " + Resultado02);
            }
            else if(Resultado01 == Resultado02)
            {
                JOptionPane.showMessageDialog(this,"EMPATES..¡¡¡\n\n TOTAL = " + Resultado01);
            }           
        }
        else
        {
            JOptionPane.showMessageDialog(this,"TURNO DE JUGADOR 01");
            Resultado01 = TirarDadoJugador(1,"JUGADOR 01");
            
            Resultado02 = TirarDadoJugador(3,"MAQUINA");    
            
            if(Resultado01 > Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO EL JUGADOR 01\n\n TOTAL = " + Resultado01);
            }
            else if(Resultado01 < Resultado02)
            {
                JOptionPane.showMessageDialog(this,"GANO LA MAQUINA\n\n TOTAL = " + Resultado02);
            }
            else if(Resultado01 == Resultado02)
            {
                JOptionPane.showMessageDialog(this,"EMPATES..¡¡¡\n\n TOTAL = " + Resultado01);
            }              
        }
    }
    
    //--Jugador01 = 1, Jugador02 = 2, Maquina = 3
    public int TirarDadoJugador(int Jugador,String NomJugador)
    {
        int Dado01,Dado02,SumaDados;        
        
        Dado01 = GenerarAleatorioDado();
        Dado02 = GenerarAleatorioDado();
        SumaDados = Dado01 + Dado02;
            
        JOptionPane.showMessageDialog(this,"RESULTADO DE TIRAR DADOS PARA " + NomJugador
                                         + "\n\nDADO 01 : " + Dado01 + "\nDADO 02 : " + Dado02
                                         + "\n\n TOTAL SUMAN " + SumaDados);
        
        switch(Jugador)
        {
            case 1: this.txtJugador01.setText(this.txtJugador01.getText() + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;
                
            case 2: this.txtJugador02.setText(this.txtJugador02.getText() + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;
                
            case 3: this.txtJugador02.setText(this.txtJugador02.getText() + "\nJUEGO " + "\nJUEGO " 
                                            + this.NumPartida + ": TOTAL " + SumaDados
                                            + " DADO 01: " + Dado01 + " DADO 02: " + Dado02);break;    
        }
        
        return SumaDados;
    }
    public int GenerarAleatorioDado()
    {
        Random Aleatorio = new Random();
        int Dado;
        
        Dado = Aleatorio.nextInt(6)+1;
        
        return Dado;
    }
    
    public static void main(String[] ARGUMENTOS)
    {
        JuegoDados Dados = new JuegoDados();
        Dados.setVisible(true);
    }
}


DALE CLICK EN LA IMAGEN PARA BAJARTE EL PROYECTO CON LOS 2 EJERCICIOS


IMPORTANTE : "TODOS LOS CODIGOS INDICADOS AQUI SON ESCRITOS POR MI PERSONA, ASI QUE CUALQUIER DUDA O EJERCICIO QUE NO PUEDAN RESOLVER, NO DUDEN EN MANDARME UN E-MAIL A MI CORREO"
shamirdhc31@gmail.com

jueves, 15 de noviembre de 2012

CONEXION ACCESS SENCILLO EN JAVA

Este es un EJEMPLO sencillo de una CONEXION JAVA - ACCESS con CONSULTA

Aqui unos Ejemplos de Consulta, para los que no estan familiarizados con SQL

SELECT * FROM cancion
* SELECCIONA TODAS LAS FILAS DE LA TABLA cancion

SELECT * FROM cancion WUERE Nombre LIKE '%Let it be%'
* CON FILTRO "Let it be" EN LA COLUMNA Nombre


SELECT * FROM cancion WUERE Autor LIKE '%Lily%'
* CON FILTRO "Lily" EN LA COLUMNA Autor

* ETC.


ATENCION LA BASE DE DATOS DE ESTE EJEMPLO SE LLAMA "musica.mdb" Y TIENE COMO UNICA TABLA "cancion" COMO EN LA IMAGEN MAS ABAJO, Y ESTE ARCHIVO DEBE ESTAR EN LA CARPETA DEL PROYECTO PARA QUE ESTE EJEMPLO FUNCIONE, Y SI TIENEN SU PROPIO BASE DE DATOS ACCESS TBN DEBE DE ESTAR EN LA CARPETA DEL PROYECTO.
  • RESULTADO