package esame; public class GrayImage { private int i=0; private int j=0; private int [][] image; public GrayImage (int i, int j) { this.i=i; this.j=j; image=new int [j]; } public void printValues () { for (int k=0; k<i; k++) { for (int h=0; h<j; h++) System.out.print (image [k][h] + " "); System.out.println (" "); } } public void changeValue (int i, int j, int v) { image [j]=v; } public void checkValues () throws ValueOutOfBoundException { for (int k=0; k<i; k++) { for (int h=0; h<j; h++) if ((image [k][h]<0)||(image [k][h]>255)) throw new ValueOutOfBoundException (image[k][h], k, h); } System.out.println ("Tutti i valori sono ok"); } public void translate (int Z) throws ParameterOutOfBoundException { int Y=0; if (Z>=-100&&Z<=100) { for (int k=0; k<i; k++) { for (int h=0; h<j; h++) { Y=image [k][h]; Y=Y+Z; image [k][h]=Y; if (Y+Z>255){ Y=255; image [k][h]=Y; } else if (Y+Z<0){ Y=0; image [k][h]=Y; } } } } else throw new ParameterOutOfBoundException (); } public void expand (float K) throws ParameterOutOfBoundException { int Y=0; if (K>=0.5&&K<=1.5) { for (int k=0; k<i; k++) { for (int h=0; h<j; h++) { Y=image[k][h]; Y=(int)((float)Y*K); image[k][h]=Y; } } } else throw new ParameterOutOfBoundException (); } }
package esame; public class ValueOutOfBoundException extends Exception { public ValueOutOfBoundException (int p, int i, int j) { super ("Errore. Il valore di intensità " + p + " in posizione " + i + " " + j +" è errato"); } } package esame; public class ParameterOutOfBoundException extends Exception { public ParameterOutOfBoundException () { super ("Errore. Parametro non compreso nell'intervallo"); } }
package esame; public class TestEsame { public static void main(String[] args) { GrayImage Image1= new GrayImage (12, 20); for (int k=0; k<20; k++) for (int h=0; h<12; h++) Image1.changeValue(h, k, (k*h)); //scambiati h e k try { Image1.checkValues(); } catch (ValueOutOfBoundException e) { System.out.println (e.getMessage()); } try { Image1.translate(-3); } catch (ParameterOutOfBoundException e) { System.out.println (e.getMessage()); } try { Image1.translate(5); } catch (ParameterOutOfBoundException e) { System.out.println (e.getMessage()); } try { Image1.expand((float)1.1); //casting anche qui } catch (ParameterOutOfBoundException e) { System.out.println (e.getMessage()); } Image1.printValues(); } }