Java Con Arreglos


1er Ejemplo

/*
 * DESCRIPCION: Para recorrer un arreglo se utiliza un ciclo, 
 *por lo general el ciclo for y se señala el índice de la posición a evaluar.
 *Vamos a observar un ejemplo de un vector que es un arreglo unidimensional, 
 * es decir de una sola Posición, una fila n columnas.
 */


public class vector {


    public static void main(String[] args) {
        int[] n; //declaracion del tipo de arreglo, en este caso es un vector.
        n = new int[5];//declaración del numero de posiciones que tendrá el vector.
        for (int i = 0; i < 4; i++) { //ciclo para recorrer el vector, desde la posicion 0 del vector hasta la 4(si cuentas, hay 5 posiciones).
            n[i] = i; //asignamos el contenido de la variable i al vector.
            System.out.println(n[i]); //se imprime el contenido del vector.
        }


    }
}




2do Ejemplo

/* * DESCRIPCIÓN: Programa que al ingresar los numero indica cual es el mayor, el menor y da la suma de los dos. */

public class vector {


    public static void main(String[] args) {


        int mayor,menor,suma;        

        int [] nums={3,4,8,2};//asignamos directamente los valores del vector        
        suma=0;        
        menor=nums[0];        
        mayor=nums[0];    
    
       for(int i=0;i<nums.length;i++){
         if (nums[i]>mayor){           
         mayor=nums[i];          
         }

          if (nums[i]<menor){         

          menor=nums[i];          
          }

          suma+=nums[i];     

       }

 System.out.println("El mayor es"+mayor);     

 System.out.println("El menor es"+menor);     
 System.out.println("La suma es"+suma);     
 } 
 }


3er Ejemplo

/* * DESCRIPCIÓN: Programa que captura las notas de un estudiante y las promedia */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Notas {

    private long codigo;
    private String nombre;
    double[] notas = new double[3];
    private double defi;

    Notas(long codigo, String nombre, double[] arreglo) {
        this.codigo = codigo;
        this.nombre = nombre;
        this.notas = arreglo;
        this.defi = defi;
    }

    public long getcodigo() {
        return codigo;
    }

    public double[] getNotas() {
        return notas;
    }

    public String toString() {
        return " Codigo: " + codigo + " Nombre: " + nombre;
    }
}

class OperaNotas {

    BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
    long codigo;
    String nombre;
    double[] notas = new double[3];
    Notas objeto;
    int def;
    private int total;

    public void setEntrarDatos() throws IOException {
        
        System.out.println("Ingrese Codigo: ");
        codigo = Integer.parseInt(entrada.readLine());
        System.out.println("Ingrese Nombre: ");
        nombre = entrada.readLine();
        for (int j = 0; j < notas.length; j++) {
            System.out.println("Ingrese la Nota: " + (j + 1) + " ");
            notas[j] = Double.parseDouble(entrada.readLine());
        }
        for (int i = 0; i <= 1; i++) {
            for (int j = i + 1; j < 2; j++) {
                def = (int) (notas[i]/3);
                total = def/3 ;
            }
        }
        objeto = new Notas(codigo, nombre, notas);
    }
    

    public void imprimir() {
        System.out.println(objeto.toString());
        for (int i = 0; i < notas.length; i++) {
            System.out.println("Nota " + (i + 1) + " " + objeto.getNotas()[i]);          
        }
        System.out.println("La Definitiva es:"+total);
    }

}

class Test {

    public static void main(String[] arg) throws IOException {
        OperaNotas n = new OperaNotas();
        n.setEntrarDatos();
        n.imprimir();
        

    }
}

4to Ejemplo

import java.io.IOException;

/**
 *PROGRAMA: MATRICES
 * DESCRIPCION: Que muestra en dos matrices la informacion de una fabrica de lavadoras 
 */

public class Lavadora {

    int[][] MatrizA = new int[3][3];//declaramos la primera matriz
    int[][] MatrizB = new int[3][3];//declaramos la segunda matriz
    String [] Vector = new String[3];

    public void setEntrarDatos() throws IOException {

        for (int i = 0; i < Vector.length; i++) {
            Vector[0]="terminacion N:";
            Vector[1]="terminacion L:";
            Vector[2]="terminacion S:";
        }

                MatrizA[0][0]= 400;
                MatrizA[0][1]= 25;
                MatrizA[0][2]= 1;
                MatrizA[1][0]= 200;
                MatrizA[1][1]= 30;
                MatrizA[1][2]= 1/2;
                MatrizA[2][0]= 50;
                MatrizA[2][1]= 33;
                MatrizA[2][2]= 1/3;
                
                MatrizB[0][0]= 300;
                MatrizB[0][1]= 25;
                MatrizB[0][2]= 1;
                MatrizB[1][0]= 100;
                MatrizB[1][1]= 30;
                MatrizB[1][2]= 1/2;
                MatrizB[2][0]= 50;
                MatrizB[2][1]= 33;
                MatrizB[2][2]= 1/3;
        
    }

    public void Imprimir() {
        System.out.println("lavadora tipo A");
        for (int i = 0; i < Vector.length; i++) {
            System.out.print(Vector[i] +"\t"+"\n");
        }
        for (int i = 0; i < MatrizA.length; i++) {
            for (int j = 0; j < MatrizA.length; j++) {
                System.out.print(MatrizA[i][j]+"\t");
            }
            System.out.println();
        }

        System.out.println("lavadora tipo B");
        for (int i = 0; i < Vector.length; i++) {
            System.out.print(Vector[i] +"\t"+"\n");
        }
        for (int i = 0; i < MatrizB.length; i++) {
            for (int j = 0; j < MatrizB.length; j++) {
                System.out.print(MatrizB[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

class testLavadora {

    public static void main(String[] arg) throws IOException  {
        Lavadora N= new Lavadora();
        N.setEntrarDatos();      
        N.Imprimir();
    }
}

5to Ejemplo

/*
 * DESCRIPCION: Programa que da la diagonal de una matriz de 3*3
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Diagonal {
    BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
    int[][] matriz = new int[3][3];

    public void setcargardatos() throws IOException {

        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz.length; j++) {
                System.out.print("ingresar dato:");
                matriz[i][j] = Integer.parseInt(entrada.readLine());
            }
        }
    }

    public void Diagonal1() {

        System.out.println("Diagonal uno:");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == j) {
                    System.out.print(matriz[i][j] + " ");
                }
            }
        }
    }

    public void Diagonal2() {
        if (3 == 3) {
            System.out.println("Diagonal dos:");
            for (int i = 0, j = 3 - 1; i <= 3 - 1; i++, j--) {
                if (i + j == 3 - 1) {
                    System.out.print(matriz[i][j] + " ");
                }
            }
        }
    }
}

class testDIAGONAL {

    public static void main(String[] arg) throws IOException {
        Diagonal D = new Diagonal();
        D.setcargardatos();
        D.Diagonal1();
        D.Diagonal2();
    }
}

6to Ejemplo

/**
 *PROGRAMA: arreglo de duplicados
 *DESCRIPCION: muestra los datos que se repiten 
 */

import java.io.IOException;

public class ArregloD {
    static int numero;
    static char arreg[] = {'1', '0', '1', '8', '4', '6', '4', '8', '3', '8', '3', '1'};
    static int aux = 0;

    public void setentrarnumeros() {
        for (int i = 0; i < arreg.length; i++) {
            for (int j = 0; j < arreg.length; j++) {
                if (i != j && arreg[i] == arreg[j]) {
                    arreg[j] = 0;
                    aux++;
                }
            }
            if (arreg[i] != 0) {
                System.out.print(" " + arreg[i] + " ");
            }
            aux = 0;
        }
    }
}

class testArregoD {

    public static void main(String[] arreg) throws IOException {
        ArregloD I = new ArregloD();
        I.setentrarnumeros();

    }
}

7mo Ejemplo

/*
 * DESCRIPCION: Notas de una estudiante
 */

public class Estudiante {

    double Notas[] = new double[4];
    long Codigo;
    String Nombre;
    String Carrera;
    double Promedio;

    public Estudiante(long Codigo, String Nombre, String Carrera, double Promedio, double [] Arreglo){
        this.Codigo=Codigo;
        this.Nombre=Nombre;
        this.Carrera=Carrera;
        this.Promedio=Promedio;
        this.Notas=Arreglo;
    }

        public long getCodigo() {
        return Codigo;
    }

    public double[] getNotas() {
        return Notas;
    }

     public String toString() {
        return "Codigo: " + Codigo + "\nNombre: " + Nombre + "\nCarrera: " + Carrera;
    }
}


public class Notas {

    Estudiante estudiante;//Asociamos

    double Notas[] = new double[4];
    String Nombre;
    long Codigo;
    String Carrera;
    String Asignatura;
    double Promedio;
    int OPC;
    String p;
    double Definitiva;

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

    public void Menu() throws IOException{
        do {
            System.out.println("*****NOTAS*******");
            System.out.println("1...INGRESE NOTAS");
            System.out.println("2...IMPRIMIR NOTAS");
            System.out.println("3...SALIR");
            OPC = Integer.parseInt(datos.readLine());

            switch (OPC) {//menú
                case 1:
                    EntrarNotas();
                    break;
                case 2:
                    Imprimir();
                    break;
                case 3:
                    break;
                default:
                    System.out.println("ERROR");
            }
            System.out.println("Desea Continuar (S/N)");
            p = datos.readLine();
        }while (OPC >3 || p.equalsIgnoreCase("s"));

    }

    public void EntrarNotas() throws IOException{
         System.out.println("Ingrese Codigo del Estudiante: ");
        Codigo = Integer.parseInt(datos.readLine());
        System.out.println("Ingrese Nombre del Estudiante: ");
        Nombre = datos.readLine();
        System.out.println("Ingrese Carrera del Estudiante");
        Carrera = datos.readLine();
        System.out.println("Ingrese La Asignatura");
        Asignatura = datos.readLine();
        for (int j = 0; j < Notas.length; j++) {
            System.out.println("Ingrese la Nota: " + (j + 1) + " ");
            Notas[j] = Double.parseDouble(datos.readLine());
        }
        for (int i = 0; i <= 1; i++) {
            for (int j = i + 1; j < 4; j++) {
                Definitiva = (int) (Notas[i]/4);
                Promedio = Definitiva/3 ;
            }
        }
        estudiante = new Estudiante(Codigo, Nombre,Carrera, Promedio, Notas);
    }

     public void Imprimir() {
        System.out.println(estudiante.toString());
        for (int i = 0; i < Notas.length; i++) {
            System.out.println("Nota " + (i + 1) + " " + estudiante.getNotas()[i]);
        }
         System.out.println("");
        System.out.println("El Promedio es: "+Promedio);
    }

     public static void main(String[] arg) throws IOException {
         Notas N = new Notas();
         N.Menu();

     }
}

8vo Ejemplo

/* Programa que da las definitivas de los estudiantes, la definitva de la materia y el promedio del curso*/


import java.util.Scanner;

public class definitiva {

    public static void main(String[] args) {
        int i,j;
        String materia,docente;
        String nombre[]=new String [5];
        float definitiva = 0;
        float n[][]=new float[5][3];
        float p[]=new float [5];
        float prom[]=new float [5];
        Scanner teclado=new Scanner (System.in);//utilizamos Scanner reemplazando BufferedReader
        System.out.print("Materia: ");
        materia=teclado.next();
        System.out.print("docente: ");
        docente=teclado.next();
        for (i=0;i<=4;i++){
        System.out.print("ingrese nombre del estudiante: ");
        nombre[i]=teclado.next();
        for (j=0;j<=2;j++){
            System.out.print("nota de "+nombre[i]+" : ");
            n[i][j]=teclado.nextFloat();
            p[i]+=n[i][j];
            }
        }
        System.out.println("La materia "+materia+" con el/la docente "+docente
                + " tiene a los siguientes");
        System.out.println("estudiantes con un promedio y una definitiva de curso "
                + "de: ");
        for(i=0;i<=4;i++){
        prom[i]=p[i]/3;
        
        System.out.println("El promedio de "+nombre [i]+" es de = "+prom[i]);
        definitiva += prom[i]/5;
        }
        System.out.println("La definitiva del curso es = "+definitiva);
    }
}

10 comentarios:

  1. Necesito un programa que me permita listar los nombres de n alumnos ingresados x teclado con un menu.-- :)

    ResponderEliminar
  2. Hola no tienes un ejemplo que incluya un menu?

    ResponderEliminar
  3. Hola que tal, tengo dudas sobre el"3er Ejemplo". ¿Es correcto hacer instancia de una objeto de tipo array estando dentro de la clase ? y no dentro del main() Gracias.

    ResponderEliminar
  4. Estuve viendo tus ejemplos en java, clases usando arreglos, estaba leyendo el código y me parece que son poco claros. Intente hacer uno de tus ejemplos el "3er Ejemplo". Me dan errores por todos los lados. Me parece que tenes que completar con comentarios los ejemplos porque hay mucho código que no se entiendo y no se sabe que es lo que hace. Saludos

    ResponderEliminar
  5. muchas gracias por tu ayuda m sirvio muchisimo :3

    ResponderEliminar
  6. Hola carolina arenas mi me puedes mandar solisito en el fecbook para preguntarte algo de sistema si aparezco como gabrielito hernandez

    ResponderEliminar
  7. Crear un arreglo multidimensional que guarde más datos personales tus compañeros de clase (nombre, apellido, carrera, lugarTrabajo

    ResponderEliminar
  8. Crear un arreglo multidimensional que guarde más datos personales

    ResponderEliminar
  9. Crear un arreglo multidimensional que guarde más datos personales tus compañeros de clase (nombre, apellido, carrera, lugarTrabajo), tomando como referencia de la información colocada en el foro Conociendonos. Llenar 5 registros Ejemplo

    ResponderEliminar