exo6: add the full implementation.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 15 Apr 2018 19:28:41 +0000 (21:28 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 15 Apr 2018 19:28:41 +0000 (21:28 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
14 files changed:
exo6/Chiffre.java
exo6/Expression.java
exo6/Main.java
exo6/Makefile
exo6/NotDigitException.java [new file with mode: 0644]
exo6/Opdiv.java
exo6/Opminus.java
exo6/Opmulti.java
exo6/Opplus.java
exo6/Parenthese.java [deleted file]
exo6/ParentheseExp.java [new file with mode: 0644]
exo6/Variable.java
exo6/design_arithmetic.pdf [new file with mode: 0644]
exo6/design_arithmetic.uxf

index be6f8816b2900b6ec765d8c1031cf2409955346b..a040ee9a04264bbe990170c86e8c7b87c86235d3 100644 (file)
@@ -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");
+        }
     }
 }
index dc8a85587212b2ed1715b17a9249b297b7ce29ef..22296ab2e26592bdcdfc457f4ff6a69f66125169 100644 (file)
@@ -1,5 +1,5 @@
 
 public abstract class Expression {
 
-    abstract double evaluer();
+    abstract double evaluer() throws NotDigitException;
 }
index 5bc0ec48b525547074f77f58efe40d1bb7bf2976..4b714afd155878022c7dffe15f78442d62286309 100644 (file)
@@ -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<Integer, Double> 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<Integer, Double> e = x.getFirstEntry();
+                System.out.println("x = y = z = " + e.getValue() + " :");
+                System.out.println(exp + " = " + ArithmeticExpression.evaluer());
+            }
+        }
+        catch (Exception e) {}
     }
 }
index d8399cfe08060f71ffa06f52d1f2cf7da31fe508..4c0ea480453b4aa80a4fcc369494ef1e4a1f2235 100644 (file)
@@ -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 (file)
index 0000000..0cd91fd
--- /dev/null
@@ -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
+    }
+
+}
index 0fe971828cba3ccc6cf59b6048d165344b65583e..dd85ebc8304ee0cfc077b6320d34c255b0cd7655 100644 (file)
@@ -5,7 +5,7 @@ class Opdiv extends Opmul {
         super(g, d);
     }
 
-    public double evaluer() {
+    public double evaluer() throws NotDigitException {
         return gauche.evaluer() / droite.evaluer();
     }
 }
index 9e5307adf175609aebbf6a70781e26eb7e2c6769..190cc881f2ae1c70e79ba547f064f80497f04a28 100644 (file)
@@ -5,7 +5,7 @@ class Opminus extends Opadd {
         super(g, d);
     }
 
-    public double evaluer() {
+    public double evaluer() throws NotDigitException {
         return gauche.evaluer() - droite.evaluer();
     }
 }
index fdb93fd94a809de81043d1fba4ee771310f8264c..13e7c812fda06f0a9f5f314a47e7a98b0237d4f2 100644 (file)
@@ -5,7 +5,7 @@ class Opmulti extends Opmul {
         super(g, d);
     }
 
-    public double evaluer() {
+    public double evaluer() throws NotDigitException {
         return gauche.evaluer() * droite.evaluer();
     }
 }
index 95b200b1c158056301f9a99315a46533899ef0e0..185e66b2174665b774dd3c78bd5a9bf6719171c0 100644 (file)
@@ -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 (file)
index 2be283b..0000000
+++ /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 (file)
index 0000000..a4d9d7e
--- /dev/null
@@ -0,0 +1,12 @@
+
+class ParentheseExp extends Facteur {
+    private Expression exp;
+
+    ParentheseExp(Expression e) {
+            exp = e;
+    }
+
+    public double evaluer() throws NotDigitException {
+        return exp.evaluer();
+    }
+}
index 30b7830a2b239517fcd0041c973229fe40c304ae..807ec8dceb6bc3e8bdeba59e730a0e137267c395 100644 (file)
@@ -1,24 +1,32 @@
 import java.util.TreeMap;
+import java.util.Map.Entry;
 
 class Variable extends Terme {
-    TreeMap<Character, Double> v;
+    //TODO: TreeMap might look overkill
+    TreeMap<Integer, Double> v;
 
-    Variable(Character c) throws NotCharacterException {
-        v = new TreeMap<Character, Double>();
-        //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<Integer, Double>();
+        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<Integer, Double> 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 (file)
index 0000000..0f1b33b
Binary files /dev/null and b/exo6/design_arithmetic.pdf differ
index 6e69bb0d494d1b66b0c713ab2c1623c7854a167d..8bcefa81bb6580de5c2655b6d53546c47983190f 100644 (file)
@@ -3,23 +3,23 @@
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>590</x>
-      <y>60</y>
-      <w>100</w>
+      <x>580</x>
+      <y>90</y>
+      <w>140</w>
       <h>60</h>
     </coordinates>
     <panel_attributes>/Expression/
 --
 --
-/+evaluer():/
+/+evaluer():double/
 </panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>380</x>
-      <y>200</y>
+      <x>390</x>
+      <y>230</y>
       <w>100</w>
       <h>30</h>
     </coordinates>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>870</x>
-      <y>200</y>
+      <x>880</x>
+      <y>230</y>
       <w>100</w>
       <h>30</h>
     </coordinates>
-    <panel_attributes>/Op-add/</panel_attributes>
+    <panel_attributes>/Opadd/</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>280</x>
-      <y>290</y>
+      <x>290</x>
+      <y>320</y>
       <w>100</w>
       <h>30</h>
     </coordinates>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>770</x>
-      <y>290</y>
-      <w>100</w>
-      <h>50</h>
+      <x>760</x>
+      <y>320</y>
+      <w>150</w>
+      <h>80</h>
     </coordinates>
-    <panel_attributes>Op+
+    <panel_attributes>Opplus
 --
 --
-+evaluer():gauche.evaluer() + droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() + 
+droite.evaluer()</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>480</x>
-      <y>290</y>
+      <x>490</x>
+      <y>320</y>
       <w>100</w>
       <h>30</h>
     </coordinates>
-    <panel_attributes>/Op-mul/</panel_attributes>
+    <panel_attributes>/Opmul/</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>970</x>
-      <y>290</y>
-      <w>100</w>
-      <h>50</h>
+      <x>960</x>
+      <y>320</y>
+      <w>140</w>
+      <h>80</h>
     </coordinates>
-    <panel_attributes>Op-
+    <panel_attributes>Opminus
 --
 --
-+evaluer():gauche.evaluer() - droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() - 
+droite.evaluer()</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
       <x>10</x>
-      <y>470</y>
-      <w>130</w>
+      <y>510</y>
+      <w>150</w>
       <h>80</h>
     </coordinates>
     <panel_attributes>Chiffre
 --
 -c:int &lt;- {0,...,9}
 --
-+evaluer():int </panel_attributes>
++evaluer():double</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>170</x>
-      <y>470</y>
-      <w>180</w>
+      <x>190</x>
+      <y>510</y>
+      <w>220</w>
       <h>80</h>
     </coordinates>
     <panel_attributes>Variable
 --
--vMap:Map&lt;key, value&gt;
+-v:TreeMap&lt;Integer,Double&gt;
 --
-+evaluer():char</panel_attributes>
++evaluer():double</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>380</x>
-      <y>470</y>
+      <x>440</x>
+      <y>510</y>
       <w>190</w>
       <h>80</h>
     </coordinates>
-    <panel_attributes>Expression parenthesée
+    <panel_attributes>ParentheseExp
 --
 --
 +evaluer():exp.evaluer()</panel_attributes>
   <element>
     <id>UMLClass</id>
     <coordinates>
-      <x>410</x>
-      <y>370</y>
-      <w>100</w>
-      <h>50</h>
+      <x>400</x>
+      <y>400</y>
+      <w>140</w>
+      <h>80</h>
     </coordinates>
-    <panel_attributes>Op*
+    <panel_attributes>Opmulti
 --
 --
-+evaluer():gauche.evaluer() * droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() *
+droite.evaluer()</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>UMLClass</id>
     <coordinates>
       <x>550</x>
-      <y>370</y>
-      <w>100</w>
-      <h>50</h>
+      <y>400</y>
+      <w>140</w>
+      <h>80</h>
     </coordinates>
-    <panel_attributes>Op\
+    <panel_attributes>Opdiv
 --
 --
-+evaluer():gauche.evaluer() / droite.evaluer()</panel_attributes>
++evaluer():
+gauche.evaluer() / 
+droite.evaluer()</panel_attributes>
     <additional_attributes/>
   </element>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>630</x>
-      <y>110</y>
+      <x>640</x>
+      <y>140</y>
       <w>30</w>
       <h>80</h>
     </coordinates>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>420</x>
-      <y>160</y>
+      <x>430</x>
+      <y>190</y>
       <w>520</w>
       <h>60</h>
     </coordinates>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>680</x>
-      <y>80</y>
-      <w>390</w>
+      <x>710</x>
+      <y>110</y>
+      <w>370</w>
       <h>150</h>
     </coordinates>
     <panel_attributes>lt=&lt;-
 droite</panel_attributes>
-    <additional_attributes>10.0;10.0;330.0;10.0;330.0;130.0;290.0;130.0</additional_attributes>
+    <additional_attributes>10.0;10.0;310.0;10.0;310.0;130.0;270.0;130.0</additional_attributes>
   </element>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>470</x>
-      <y>190</y>
+      <x>480</x>
+      <y>220</y>
       <w>420</w>
       <h>40</h>
     </coordinates>
@@ -208,8 +216,8 @@ gauche</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>420</x>
-      <y>220</y>
+      <x>430</x>
+      <y>250</y>
       <w>30</w>
       <h>60</h>
     </coordinates>
@@ -219,8 +227,8 @@ gauche</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>320</x>
-      <y>250</y>
+      <x>330</x>
+      <y>280</y>
       <w>230</w>
       <h>60</h>
     </coordinates>
@@ -230,8 +238,8 @@ gauche</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>370</x>
-      <y>280</y>
+      <x>380</x>
+      <y>310</y>
       <w>130</w>
       <h>40</h>
     </coordinates>
@@ -242,8 +250,8 @@ gauche</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>470</x>
-      <y>210</y>
+      <x>480</x>
+      <y>240</y>
       <w>210</w>
       <h>110</h>
     </coordinates>
@@ -254,8 +262,8 @@ droite</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>910</x>
-      <y>220</y>
+      <x>920</x>
+      <y>250</y>
       <w>30</w>
       <h>60</h>
     </coordinates>
@@ -265,8 +273,8 @@ droite</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>810</x>
-      <y>250</y>
+      <x>820</x>
+      <y>280</y>
       <w>230</w>
       <h>60</h>
     </coordinates>
@@ -276,8 +284,8 @@ droite</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>520</x>
-      <y>310</y>
+      <x>530</x>
+      <y>340</y>
       <w>30</w>
       <h>60</h>
     </coordinates>
@@ -287,8 +295,8 @@ droite</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>450</x>
-      <y>340</y>
+      <x>460</x>
+      <y>370</y>
       <w>170</w>
       <h>50</h>
     </coordinates>
@@ -298,46 +306,46 @@ droite</panel_attributes>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>560</x>
-      <y>60</y>
-      <w>580</w>
-      <h>470</h>
+      <x>620</x>
+      <y>90</y>
+      <w>530</w>
+      <h>480</h>
     </coordinates>
     <panel_attributes>lt=&lt;-
 exp</panel_attributes>
-    <additional_attributes>130.0;10.0;540.0;10.0;540.0;450.0;10.0;450.0</additional_attributes>
+    <additional_attributes>100.0;10.0;490.0;10.0;490.0;460.0;10.0;460.0</additional_attributes>
   </element>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>60</x>
-      <y>430</y>
-      <w>430</w>
-      <h>60</h>
+      <x>70</x>
+      <y>480</y>
+      <w>480</w>
+      <h>50</h>
     </coordinates>
     <panel_attributes>lt=-</panel_attributes>
-    <additional_attributes>10.0;40.0;10.0;10.0;410.0;10.0;410.0;40.0</additional_attributes>
+    <additional_attributes>10.0;30.0;10.0;10.0;460.0;10.0;460.0;30.0</additional_attributes>
   </element>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>320</x>
-      <y>310</y>
+      <x>330</x>
+      <y>340</y>
       <w>30</w>
-      <h>150</h>
+      <h>170</h>
     </coordinates>
     <panel_attributes>lt=&lt;&lt;-</panel_attributes>
-    <additional_attributes>10.0;10.0;10.0;130.0</additional_attributes>
+    <additional_attributes>10.0;10.0;10.0;150.0</additional_attributes>
   </element>
   <element>
     <id>Relation</id>
     <coordinates>
-      <x>250</x>
-      <y>430</y>
+      <x>290</x>
+      <y>480</y>
       <w>30</w>
-      <h>60</h>
+      <h>50</h>
     </coordinates>
     <panel_attributes>lt=-</panel_attributes>
-    <additional_attributes>10.0;10.0;10.0;40.0</additional_attributes>
+    <additional_attributes>10.0;10.0;10.0;30.0</additional_attributes>
   </element>
 </diagram>