:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
![[LOGO]](http://igm.univ-mlv.fr/ens/resources/mlv.png) |
#7.en Interface and abstract class
|
Exercice 1 - Expressions Tree
The goal of this exercise is to build a parser of simple arithmetic expressions.
These expressions are represented in the form of trees.
All classes should be defined in the package
fr.umlv.calc if no package is specified.
The
OpOrValue class defines objects which can be either values
or operations (with a left and a right member).
-
Why is the constructor that takes 4 arguments declared private ?
-
What is the problem with the OpOrValue (int, OpOrValue, OpOrValue) constructor ?
Correct the problem.
-
Write a parse method that takes a Scanner as input
and create the corresponding expression tree knowing that the tree will be
given to the scanner using reverse Polish notation.
For example, instead of 2 + 3 - 4 , the reverse Polish notation is - + 2 3 4 .
Note: the parse method is naturally recursive: if the expression still contains symbols (and is well formed) then:
-
either the next symbol is an integer and just make it a leaf of the expression tree,
-
the next symbol is an operator and you have to call parse () twice to get the left and right children and combine them with the operator to make a new expression.
Finally, as a reminder, scanner.next () returns the next word, Integer.parseInt()
lets you know if it is an integer and it is possible to use the switch expression
(the one with arrows) on Strings in Java.
-
Move main to a new class Main
in the fr.umlv.calc.main package.
-
Write the display method of the expression tree so that the display
is done in the usual reading order.
Note: you will have to add parentheses!
-
Note that taking a Scanner as a parameter does not allow the method parse to be reused
if, for example, the expression to parse is stored in a List of String .
Which interface should we use instead of Scanner so that we can
call the parse method with a List or a Scanner .
Exercice 2 - Heritage tree
The
OpOrValue class is actually badly coded, because it is not object-coded.
Indeed, each object stores too much information and the distinction between
the value type and the operation type is done by a field and not by the
class itself.
-
Write the classes Expr , Value ,
Add and Sub having the same functionality as
the OpOrValue class, but object-coded.
In addition, modify the test class Main .
-
In which class should we put the parse method?
Modify it so that it works with the new classes.
-
Write a eval method calculating the result of the expression.
-
Talk about transforming the Expr class
in abstract class or in interface. Make the necessary changes.
-
Note that the Add and Sub classes have a lot
common code ... How to re-factor the code to put the common code in one place?
What should be the visibility of the different methods?
What should be the visibility of the introduced class?
Reminder: fields must always be private!
© Université de Marne-la-Vallée