1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
public class Test {
public static void main(String[] args) throws Exception {
Test test = new Test();
System.out.println(test.racine(2));
}
public double racine(double x) throws Exception {
if (!precondition(x)) {
error("precondition failed");
}
// traitement de la racine
double resultat = Math.sqrt(x);
if (!postcondition(resultat, x)) {
error("postcondition failed");
}
return resultat;
}
private void error(String error) throws Exception {
throw new Exception("error = " + error);
}
private boolean precondition(double x) {
return (x >=0) ? true : false;
}
private boolean postcondition(double resultat, double x) {
return (resultat >=0 && (resultat * resultat == x)) ? true : false;
}
} |
Partager