Multiplying strings and integers
Interpreters, Iteration 8
User Story
Your client does want to multiply strings and integers!
User Story #8: Interpreter evaluates a string-integer product.
A Hobbes program can be the multiplication of a string and an integer (in that order).
Examples:
code | evaluation |
---|---|
"hi! " * 5 |
"hi! hi! hi! hi! hi! " |
"abc" * 0 |
"" |
456 * "sticks" |
error: not allowed |
"two" * "three" |
error: not allowed |
Of course, this does not invalidate or change integer-integer multiplication.
Your Job
Implement this user story just for your object-oriented interpreter.
Use the same approach that you used for making the addition
operator in the previous iteration. Reuse the
OperatorAlgorithm
, but create a new
MultiplicationAlgorithm
class.
Do not neglect your refactoring duties. Play around with Extract Method and Rename (especially) to clean up your code and make it more readable. I can't emphasize the name issue too much:
``The first step towards wisdom is calling things by their right names.'' –Chinese proverb
I was very, very wise. The interface for implementing operators
is called "OperatorAlgorithm
". The implementation for
addition is called "AdditionAlgorithm
". Great
descriptive names!
Make use of Eclipse's Refactor -> Rename! Although, it won't get all of the names in the multiple-dispatch classes. You'll have to change them by hand, but that's what the tests are for: to find the names you have to change!
But I was very, very dumb. Not all operator algorithms want to
"add" expressions together. Calling the method "add()
"
is confusing (or will be when you start multiplying with it). So
rename it! Call it something like compute()
. In the
context of an "operator algorithm", the idea of "computing" will
make sense. After each renaming, re-run your tests to make sure you
haven't broken anything.
Whenever you come across a name in your code that is vague or an outright lie, change it immediately.