Mostrando entradas con la etiqueta java. Mostrar todas las entradas
Mostrando entradas con la etiqueta java. Mostrar todas las entradas

lunes, 4 de octubre de 2010

UNA MATRIZ EN JAVA CON SCANNER


ESTA ES UNA SENCILLA AYUDA PARA QUIEN QUIERA EN JAVA LLENAR Y MOSTAR UNA MATRIZ, ADEMAS UTILIZO UN SCANNER PARA LEER DESDE LA CONSOLA.
PUEDEN CORTAR Y PEGAR EN SU PACKAGE Y FUNCIONARA SIN PROBLEMAS.
public class Main {
    public static Scanner EN = new Scanner(System.in);
    public static int i,j;
    public static void main(String[] args) {
        int M, N;
        System.out.println("inserte la cantidad de filas");
        M = EN.nextInt();//CON SCANNER JAVA  LEEMOS
        System.out.println("inserte la cantidad de columnas");
        N = EN.nextInt();//CON SCANNER JAVA  LEEMOS

        System.out.println("inserte los datos");

        int mat[][] = new int[M][N];//DECLARAMOS E INICIALIZAMOS UNA MATRIZ EN JAVA

        llenar(mat);//LLAMAMOS AL METODO LLENAR Y LE PASAMOS UNA MATRIZ EN JAVA
System.out.println("La Matriz queda asi!!");

        mostrar(mat);//LLAMAMOS AL METODO MOSTRAR Y LE PASAMOS UNA MATRIZ
    } 
//EL METODO EN JAVA PARA LLENAR
    private static void llenar(int[][] mat) {
       for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                mat[i][j] = EN.nextInt();//EL OBJETO DE TIPO SCANNER LEE
            }
        }
    }
//EL METODO EN JAVA PARA MOSTRAR
    private static void mostrar(int[][] mat) {
        for ( i = 0; i < mat.length; i++) {
            for ( j = 0; j < mat.length; j++) {
                System.out.print(mat[i][j] + " ");
            }
            System.out.println();
        }
    }   
}
LUEGO DE INGRESAR LAS CANTIDADES PARA FILA Y COLUMNA, PEDIRA LOS DATOS
DE LA MATRIZ A LLENAR , POR ULTIMO MOSTRARA EN ORDEN Y EN FORMATO DE MATRIZ.
LA SALIDA POR CONSOLA DEBERA SER ASI:

miércoles, 22 de septiembre de 2010

EJERCICIO DE PROGRAMACION III

AQUI LES DEJO EL MAIN EL EJERCICIO DE PROGRAMACION III
OBVIO QUE USTEDES HABRAN HECHO LOS MODELOS
ESTO ES SOLO PARA GUIA.
package ormjpa;

import controlador.GestorPersona;
import java.util.ArrayList;
import java.util.List;
import modelo.Articulo;
import modelo.Direccion;
import modelo.Cliente;
import modelo.Factura;
import modelo.FacturaDetalle;
import modelo.TipoArticulo;

/**
 *
 * @author guillermo garcia huidobro
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       
        try {
            GestorPersona gp = new GestorPersona();

            TipoArticulo f = new TipoArticulo();
            Articulo g = new Articulo();
            FacturaDetalle h = new FacturaDetalle();
            Listlistafdetalle = new ArrayList();

            Cliente p = new Cliente();
            Direccion d = new Direccion();                                                                      
            Factura a = new Factura();
            List listafactura = new ArrayList();

            //set cliente
            p.setNombre("guillermo");
            p.setApellido("garcia");
            p.setCuil("92814690");
            //set direccion
            d.setNombrecalle("reta");
            d.setNumero(45);
            //set factura
            a.setTotal("1000");
            a.setNoFactura("1235");
            a.setFecha("17/25/65");
            //guardo en factura
            listafactura.add(a);

            //guardo en direccion
           p.setDireccion(d);
            //guardo en factura
            p.setListaFactura(listafactura);

           
            f.setDenominacion("procesador");//tipoarticulo
            g.setDenominacion("doble nucleo");//articulo
            g.setPrecio("$250");//articulo
            g.setTipoarticulo(f);//guardo un tipoarticulo
            h.setCantidad("10");//facturadetalle
            h.setSubtotal("$2500");//facturadetalle
            h.setArticulo(g);//guardo un articulo
            h.setFactura(a);//guardo una factura
            listafdetalle.add(h);//guardo un facturadetalle
                      
            gp.guardar(p);
            gp.guardar(h);
                        
        } catch (Exception e) {
            e.printStackTrace();
        }      
    }
}

viernes, 30 de julio de 2010

EJEMPLO DE ARRAYLIST

Creamos una clase con sus atributos privados
public class Producto{
private String nombre;
private int cantidad;
Cada uno con sus get and set
      private void setnombre (String nom){
            nombre=nom;
      }
     private void setnombre (int cant){
            cantidad=cant;

      }
     private String getnombre(){
            return nombre;
     }
      private String getnombre(){
            return nombre;
                                                                           }
Creamos los constructores el vacio y el sobrecargado
     public Producto{
     }
      public Producto(String nom,int cant){
              nombre=nom;
              canidad=cant;
     }
  }

En el Main hacemos lo siguiente

public class Main{                                          
     public void class main(String[] args){
Creamos 2 instancias de la clase Producto         
     Producto a = new Producto ("disco duro", 33256);
     Producto b = new Producto ("cable sata", 11656);
Creamos un ArrayList
      ArrayList lista = new ArrayList();
Insertamos los objetos
      lista.add(a);
      lista.add(b);
Creamos un metodo para mostrar
     mostrar(lista);


     }
Metodo para mostrar el ArrayList
      private void mostrar(List lista){
           for(iterator it = lista.iterator().hasNext()){
           Producto x = (Producto)it.Next();
           sout(x.getName+":"+x.getcantidad);
          }
      }
/*===============medicion============================*/