Advertisement
Guest User

Ejercicio.java

a guest
Jun 7th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. class Matriz {
  5.     private int [][]matriz;
  6.     private int tam_fila;
  7.     private int tam_columna;
  8.     private int fila_actual;
  9.     private int columna_actual;
  10.  
  11.     public Matriz(int x, int y) {
  12.         this.matriz = new int[x][y];
  13.         this.tam_fila = x;
  14.         this.tam_columna = y;
  15.         this.fila_actual = 0;
  16.         this.columna_actual = 0;
  17.     }
  18.  
  19.     public void mostrar() {
  20.         for(int i = 0; i < tam_fila; i++) {
  21.             for(int j = 0; j < tam_columna; j++) {
  22.                 System.out.printf("%3d",this.matriz[i][j]);
  23.             }
  24.             System.out.println();
  25.         }
  26.     }
  27.  
  28.     public void borrar() {
  29.         for(int i = 0; i < tam_fila; i++) {
  30.             for(int j = 0; j < tam_columna; j++) {
  31.                 this.matriz[i][j] = 0;
  32.             }
  33.         }
  34.     }
  35.  
  36.     public int elemento(int x, int y) {
  37.         return this.matriz[x][y];
  38.     }
  39.  
  40.     public boolean agregar(int n) {
  41.         if(fila_actual < tam_columna) {
  42.             if(columna_actual < tam_columna) {
  43.                 matriz[fila_actual][columna_actual] = n;
  44.                 columna_actual++;
  45.                 if(columna_actual == tam_columna) {
  46.                     fila_actual++;
  47.                     columna_actual = 0;
  48.                 }
  49.                 return true; // Si se pudo ingresar un valor
  50.             }
  51.         }
  52.         return false; // Ya no se pueden ingresar mas valores
  53.     }
  54. }
  55.  
  56. public class Ejercicio {
  57.     public static void main(String[] args) {
  58.         Scanner entrada = new Scanner(System.in);
  59.  
  60.         int filas = 0;
  61.         int columnas = 0;
  62.  
  63.         System.out.printf("Ingrese tamaño filas: ");
  64.         filas = entrada.nextInt();
  65.  
  66.         System.out.printf("Ingrese tamaño columnas: ");
  67.         columnas = entrada.nextInt();
  68.  
  69.         Matriz matriz = new Matriz(filas,columnas);
  70.  
  71.         System.out.println("Llenando la matriz con numeros Aleatorios...");
  72.         Random rnd = new Random();
  73.         for(int i = 0; i < filas; i++) {
  74.             for(int j = 0; j < columnas; j++) {
  75.                 matriz.agregar(rnd.nextInt(100));
  76.             }
  77.         }
  78.  
  79.         System.out.println("Mostrando Matriz...");
  80.         matriz.mostrar();
  81.  
  82.         System.out.println("Borrando la matriz...");
  83.         matriz.borrar();
  84.  
  85.         System.out.println("Mostrando Matriz...");
  86.         matriz.mostrar();
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement