From: Jérôme Benoit Date: Sun, 15 Apr 2018 19:28:41 +0000 (+0200) Subject: exo6: add the full implementation. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c70430542780ccdc90f1eceed659d016951aa2bd;p=Project_POO.git exo6: add the full implementation. Signed-off-by: Jérôme Benoit --- diff --git a/exo6/Chiffre.java b/exo6/Chiffre.java index be6f881..a040ee9 100644 --- a/exo6/Chiffre.java +++ b/exo6/Chiffre.java @@ -14,7 +14,11 @@ class Chiffre extends Facteur { private boolean isChiffre() { return Character.isDigit(c); } - public double evaluer() { - return 0.0; + public double evaluer() throws NotDigitException { + if(isChiffre()) { + return Character.getNumericValue(c); + } else { + throw new NotDigitException(c + " is not a digit"); + } } } diff --git a/exo6/Expression.java b/exo6/Expression.java index dc8a855..22296ab 100644 --- a/exo6/Expression.java +++ b/exo6/Expression.java @@ -1,5 +1,5 @@ public abstract class Expression { - abstract double evaluer(); + abstract double evaluer() throws NotDigitException; } diff --git a/exo6/Main.java b/exo6/Main.java index 5bc0ec4..4b714af 100644 --- a/exo6/Main.java +++ b/exo6/Main.java @@ -1,3 +1,4 @@ +import java.util.Map.Entry; class Main { @@ -6,5 +7,35 @@ class Main { * @param String[] args main() function arguments array */ public static void main(String[] args) { + System.out.println("-----"); + String exp = "5 + x"; + try { + Variable x = new Variable(); + Expression ArithmeticExpression = new Opplus(new Chiffre('5'), x); + while (true) { + Entry e = x.getFirstEntry(); + System.out.println("x = " + e.getValue() + " :"); + System.out.println(exp + " = " + ArithmeticExpression.evaluer()); + } + } + catch (Exception e) {} + + System.out.println("-----"); + exp = "(3 + 2*x)*(7/y - 2) - 5/(3*z + 4)"; + try { + Variable x = new Variable(); + Variable y = new Variable(); + Variable z = new Variable(); + Expression ParentheseOne = new ParentheseExp(new Opplus(new Chiffre('3'), new Opmulti(new Chiffre('2'), x))); + Expression ParentheseTwo = new ParentheseExp(new Opminus(new Opdiv(new Chiffre('7'), y), new Chiffre('2'))); + Expression ParentheseThree = new ParentheseExp(new Opplus(new Opmulti(new Chiffre('3'), z), new Chiffre('4'))); + Expression ArithmeticExpression = new Opminus(new Opmulti((Facteur)ParentheseOne, (Terme)ParentheseTwo), new Opdiv(new Chiffre('5'), (Terme)ParentheseThree)); + while (true) { + Entry e = x.getFirstEntry(); + System.out.println("x = y = z = " + e.getValue() + " :"); + System.out.println(exp + " = " + ArithmeticExpression.evaluer()); + } + } + catch (Exception e) {} } } diff --git a/exo6/Makefile b/exo6/Makefile index d8399cf..4c0ea48 100644 --- a/exo6/Makefile +++ b/exo6/Makefile @@ -47,6 +47,7 @@ JVM = java CLASSES = \ NotCharacterException.java \ + NotDigitException.java \ Expression.java \ Terme.java \ Opadd.java \ @@ -58,7 +59,7 @@ CLASSES = \ Opdiv.java \ Chiffre.java \ Variable.java \ - Parenthese.java \ + ParentheseExp.java \ Main.java # diff --git a/exo6/NotDigitException.java b/exo6/NotDigitException.java new file mode 100644 index 0000000..0cd91fd --- /dev/null +++ b/exo6/NotDigitException.java @@ -0,0 +1,24 @@ + +public class NotDigitException extends Exception { + + public NotDigitException() { + super(); + // TODO Auto-generated constructor stub + } + + public NotDigitException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public NotDigitException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public NotDigitException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/exo6/Opdiv.java b/exo6/Opdiv.java index 0fe9718..dd85ebc 100644 --- a/exo6/Opdiv.java +++ b/exo6/Opdiv.java @@ -5,7 +5,7 @@ class Opdiv extends Opmul { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() / droite.evaluer(); } } diff --git a/exo6/Opminus.java b/exo6/Opminus.java index 9e5307a..190cc88 100644 --- a/exo6/Opminus.java +++ b/exo6/Opminus.java @@ -5,7 +5,7 @@ class Opminus extends Opadd { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() - droite.evaluer(); } } diff --git a/exo6/Opmulti.java b/exo6/Opmulti.java index fdb93fd..13e7c81 100644 --- a/exo6/Opmulti.java +++ b/exo6/Opmulti.java @@ -5,7 +5,7 @@ class Opmulti extends Opmul { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() * droite.evaluer(); } } diff --git a/exo6/Opplus.java b/exo6/Opplus.java index 95b200b..185e66b 100644 --- a/exo6/Opplus.java +++ b/exo6/Opplus.java @@ -5,7 +5,7 @@ class Opplus extends Opadd { super(g, d); } - public double evaluer() { + public double evaluer() throws NotDigitException { return gauche.evaluer() + droite.evaluer(); } } diff --git a/exo6/Parenthese.java b/exo6/Parenthese.java deleted file mode 100644 index 2be283b..0000000 --- a/exo6/Parenthese.java +++ /dev/null @@ -1,7 +0,0 @@ - -class Parenthese extends Facteur { - - public double evaluer() { - return 0.0; - } -} diff --git a/exo6/ParentheseExp.java b/exo6/ParentheseExp.java new file mode 100644 index 0000000..a4d9d7e --- /dev/null +++ b/exo6/ParentheseExp.java @@ -0,0 +1,12 @@ + +class ParentheseExp extends Facteur { + private Expression exp; + + ParentheseExp(Expression e) { + exp = e; + } + + public double evaluer() throws NotDigitException { + return exp.evaluer(); + } +} diff --git a/exo6/Variable.java b/exo6/Variable.java index 30b7830..807ec8d 100644 --- a/exo6/Variable.java +++ b/exo6/Variable.java @@ -1,24 +1,32 @@ import java.util.TreeMap; +import java.util.Map.Entry; class Variable extends Terme { - TreeMap v; + //TODO: TreeMap might look overkill + TreeMap v; - Variable(Character c) throws NotCharacterException { - v = new TreeMap(); - //TODO?: remove extragenous whitespace - if (Character.isLetterOrDigit(c)) { - v.put(c, 0.0); // we suppose the default variable value is 0 - } else { - throw new NotCharacterException(c + " is not a character type"); + Variable() { + v = new TreeMap(); + fill(); + } + + private void fill() { + int i = 0; + for (double d = -5.0; d <= 5.0; d = d + 0.25) { + v.put(i, d); + i++; } } - private boolean isVariable() { - //FIXME: this cover more than latin alphabet - return Character.isLetter(v.firstKey()); + public Entry getFirstEntry() { + return v.firstEntry(); + } + + public Integer size() { + return v.size(); } public double evaluer() { - return 0.0; + return v.pollFirstEntry().getValue(); } } diff --git a/exo6/design_arithmetic.pdf b/exo6/design_arithmetic.pdf new file mode 100644 index 0000000..0f1b33b Binary files /dev/null and b/exo6/design_arithmetic.pdf differ diff --git a/exo6/design_arithmetic.uxf b/exo6/design_arithmetic.uxf index 6e69bb0..8bcefa8 100644 --- a/exo6/design_arithmetic.uxf +++ b/exo6/design_arithmetic.uxf @@ -3,23 +3,23 @@ UMLClass - 590 - 60 - 100 + 580 + 90 + 140 60 /Expression/ -- -- -/+evaluer():/ +/+evaluer():double/ UMLClass - 380 - 200 + 390 + 230 100 30 @@ -29,19 +29,19 @@ UMLClass - 870 - 200 + 880 + 230 100 30 - /Op-add/ + /Opadd/ UMLClass - 280 - 290 + 290 + 320 100 30 @@ -51,81 +51,85 @@ UMLClass - 770 - 290 - 100 - 50 + 760 + 320 + 150 + 80 - Op+ + Opplus -- -- -+evaluer():gauche.evaluer() + droite.evaluer() ++evaluer(): +gauche.evaluer() + +droite.evaluer() UMLClass - 480 - 290 + 490 + 320 100 30 - /Op-mul/ + /Opmul/ UMLClass - 970 - 290 - 100 - 50 + 960 + 320 + 140 + 80 - Op- + Opminus -- -- -+evaluer():gauche.evaluer() - droite.evaluer() ++evaluer(): +gauche.evaluer() - +droite.evaluer() UMLClass 10 - 470 - 130 + 510 + 150 80 Chiffre -- -c:int <- {0,...,9} -- -+evaluer():int ++evaluer():double UMLClass - 170 - 470 - 180 + 190 + 510 + 220 80 Variable -- --vMap:Map<key, value> +-v:TreeMap<Integer,Double> -- -+evaluer():char ++evaluer():double UMLClass - 380 - 470 + 440 + 510 190 80 - Expression parenthesée + ParentheseExp -- -- +evaluer():exp.evaluer() @@ -134,36 +138,40 @@ UMLClass - 410 - 370 - 100 - 50 + 400 + 400 + 140 + 80 - Op* + Opmulti -- -- -+evaluer():gauche.evaluer() * droite.evaluer() ++evaluer(): +gauche.evaluer() * +droite.evaluer() UMLClass 550 - 370 - 100 - 50 + 400 + 140 + 80 - Op\ + Opdiv -- -- -+evaluer():gauche.evaluer() / droite.evaluer() ++evaluer(): +gauche.evaluer() / +droite.evaluer() Relation - 630 - 110 + 640 + 140 30 80 @@ -173,8 +181,8 @@ Relation - 420 - 160 + 430 + 190 520 60 @@ -184,20 +192,20 @@ Relation - 680 - 80 - 390 + 710 + 110 + 370 150 lt=<- droite - 10.0;10.0;330.0;10.0;330.0;130.0;290.0;130.0 + 10.0;10.0;310.0;10.0;310.0;130.0;270.0;130.0 Relation - 470 - 190 + 480 + 220 420 40 @@ -208,8 +216,8 @@ gauche Relation - 420 - 220 + 430 + 250 30 60 @@ -219,8 +227,8 @@ gauche Relation - 320 - 250 + 330 + 280 230 60 @@ -230,8 +238,8 @@ gauche Relation - 370 - 280 + 380 + 310 130 40 @@ -242,8 +250,8 @@ gauche Relation - 470 - 210 + 480 + 240 210 110 @@ -254,8 +262,8 @@ droite Relation - 910 - 220 + 920 + 250 30 60 @@ -265,8 +273,8 @@ droite Relation - 810 - 250 + 820 + 280 230 60 @@ -276,8 +284,8 @@ droite Relation - 520 - 310 + 530 + 340 30 60 @@ -287,8 +295,8 @@ droite Relation - 450 - 340 + 460 + 370 170 50 @@ -298,46 +306,46 @@ droite Relation - 560 - 60 - 580 - 470 + 620 + 90 + 530 + 480 lt=<- exp - 130.0;10.0;540.0;10.0;540.0;450.0;10.0;450.0 + 100.0;10.0;490.0;10.0;490.0;460.0;10.0;460.0 Relation - 60 - 430 - 430 - 60 + 70 + 480 + 480 + 50 lt=- - 10.0;40.0;10.0;10.0;410.0;10.0;410.0;40.0 + 10.0;30.0;10.0;10.0;460.0;10.0;460.0;30.0 Relation - 320 - 310 + 330 + 340 30 - 150 + 170 lt=<<- - 10.0;10.0;10.0;130.0 + 10.0;10.0;10.0;150.0 Relation - 250 - 430 + 290 + 480 30 - 60 + 50 lt=- - 10.0;10.0;10.0;40.0 + 10.0;10.0;10.0;30.0