Herramientas Informaticas

Mes: enero 2012

Arreglos en JAVA ejemplos

Algunos ejemplos de arreglos en JAVA
Archivo Arreglos.java
   1: package unidad4;

   2: 

   3:  

   4: public class Arreglos{ 

   5: int []cal=new int [10]; 

   6: int []rep =new int [10];

   7: int []ren=new int [10];

   8:  

   9: //FUNCION PARA LLENAR EL ARREGLO

  10: void llenarArreglo()

  11: {

  12: for(int x=0; x

  13: cal[x]=(int) (Math.random()*(90)+10);

  14: }

  15:  

  16: //FUNCION PARA REDONDEAR EL ARREGLO

  17: void redondear()

  18: {

  19: for(int x=0; x

  20: {

  21: double a;

  22: a=cal[x]/10.0f;

  23: ren[x]=(int) Math.round(a)*10;

  24: }

  25: }

  26:  

  27: /* NO RECUERDO QUE HACE ESTO CREO QUE SACA LA FRECUENCIA DE LA REPETICION DE UN NUMERO */

  28: void frecuencia()

  29: {

  30: for(int i=0; i

  31: {

  32: ++rep[(cal[i])/10-1];

  33: }

  34: }

  35:  

  36:  

  37: //MUESTRA LAS FRECUENCIAS EN LA PANTALLA

  38: void despliegaFrecuencias()

  39: {

  40: for(int i=0; i

  41: {

  42: System.out.println((i+1)*10 +" Frecuencia "+rep[i]);

  43: }

  44: }

  45:  

  46: /* MUESTRA LOS NUMEROS DEL ARREGLO REDONDEADO */

  47: void muestraRedondeados()

  48: {

  49: for(int x=0; x

  50: {

  51: System.out.println(ren[x]);

  52: }

  53: }

  54:  

  55: //MUESTRA EL ARREGLO

  56:  

  57: void muestrag()

  58: {

  59: for(int x=0; x

  60: {

  61: System.out.println(cal[x]);

  62: }

  63: }

  64:  

  65:  

  66: // REGRESA LA MEDIE DE TODO EL ARREGLO

  67: public float media()

  68: {

  69: float m=0f;

  70: for(int x=0; x

  71: {

  72: m=m+ren[x];

  73: }

  74: m=(m/ren.length);

  75: return m;

  76: }

  77:  

  78: //REGRESA EL NUMERO MAXIMO DEL ARREGLO

  79: public int maximo()

  80: {

  81: int maximo=0;

  82: for(int x=0; x

  83: {

  84: if(ren[x]>maximo)

  85: {

  86: maximo=ren[x];

  87: }

  88:  

  89:  

  90: }

  91: return maximo;

  92: }

  93:  

  94: //REGRESA EL NUMERO MINIMO DEL ARREGLO

  95: public int minimo()

  96: {

  97: int minimo=100;

  98: for(int x=0; x

  99: {

 100: if(ren[x]

 101: {

 102: minimo=ren[x];

 103: }

 104:  

 105:  

 106: }

 107: return minimo;

 108: }

 109:  

 110:  

 111:  

 112: public static void main(String[] args) 

 113: {

 114: Arreglos obj = new Arreglos();

 115: obj.llenarArreglo();

 116: obj.redondear();

 117: obj.frecuencia();

 118: obj.despliegaFrecuencias();

 119: obj.muestrag();

 120: System.out.println("REDONDEADOS");

 121: obj.muestraRedondeados();

 122: System.out.println("La media es "+obj.media());

 123: System.out.println("El numero maximo es "+obj.maximo());

 124: System.out.println("EL numero minimo es "+obj.minimo());

 125: }

 126:  

 127: }

 128:  

 129:  

 130:  

 131:  

 132: Archivo ArregloTest.java

 133:  

 134: package unidad4;

 135:  

 136: public class ArregloTest {

 137:  

 138:  

 139:  

 140: public static void main(String[] args) {

 141:  

 142:  

 143:  

 144: Arreglo ar1=new Arreglo();

 145:  

 146: Arreglo ar2=new Arreglo();

 147:  

 148: Arreglo ar3=new Arreglo();

 149:  

 150:  

 151:  

 152: ar1.llenarArreglo();

 153:  

 154: ar1.muestraA();

 155:  

 156: ar2.llenarArreglo();

 157:  

 158: ar2.muestraA();

 159:  

 160: ar3=ar2.sumar(ar1);

 161:  

 162: ar3.muestraA();

 163:  

 164: }

 165:  

 166: }

 167:  


 

El codigo se pudo distorcionar por el formato, les dejo los archivos para descargar

Examen de java Juego Adivinar

Examen Juego Adivinar

Luego de recuperar mis apuntes encontré un examen practico de programación en JAVA, es un juego que se llama adivinar, no recuerdo que hace exactamente al parecer tienes que adivinar un numero y cada vez te acercas mas con las pistas que te da diciendo que es menor o mayor allí les dejo el código

   1:  

   2:  

   3: package examen;

   4:  

   5:  

   6:  

   7:  

   8: import javax.swing.JOptionPane;

   9: public class Adivinar 

  10: {

  11: public String nombre;

  12: public int intentos;

  13: public int puntuacion;

  14: public double numero;

  15: public int aleatorio1;

  16: public int partidas;

  17: public int ni;

  18: public int mp;

  19: /** Funcion que genera un numero aleatorio de 1 al 100 */

  20: public static void aleatorio(int numero)

  21: {

  22: numero=(int) (Math.random()*(100)+1);

  23: }

  24: /** Inserta el nombre */

  25: public void insertaNombre()

  26: {

  27: nombre=JOptionPane.showInputDialog("Inserte el nombre");

  28: }

  29: /**Jugar*/

  30: public void jugar()

  31: {

  32: puntuacion=1000;

  33: intentos++;

  34: puntuacion--;

  35: ni=Integer.parseInt(JOptionPane.showInputDialog("Inserte el numero"));

  36: if(ni!=aleatorio1)

  37: {

  38: if(ni>aleatorio1)

  39: {

  40: JOptionPane.showMessageDialog(null,"El numero correcto es menor");

  41: }

  42: if(ni

  43:  

  44: {

  45: JOptionPane.showMessageDialog(null,"El numero correcto es mayor");

  46: }

  47: }

  48: }

  49: /**Puntuacion */

  50: public void puntuacion()

  51: {

  52: JOptionPane.showMessageDialog(null,"Nombre "+nombre+"n puntuacion "+puntuacion+"n Mejor puntuacion "+mp);

  53: }

  54: /**Mejor puntuacion */

  55: public void mejorPuntuacion()

  56: {

  57: if(puntuacion>mp)

  58: {

  59: mp=puntuacion;

  60: }

  61: }

  62: public String toString()

  63: {

  64: return "Mejor puntuacion n Nombre "+nombre+"n Mejor puntuacion "+mp;

  65: }

  66: }

Puede que por el formato se modifique el código, aquí los archivos .java




Manejo de excepciones en JAVA

Manejo de excepciones JAVA

package unidad5;

public class ManejoDeExepciones {


public static void main(String[] args) {
int num1;
int num2;
/*
try
{
arg1=args[0];
arg2=args[1];


}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Argumentos incompletos");
}
try {
num1=Integer.parseInt(arg1);
num2=Integer.parseInt(arg2);
}catch(NumberFormatException e1){
System.out.println("No se ha recibido un valor numerico");
}
System.out.println(arg1+" "+arg2);
*/
try
{
num1=Integer.parseInt(args[0]);
num2=Integer.parseInt(args[1]);

System.out.println(num1/num2);
System.out.println(Potencia.potencia(5, 5));


/* }catch(ArrayIndexOutOfBoundsException e){
System.out.println("Argumentos incompletos");
}
catch(NumberFormatException e1){
System.out.println("No se ha recibido un valor numerico");
}catch(ArithmeticException e){
System.out.println("La divicion entre cero es imposible");
}

catch(Exception e){
System.out.println("Ocurrio un error");
}
finally{
System.out.println("Siempre me ejecuto");
}
}

}



SImple serpientes y escalera en JAVA

Ejercicio simple de serpientes y escaleras hecho en java

Archivo Serpientes.java

   1: package serpientesYescaleras;

   2:  

   3: import static utilerias.Mate.factorial;

   4:  

   5: import javax.swing.JOptionPane;

   6:  

   7: public class Serpientes {

   8:  

   9: private int []tablero={0,0,0,0,0,0,0,0,0,0,+28,0,0,0,0,0,+49,0,+27,-15,

  10: +36,0,-21,0,0,+24,0,0,0,0,0,0,0,0,-13,0,0,0,0,0,0,0,+41,0,0,

  11: 0,0,0,0,0,0,+24,0,0,0,0,0,0,0,0,0,-48,0,0,0,0,0,+24,0,0,0,0,-66,+26

  12: ,-45,0,0,-29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-54,0,0,+28,0,0,0};

  13:  

  14: private String nombre;

  15: private int edad;

  16: private int casilla=0;

  17:  

  18: private static int puntuacion=0;

  19: private static String ganador;

  20:  

  21:  

  22: public int dados()

  23: {

  24: return (int)(Math.random()*(6)+1);

  25: }

  26:  

  27: public void insertarDatos()

  28: {

  29: nombre=JOptionPane.showInputDialog("Dame tu nombre");

  30: edad=Integer.parseInt(JOptionPane.showInputDialog("Dame tu edad"));

  31: }

  32:  

  33: public void avanzar()

  34: {

  35: int d;

  36: JOptionPane.showMessageDialog(null,nombre+" De clic en aceptar para tirar el dado");

  37: d=dados();

  38: JOptionPane.showMessageDialog(null,nombre+" Ha caido "+d);

  39: casilla=casilla+d;

  40:  

  41:  

  42: if(tablero[casilla]>0)

  43: {

  44: casilla=casilla+tablero[casilla];

  45: JOptionPane.showMessageDialog(null,"Bien "+nombre+" has escalado hasta escalado hasta la casilla "+casilla);

  46: }

  47: if(tablero[casilla]<0)

  48: {

  49: casilla=casilla+tablero[casilla];

  50: JOptionPane.showMessageDialog(null,"Huu "+nombre+" Has retrocedido hasta la casilla "+casilla);

  51: }

  52: JOptionPane.showMessageDialog(null,nombre+" esta en la casilla "+casilla);

  53: if(casilla>100)

  54: {

  55:  

  56: JOptionPane.showMessageDialog(null,nombre+" felicidades has ganado "+casilla);

  57:  

  58: }

  59: setPuntuacion(casilla);

  60: }

  61:  

  62: public static void setPuntuacion(int puntuacion) {

  63: Serpientes.puntuacion = puntuacion;

  64: }

  65:  

  66: public static int getPuntuacion() {

  67: return puntuacion;

  68: }

  69:  

  70: }

  71:  

  72: Archivo SerpientesTest.java

  73:  

  74: package serpientesYescaleras;

  75: import javax.swing.JOptionPane;

  76: public class SerpientesTest {

  77:  

  78:  public static void main(String[] args) {

  79:   Serpientes jugador1=new Serpientes();

  80:   Serpientes jugador2=new Serpientes();

  81:  

  82:   jugador1.insertarDatos();

  83:   jugador2.insertarDatos();

  84:  

  85:   do

  86:    {

  87:    jugador1.avanzar();

  88:    jugador2.avanzar();

  89:  

  90:    }while(Serpientes.getPuntuacion()<100);

  91:   JOptionPane.showMessageDialog(null," Fin del juego ");

  92:  }

  93: }

  94:  

 

Conexion A mysql con Visual C# Express

Otro proyecto



Viene con el codigo fuente incluido.

EL programa no tiene ni un objetivo si no mostrar una manera de como conectar un programa hecho en Visual express C# con Mysql
Funciona en Windows XP en 7 me fallo 😀



CONEXION C# EXPRESS CON MYSQL

LES DEJO EL PROGRAMA DE FIN DE SEMESTRE

CONSISTE EN CONECTAR UN PROGRAMA HECHO EN VISUAL EXPRESS C# CON MYSQL

EL PROYECTO TIENE LA INSTALACION AUTOMATICA DE LA BASE DE DATOS
TAMBIEN CONTIENE EL CONECTOR MYSQL.DATA 5.1 QUE SE ENCUENTRA EN LA CARPETA DONDE ESTA EL CODIGO FUENTE
A TAMBIEN INCLUI EL CODIGO FUENTE QUE ES LO MAS IMPORTANTE ALLI EN CIERTA CLASE PUSE TODAS LAS SECUENCIAS SQL PARA INSTANCIARLAS EN EL PROGRAMA DESPUES PARA NO ESCRIBIR TANTO CODIGO
EL PROGRAMA FUNCIONA CORRECTAMENTE EN WINDOWS XP SP3
Cualquier mal funcionamiento porfavor comentelo 😀
ESPERO Y LES SIRVA EL CODIGO

Creado con WordPress & Tema de Anders Norén