Improving the calculator

So, you have delivered the calculator to your customer - but as usual the customer after some hours wants more. In this case the first request is to be able to use the first operand also as the second operand - he wants a SAME button. So, 33 + SAME will be 66. This not very complicated -- lets look into our widget. We drag-and-drop digit to the free area at the bottom and rename it to Same. Then we have to set the parameter. In this case we use not a constant but the variable 'memory'.

We took the opportunity to make some minor changes to the layout of our example - as with any interface builder DM makes these things very easy. We tested it with 3333 * SAME - you can verify it by yourself. Save   the form and go !

Note that this extension was done without recompilation, without even touching our Eiffel class at all. Only the resource file has been changed. (Naturally, you can also start the application from the desktop.)

Now things become harder. The customer wants a real memory (with two buttons - store and load) and also a way to correct the last digit entered. This means that we have to extend our virtual machine.

-- extensions for customer xyz

	xyz_memory: INTEGER

	store_mem is

		do
			xyz_mem := indicator
		end

	get_mem is

		do
			indicator := xyz_mem
		end

	clear_digit is

		do
			indicator := indicator // 10

		end

We can see above the changes made to the virtual machine. We have introduced a new register, 'xyz_mem' and included a data path from the indicator to the register as well from the register to the indicator. Then we included a routine to remove the last digit from the indicator. We must now recompile our base application.

The interface builder now knows about the new features we have included and we can use them in order to perform the tasks our customer asked for.

First, lets implement the Memory. We drag-and-drop the new routines and write the new names. That's easy.

Then we want to display the content of the memory - just drag-and-drop xyz_mem to the calculator. It will open an edit field (and we should set the read-only flag!). Finally we produce the "clear last digit" button. The final result can be seen on the right. It's not perfect - but this is a common feature of all technical products.

We can reuse our calculator in all our future projects. All we need to know is the name of the resource file describing our calculator - and we must include our class CALC in the project where we want to use it. Then the "standard" sequence of calling it is:

a: CALC

!! a.make

That's all - we can then access the result - directly via a.indicator!

Read about our strategy to distinguish between application and virtual machine if you wonder why you cannot write code directly into the form (a common practice in other RAD tools)

Now we will polish the application - we "activate" the grid and position the buttons in a regular pattern. You would probably choose to do it differently ... please do!

We suggest that you modify the calculator in order to get a "classical" stack calculator. To do this you must modify the semantic (the virtual machine). Then you can add the additional enter key.


Copyright © Object Tools -- info@object-tools.com
Last updated: 2005/02/02 11:51:12