Reading the code, I can have a good/valid idea of what that code
should "express". In other words, I understand what you would like to do in general.
But the fact is that
you must have a clear vision of where these results (2, 3, etc...) should come from!
Take for example the first result "
The Total Of Statement is : 2". Why 2? Try to think ....
You have an Stm variable that references a CompoundStm object. Note here, the real object is a CompoundStm but you "see" it only as the more general Stm base type.
Speaking with words, you should say "hey Stm object, give me the number of statements!". The Stm class should declare an abstract method that must be implemented by all Stm concrete subclasses. The implementation in CompoundStm "knows" that, being a compound statement, it has 2 statements.
But note that in CompoundStm the two objects are of type Stm, so theoretically it's possible that a CompoundStm contains in turn other CompoundStm.
So the same request should go to both stm1 and stm2 until you find a "final" statement that doesn't contain other statements.
To be more clear, suppose a method getStatementsCount() in Stm:
- you invoke getStatementsCount() on s (Stm)
- the concrete implementation (CompoundStm) invokes getStatementsCount() on stm1 and then on stm2
- stm1, being a AssignStm, is not a container for other statements, and should return 1 for getStatementsCount()
- stm2, being a PrintStm, is not a container for other statements, and should return 1 for getStatementsCount()
- CompoundStm get 1 and 1, and so should return 2.
I hope to have explained (for my english
) the concept at high level. Try to think about what I said.