Advertisement
fmbalvarez

Guía 8 - Ejercicio 1/5 - Humedad [Technical Preview]

Nov 5th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1.  
  2. public class MedidorDeHumedad {
  3.    
  4.     private int nivelDeHumedad;
  5.    
  6.     MedidorDeHumedad(int nivelDeHumedad){
  7.        
  8.         this.nivelDeHumedad = nivelDeHumedad;
  9.     }
  10.    
  11.     public int getHumedad(){
  12.        
  13.         return this.nivelDeHumedad;
  14.     }
  15.    
  16.  
  17.     public void setHumedad(int nivelDeHumedad) {
  18.        
  19.         this.nivelDeHumedad = nivelDeHumedad;
  20.     }
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27. public class Terreno {
  28.    
  29.     private MedidorDeHumedad[][] medidores;
  30.    
  31.     public Terreno(int ancho, int largo, int nivelDeHumedad){
  32.        
  33.         if (ancho <= 0 || largo <= 0 || nivelDeHumedad < 0 || nivelDeHumedad > 100){
  34.            
  35.             throw new Error("No respeta las pre-condiciones.");
  36.         }
  37.        
  38.         this.medidores = new MedidorDeHumedad[largo][ancho];
  39.        
  40.         for (int i = 0; i < this.medidores.length; i++){
  41.            
  42.             for (int j = 0; j < this.medidores[i].length; j++){
  43.                
  44.                 this.medidores[i][j] = new MedidorDeHumedad(nivelDeHumedad);
  45.             }
  46.         }
  47. }
  48.    
  49.     public MedidorDeHumedad obtenerMedidor(int largo, int ancho){
  50.        
  51.         return this.medidores[largo][ancho];
  52.     }
  53.    
  54.     public int calcularPromedio(){
  55.        
  56.         int humedadTotal = 0;
  57.         int cantidadDeMedidores = 0;
  58.  
  59.         for (int i = 0; i < this.medidores.length; i++){
  60.            
  61.             for (int j = 0; j < this.medidores[i].length; j++){
  62.                
  63.                 humedadTotal += this.obtenerMedidor(i,j).getHumedad();
  64.                 cantidadDeMedidores++;
  65.             }
  66.     }
  67.        
  68.         return (humedadTotal/cantidadDeMedidores);
  69. }
  70.    
  71.     public int buscarMaximo(){
  72.        
  73.         int valorMaximo = 0;
  74.        
  75.         for (int i = 0; i < this.medidores.length; i++){
  76.            
  77.             for (int j = 0; j < this.medidores[i].length; j++){
  78.                
  79.                 if (this.obtenerMedidor(i,j).getHumedad() >= valorMaximo){
  80.                    
  81.                     valorMaximo = this.obtenerMedidor(i, j).getHumedad();
  82.                 }
  83.             }
  84.     }
  85.        
  86.         return valorMaximo;
  87. }
  88.    
  89.     public int buscarMinimo(){
  90.        
  91.         int valorMinimo = 0;
  92.        
  93.         for (int i = 0; i < this.medidores.length; i++){
  94.            
  95.             for (int j = 0; j < this.medidores[i].length; j++){
  96.                
  97.                 if (this.obtenerMedidor(i,j).getHumedad() >= valorMinimo){
  98.                    
  99.                     valorMinimo = this.obtenerMedidor(i, j).getHumedad();
  100.                 }
  101.             }
  102.     }
  103.        
  104.         return valorMinimo;
  105. }
  106.    
  107.     public void modificarHumedad(int nivelDeHumedad, int ancho, int largo){
  108.        
  109.         this.obtenerMedidor(largo, ancho).setHumedad(nivelDeHumedad);
  110.     }
  111.    
  112.    
  113.     public int calcularContorno(int ancho, int largo){
  114.        
  115.         int humedadTotal = 0;
  116.        
  117.         humedadTotal = this.obtenerMedidor(largo + 1, ancho).getHumedad()
  118.                      + this.obtenerMedidor(largo, ancho + 1).getHumedad()
  119.                      + this.obtenerMedidor(largo - 1, ancho).getHumedad()
  120.                      + this.obtenerMedidor(largo, ancho - 1).getHumedad();
  121.            
  122.        
  123.         return (humedadTotal/4);
  124.         }
  125.  
  126.    
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement