jueves, 29 de marzo de 2012

PROGRAMA DE CLASES (OPERACIONES BASICAS)

CLASE MAIN


package ejemploclase;
import javax.swing.*;

/**
 *
 * @author J y A
 */
public class Main {
   
     public static int menu() {
        int opc=-1;
        String cadMenu="Digite sus opciones:\r\n\r\n"
                + "1. Suma\r\n"
                + "2. multiplicar\r\n"
                + "3. dividir\r\n"
                + "\r\n0. SALIR\r\n \r\n";
        do {
           try {
            opc=Integer.parseInt(JOptionPane.showInputDialog(cadMenu));
            }
            catch(Exception e){
                System.out.println("No es un número, es una cadena de texto.");
                JOptionPane.showMessageDialog(null, "No es una entrada valida \r\n"+ e.getMessage());
             }

        } while ((opc<0)||(opc>3));

        return (opc);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
       /* int x, y, z;
       
        x= Integer.parseInt(JOptionPane.showInputDialog("Numero?"));
        y= Integer.parseInt(JOptionPane.showInputDialog("Numero?"));
       
        z=x+y;
        JOptionPane.showMessageDialog(null,"valor de la suma: "+ z);
       
        if (x%2==0)
            JOptionPane.showMessageDialog(null,"es par ");
        else
            JOptionPane.showMessageDialog(null,"es impar ");*/
       
        //con la clase
       
         boolean seguir=true;

        do {
           switch (menu()) {
               case 1:
                   JOptionPane.showMessageDialog(null, "sumar");
       
        Numero num;
        num= new Numero(Integer.parseInt(JOptionPane.showInputDialog("Numero?")));
       
        JOptionPane.showMessageDialog(null,"Valor de la suma: "
                + num.suma(Integer.parseInt(JOptionPane.showInputDialog("Numero?"))));
       
        if (num.esPar())
            JOptionPane.showMessageDialog(null, "Es par");
        else
            JOptionPane.showMessageDialog(null, "No es par");
       
         break;
               case 2:
                   JOptionPane.showMessageDialog(null, "multiplicar");
     
        Numero num2;
        num2= new Numero(Integer.parseInt(JOptionPane.showInputDialog("Numero?")));
         int n=num2.producto(Integer.parseInt(JOptionPane.showInputDialog("Digite el numero")));
           JOptionPane.showMessageDialog(null,"El producto es: "+n);
     
          //indica si el producto es o no primo
            if (num2.esPrimo())
               JOptionPane.showMessageDialog(null,"Es primo");
           else
               JOptionPane.showMessageDialog(null,"No es primo");
         
           
           break;
               case 3:
                   JOptionPane.showMessageDialog(null, "dividir");
        Numero num3;
        num3= new Numero(Integer.parseInt(JOptionPane.showInputDialog("Numero?")));
         n=num3.cosiente(Integer.parseInt(JOptionPane.showInputDialog("Digite el numero")));
           JOptionPane.showMessageDialog(null,"El cosiente es: "+n);
         
           //indica que el cosiente es o no primo
           if (num3.esPrimo())
               JOptionPane.showMessageDialog(null,"Es primo");
           else
               JOptionPane.showMessageDialog(null,"No es primo");
     
     
           break;
               default: seguir=false;
                }
         } while (seguir);
    }
}

CLASE NUMERO


public class Numero {
   
    private int valor;

    public Numero() {
        valor = 0;
    }

    public Numero(int val) {
        valor = val;
    }
   
    public int suma (int x){
       
        int w;
        w= valor+x;
        return(w);
    }
   
    public boolean esPar(){
       
        return(valor%2==0);
    }
   
    //multiplicar
     public int producto  (int x){
       
        int w;
        w= valor*x;
        return(w);
    }
   
     //dividir
      public int cosiente (int x){
       
        int w;
        w= valor/x;
        return(w);
    }

     
      //es o no primo
      public boolean esPrimo(){
          boolean sw=true;
 
         
          for (int d=2; d<=(this.valor/2); d++) {
              if (this.valor%d==0)
                  sw=false;
          }
         
          return(sw);
      }
   }

MENÚ BÁSICO

package menubasico;
import javax.swing.*;

/**
 *
 * @author RafaLizcano
 */
public class Main {

   public static int menu() {
        int opc=-1;
        String cadMenu="Digite sus opciones:\r\n\r\n"
                + "1. Opcion 1\r\n"
                + "2. Opcion 2\r\n"
                + "3. Opcion 3\r\n"
                + "\r\n0. SALIR\r\n \r\n";
        do {
           try {
            opc=Integer.parseInt(JOptionPane.showInputDialog(cadMenu));
            }
            catch(Exception e){
                System.out.println("No es un número, es una cadena de texto.");
                JOptionPane.showMessageDialog(null, "No es una entrada valida \r\n"+ e.getMessage());
             }

        } while ((opc<0)||(opc>3));

        return (opc);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         boolean seguir=true;

        do {
           switch (menu()) {
               case 1:
                   JOptionPane.showMessageDialog(null, "Aqui va la opcion 1");
                   break;
               case 2:
                   JOptionPane.showMessageDialog(null, "Aqui va la opcion 2");
                   break;
               case 3:
                   JOptionPane.showMessageDialog(null, "Aqui va la opcion 3");
                   break;
               default: seguir=false;
                }
         } while (seguir);
    }

}

PROGRAMA DE CLASES (BIBLIOTECA)

CLASE MAIN


package parcial_p2;
import javax.swing.*;

/**
 *
 * @author CENTIC
 */
public class Main {
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     
       Usuario u;
     
       u=new Usuario(JOptionPane.showInputDialog("digite el doct"),
                   JOptionPane.showInputDialog("digite el NOMBRE" ));
       
       
        // TODO code application logic here
       
    }

}

CLASE USUARIO


public class Usuario {
    private String noDcto;
    private String nombre;

    public Usuario(String noDcto, String nombre) {
        this.noDcto = noDcto;
        this.nombre = nombre;
    }

    public String mostrarDatos(){
        return ("Documento: " + this.noDcto + "\n Nombre: "+ this.nombre);
    }

    public String getNoDcto() {
        return noDcto;
    }

    public void setNoDcto(String noDcto) {
        this.noDcto = noDcto;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }


}


CLASE PRÉSTAMO

public class Prestamo {
    private int estado;
    private Usuario usuario;
    private Fecha fechaInicio;
    private Fecha fechaFin;

    public Prestamo(Usuario usuario, Fecha fechaInicio, Fecha fechaFin) {
        this.usuario = usuario;
        this.fechaInicio = fechaInicio;
        this.fechaFin = fechaFin;
        this.estado=1;  //1 Activo, 0 Terminado, 2  Atrasado
    }

    public String mostrarDatos() {
        return "Prestamo{" + "estado=" + estado +
                "usuario=" + usuario.mostrarDatos() +
                "fechaInicio=" + fechaInicio.impFecha() +
                "fechaFin=" + fechaFin.impFecha() + "}";
    }


    public int getEstado() {
        return estado;
    }

    public void setEstado(int estado) {
        this.estado = estado;
    }

    public Fecha getFechaFin() {
        return fechaFin;
    }

    public void setFechaFin(Fecha fechaFin) {
        this.fechaFin = fechaFin;
    }

    public Fecha getFechaInicio() {
        return fechaInicio;
    }

    public void setFechaInicio(Fecha fechaInicio) {
        this.fechaInicio = fechaInicio;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }




}

CLASE LIBRO

public class Libro {

    private String isbn;
    private String titulo;
    private Ejemplar [] ejemplares;

    public Libro(String isbn, String titulo) {
        this.isbn = isbn;
        this.titulo = titulo;
        this.ejemplares=new Ejemplar[3];
    }


    public Ejemplar[] getEjemplares() {
        return ejemplares;
    }

    public void setEjemplares(Ejemplar[] ejemplares) {
        this.ejemplares = ejemplares;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }


}

CLASE FECHA

public class Fecha {

    private int dia;
    private int mes;
    private int año;

    public Fecha (int dia, int mes, int año) {
        this.dia=dia;
        this.mes=mes;
        this.año=año;
    }

    public int compararFecha(Fecha f) {
        //-1 Si es menor, 0 si son iguales y 1 si es mayor
        int sw=0;
        if (f.getAño()==this.año)
        {
            if (this.mes==f.getMes())
            {
                if (this.dia==f.getDia())
                    sw=0;
                else
                    if (this.dia<f.getDia())
                        sw=-1;
                    else
                        sw=1;
            }
            else
                if (this.mes<f.getMes())
                    sw=-1;
                else
                    sw=1;
        }
        else
            if (this.año<f.getAño())
                sw=-1;
            else
                sw=1;
        return (sw);
    }

    public boolean añoBisiesto() {
        if ((this.año%4) == 0)
            return(true);
        else
            return (false);
    }


    public Fecha diaSiguiente(){
        Fecha t=new Fecha (this.dia, this.mes, this.año);

        t.setDia(t.getDia()+1);

        switch (t.getMes()) {
            case 2: if (t.añoBisiesto())
                        if (t.getDia()>29)
                        {
                            t.setDia(1);
                            t.setMes(3);
                        }
                        else if (t.getDia()>28)
                             {
                                t.setDia(1);
                                t.setMes(3);
                              }

                      break;
            case 11:  //meses de 30
            case 4:
            case 6:
            case 9:  if (t.getDia()>30)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
                     break;
            case 12:  if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(1);
                          t.setAño(t.getAño()+1);
                       }
                     break;

            default: //meses de 31 dias
               if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
        }
        return(t);
    }

   public String impFecha() {
       return (this.dia + "/" + this.mes+ "/" +this.año);
   }


    public int getAño() {
        return año;
    }

    public void setAño(int año) {
        this.año = año;
    }

    public int getDia() {
        return dia;
    }

    public void setDia(int dia) {
        this.dia = dia;
    }

    public int getMes() {
        return mes;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }


}

CLASE  EJEMPLAR

public class Ejemplar {
  private String noReferencia;
  private Prestamo [] prestamos;

    public Ejemplar(String noReferencia, Prestamo[] prestamos) {
        this.noReferencia = noReferencia;
        this.prestamos = prestamos;
    }
  
  

    public String getNoReferencia() {
        return noReferencia;
    }

    public void setNoReferencia(String noReferencia) {
        this.noReferencia = noReferencia;
    }

    public Prestamo[] getPrestamos() {
        return prestamos;
    }

    public void setPrestamos(Prestamo[] prestamos) {
        this.prestamos = prestamos;
    }
  
  

}

CLASE BIBLIOTECA

public class Biblioteca {

    private Libro [] libros;
    private Usuario [] usuarios;

    public Biblioteca(Libro[] libros, Usuario[] usuarios) {
        this.libros = libros;
        this.usuarios = usuarios;
    }

    
    
    public Libro[] getLibros() {
        return libros;
    }

    public void setLibros(Libro[] libros) {
        this.libros = libros;
    }

    public Usuario[] getUsuarios() {
        return usuarios;
    }

    public void setUsuarios(Usuario[] usuarios) {
        this.usuarios = usuarios;
    }
}



PROGRAMA DE CLASES (PLANCHA)

CLASE MAIN


public class Main {

   
    public static void main(String[] args) {
        // TODO code application logic here
        Plancha p1;
        p1=new Plancha();
       
        p1.encender();
       
       
    }

}

CLASE PLANCHA


public class Plancha {

    private int estado;  //0 apagado, 1 Encendido, 2 dañada, 3 mantenimiento
    private int sn;
    private String dueño;
   
    public void encender(){
        estado=1;
    }
   
    public void apagar(){
        estado=0;
    }
   
    public String verEstado(){
        String est;
       
        if (estado==0)
            est="Apagado";
        else if (estado==1)
               est="Encendido";
             else
                est="Otro";
        return(est);
        }
       
    }

PROGRAMA DE CLASES (FECHA)

CLASE MAIN


import javax.swing.*;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Fecha a, b;
        int x;

        a=new Fecha (Integer.parseInt(JOptionPane.showInputDialog("Dia?")),
                    Integer.parseInt(JOptionPane.showInputDialog("MEs?")),
                Integer.parseInt(JOptionPane.showInputDialog("Año?")));

        b=new Fecha (Integer.parseInt(JOptionPane.showInputDialog("Dia?")),
                    Integer.parseInt(JOptionPane.showInputDialog("MEs?")),
                Integer.parseInt(JOptionPane.showInputDialog("Año?")));

        JOptionPane.showMessageDialog(null,"Dia siguiente de: " + a.impFecha() + " es: " + a.diaSiguiente().impFecha());

        x=a.compararFecha(b);
        if (x==0)
            JOptionPane.showMessageDialog(null,"Son iguales");
        else
            if (x==-1)
                JOptionPane.showMessageDialog(null,a.impFecha() + " es menor de " + b.impFecha());
            else
                JOptionPane.showMessageDialog(null,a.impFecha() + " es mayor de " + b.impFecha());



    }

}


CLASE FECHA


public class Fecha {

    private int dia;
    private int mes;
    private int año;

    public Fecha (int dia, int mes, int año) {
        this.dia=dia;
        this.mes=mes;
        this.año=año;
    }

    public int compararFecha(Fecha f) {
        //-1 Si es menor, 0 si son iguales y 1 si es mayor
        int sw=0;
        if (f.getAño()==this.año)
        {
            if (this.mes==f.getMes())
            {
                if (this.dia==f.getDia())
                    sw=0;
                else
                    if (this.dia<f.getDia())
                        sw=-1;
                    else
                        sw=1;
            }
            else
                if (this.mes<f.getMes())
                    sw=-1;
                else
                    sw=1;
        }
        else
            if (this.año<f.getAño())
                sw=-1;
            else
                sw=1;
        return (sw);
    }

    public boolean añoBisiesto() {
        if ((this.año%4) == 0)
            return(true);
        else
            return (false);
    }


    public Fecha diaSiguiente(){
        Fecha t=new Fecha (this.dia, this.mes, this.año);

        t.setDia(t.getDia()+1);

        switch (t.getMes()) {
            case 2: if (t.añoBisiesto())
                        if (t.getDia()>29)
                        {
                            t.setDia(1);
                            t.setMes(3);
                        }
                        else if (t.getDia()>28)
                             {
                                t.setDia(1);
                                t.setMes(3);
                              }

                      break;
            case 11:  //meses de 30
            case 4:
            case 6:
            case 9:  if (t.getDia()>30)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
                     break;
            case 12:  if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(1);
                          t.setAño(t.getAño()+1);
                       }
                     break;

            default: //meses de 31 dias
               if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
        }
        return(t);
    }

   public String impFecha() {
       return (this.dia + "/" + this.mes+ "/" +this.año);
   }


    public int getAño() {
        return año;
    }

    public void setAño(int año) {
        this.año = año;
    }

    public int getDia() {
        return dia;
    }

    public void setDia(int dia) {
        this.dia = dia;
    }

    public int getMes() {
        return mes;
    }

    public void setMes(int mes) {
        this.mes = mes;
    }


}

martes, 17 de enero de 2012

4. Programa de vectores


import javax.swing.JOptionPane;

public class Main {
public static void main(String[] args) {
    
        int n;
        float V[];
        float def=0;
       
       String t =JOptionPane.showInputDialog("Digite el numero de notas");
        n=Integer.parseInt(t);
       
      
        V = new float[n];
       
        for (int i=0;i<n;i++)
        {
        V[i]= Float.parseFloat(JOptionPane.showInputDialog("Digite la nota"+i));
       
        def=def+V[i];
       }
        def = def/n;
        JOptionPane.showMessageDialog(null,"La definitiva es:"+def);
       
        // TODO code application logic here
    }

}