Primo Anno I.C.M.C

Esame TLP

  • Messaggi
  • OFFLINE
    bgfeddy
    Post: 9
    Città: TORINO
    Età: 37
    Sesso: Maschile
    00 03/07/2008 15:16
    che poi su google c'è
    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 ();
    	}
    	
    	
    }
    
    [Modificato da bgfeddy 03/07/2008 15:17]
  • OFFLINE
    bgfeddy
    Post: 9
    Città: TORINO
    Età: 37
    Sesso: Maschile
    00 03/07/2008 15:18
    eccezioni
    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");
    	}
    }
    
    [Modificato da bgfeddy 03/07/2008 15:21]
  • OFFLINE
    bgfeddy
    Post: 9
    Città: TORINO
    Età: 37
    Sesso: Maschile
    00 03/07/2008 15:18
    test
    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();
    	}
    }
    
    [Modificato da bgfeddy 03/07/2008 15:20]
  • OFFLINE
    bgfeddy
    Post: 9
    Città: TORINO
    Età: 37
    Sesso: Maschile
    00 03/07/2008 22:54
    c'è qualche errorino e manca il metodo Math.round (double) per approssimare all'intero più vicino