Advertisement
Graf_Spee

CBD.java

May 9th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package circuitobioquímicodigital;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CBD {
  6.     private boolean[][] matriz;
  7.     private int[] matriz_a;
  8.     private int p, m;
  9.     Scanner scan;
  10.    
  11.     CBD(int p, int m) { // p = pontos = colunas | m = medições = linhas
  12.         matriz = new boolean[m][p + 1];
  13.         matriz_a = new int[p + 1];
  14.         for(int i = 0; i < p; i++)
  15.             matriz_a[i] = 0;
  16.         scan = new Scanner(System.in);
  17.         this.p = p;
  18.         this.m = m;
  19.     }
  20.    
  21.     public void lerCBD() {
  22.         System.out.println("Insira, linha por linha, o CBD:");
  23.         for(int i = 0; i < this.m; i++) {
  24.             for(int j = 0; j < this.p; j++) {
  25.                 int temp = scan.nextInt();
  26.                 if(temp == 1)                    
  27.                     matriz[i][j] = true;
  28.                 else if(temp == 0)
  29.                     matriz[i][j] = false;
  30.                 else {
  31.                     System.out.println("Valor inválido! Definido para 1.");
  32.                     matriz[i][j] = true;
  33.                 }
  34.             }
  35.         }        
  36.     }
  37.    
  38.     public void calcularSaida() {
  39.         boolean anterior = false;
  40.         for(int i = 0, temp = 0; i < this.p; i++) {
  41.             for(int j = 0; j < this.m; j++) {
  42.                 if(matriz[j][i]) {
  43.                     temp++;
  44.                     anterior = true;
  45.                 }
  46.                 if(j + 1 == this.m || (!matriz[j][i] && anterior)) {
  47.                     matriz_a[temp]++;
  48.                     System.out.println(temp);
  49.                     anterior = false;
  50.                     temp = 0;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.    
  56.     public void printarSaida() {
  57.         System.out.println("\n\n" + this.toString() + "\nForam encontrados:");
  58.         for(int i = 0; i <= this.m; i++)
  59.             if(matriz_a[i] != 0 && i != 0)
  60.                 System.out.println(matriz_a[i] + " palito(s) de comprimento " + i);
  61.     }
  62.    
  63.     @Override
  64.     public String toString() {
  65.         String field = "";
  66.         for(int i = 0; i < this.m; i++) {
  67.             for(int j = 0; j < this.p; j++) {
  68.                 if(matriz[i][j])
  69.                     field += "1 ";
  70.                 else
  71.                     field += "0 ";
  72.             }
  73.             field += "\n";
  74.         }
  75.         return field;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement