Metodologie di Programmazione a.a. 2008-2009
(canale E-O)

Soluzioni 11 marzo 2009

Esercizio [Potenze]
public class Potenze {
    public static double integerPow(double b, int k) {
        if (k == 0) return 1;
        else {
            double x = integerPow(b, k/2);
            x *= x;
            return (k % 2 == 0 ? x : x*b);
        }
    }
    public static void main(String[] args) {
        double maxErrRelativo = 0;
        for (long i = 1 ; i <= 10000 ; i++) {
            double b = 0.5 + Math.random(); // un numero random tra 0.5 e 1.5
            int k = (int)Math.round(100*Math.random());  // un intero random tra 0 e 100
            double m = Math.pow(b, k);
            double ip = integerPow(b, k);
            double err = Math.abs(m - ip)/m;   // errore relativo
            if (err > maxErrRelativo) maxErrRelativo = err;
        }
        System.out.println("Massimo errore relativo: "+maxErrRelativo);
    }
}

Esercizi [Date] e [Date+]
import static java.lang.System.*;

public class Data {
    private int giorno, mese, anno;

    public Data(int g, int m, int a) {
        giorno = g;
        mese = m;
        anno = a;
    }

    public void stampaInCifre() {
        out.printf("%02d%02d%d\n", giorno, mese, anno);
    }
    public void stampaConSlash() {
        out.println(giorno+"/"+mese+"/"+anno);
    }
    public void stampaInLettere() {
        String meseInLettere = null;
        switch (mese) {
            case 1: meseInLettere = "gennaio"; break;
            case 2: meseInLettere = "febbraio"; break;
            case 3: meseInLettere = "marzo"; break;
            case 4: meseInLettere = "aprile"; break;
            case 5: meseInLettere = "maggio"; break;
            case 6: meseInLettere = "giugno"; break;
            case 7: meseInLettere = "luglio"; break;
            case 8: meseInLettere = "agosto"; break;
            case 9: meseInLettere = "settembre"; break;
            case 10: meseInLettere = "ottobre"; break;
            case 11: meseInLettere = "novembre"; break;
            case 12: meseInLettere = "dicembre"; break;
        }
        out.println(giorno+" "+meseInLettere+" "+anno);
    }

    public int getGiorno() { return giorno; }
    public int getMese() { return mese; }
    public int getAnno() { return anno; }

        // non tiene conto degli anni bisestili
    public void incrementa(int gg) {
        int ng = nGGdaInizioAnno() + gg;
        int da = ng/365;
        int rg = ng % 365;
        anno += da;
        if (rg < 31) {
            mese = 1;
            giorno = rg + 1;
        } else if (rg < 59) {
            mese = 2;
            giorno = rg - 30;
        } else if (rg < 90) {
            mese = 3;
            giorno = rg - 58;
        } else if (rg < 120) {
            mese = 4;
            giorno = rg - 89;
        } else if (rg < 151) {
            mese = 5;
            giorno = rg - 119;
        } else if (rg < 181) {
            mese = 6;
            giorno = rg - 150;
        } else if (rg < 212) {
            mese = 7;
            giorno = rg - 180;
        } else if (rg < 243) {
            mese = 8;
            giorno = rg - 211;
        } else if (rg < 273) {
            mese = 9;
            giorno = rg - 242;
        } else if (rg < 304) {
            mese = 10;
            giorno = rg - 272;
        } else if (rg < 334) {
            mese = 11;
            giorno = rg - 303;
        } else {
            mese = 12;
            giorno = rg - 333;
        }
    }
        // numero di giorni dal primo giorno dell'anno
    private int nGGdaInizioAnno() {
        int ngm = 0;
        switch (mese) {
            case 12: ngm += 30;
            case 11: ngm += 31;
            case 10: ngm += 30;
            case 9: ngm += 31;
            case 8: ngm += 31;
            case 7: ngm += 30;
            case 6: ngm += 31;
            case 5: ngm += 30;
            case 4: ngm += 31;
            case 3: ngm += 28;
            case 2: ngm += 31;
        }
        return ngm + giorno - 1;
    }
}





-- RiccardoSilvestri - 23 Mar 2009


This topic: Metod_prog/EO > WebHome > MPdiario > SOL110309
Topic revision: r2 - 2009-03-23 - RiccardoSilvestri
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback