Assignment #2 Extra Task #1

Please note, this blog entry is from a previous course. You might want to check out the current one.

Enhance your application to show the user error conditions like divide by zero, square root of a negative number, and insufficient operands. One way to do this (and to get more experience with using id) might be to have runProgram:usingVariableValues: in CalculatorBrain return an id (instead of a double) which is an NSNumber with the result or an NSString with the description of an error if it encounters one. Then update your Controller to use introspection on the return value and display the appropriate thing to the end-user.

Mainly the API of the CalculatorBrain has to be changed from double to id:
Continue reading “Assignment #2 Extra Task #1”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Assignment #2 Task #4

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add an Undo button to your calculator. Hitting Undo when the user is in the middle of typing should take back the last digit or decimal point pressed until doing so would clear the display entirely at which point it should show the result of running the brain’s current program in the display (and now the user is clearly not in the middle of typing, so take care of that). Hitting Undo when the user is not in the middle of typing should remove the top item from the program stack in the brain and update the user- interface.

This task can be implemented with 1 method in your Controller (of about 5-6 lines of code, assuming you’ve factored out the updating of your user-interface into a single method somewhere) and 1 method (with 1 line of code) in your Model. If it’s taking much more than that, you might want to reconsider your approach.

The first part of this task has already been implemented in the previous assignment. So we have to adjust the backSpace method to achieve the rest. To receive the result of the last program when the last entry is cleared we just add

        [self updateCalculatorView];

Continue reading “Assignment #2 Task #4”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Assignment #2 Task #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

Update the user-interface of your Calculator to test all of the above.

a. Your UI should already have a UILabel which shows what has been sent to the brain. Change it to now always show the latest description of the program currently in the CalculatorBrain using your descriptionOfProgram:method. It should show the description without substituting variable values (obviously, since descriptionOfProgram: does not take a variable value dictionary as an argument).
b. Add a few variable buttons (e.g, x, y, foo). These are buttons that push a variable into the CalculatorBrain.
c. Change your calculator to update its display (as needed) by calling your new runProgram:usingVariableValues: method. The variable values dictionary it passes to this method should be a property in your Controller (let’s call it “testVariableValues”).
d. Add a UILabel to your UI whose contents are determined by iterating through all the variablesUsedInProgram: and displaying each with its current value from testVariableValues. Example display: “x = 5 y = 4.8 foo = 0”.
e. Add a few different “test” buttons which set testVariableValues to some preset testing values. One of them should set testVariableValues to nil. Don’t forget to update the rest of your UI when you change testVariableValues by pressing one of these test buttons. Make sure that your preset values are good edge-cases for testing (we are intentionally not telling you what to use since part of good programming is figuring out how to thoroughly test your application).

To solve #a just remove all occurrences of code where you have manipulated the history label from CalculatorViewControler.m and replace it by

self.history.text = [CalculatorBrain descriptionOfProgram:self.brain.program];

which will render removeEqualSignFromHistory obsolate, so you might want to remove this helper function, too.
Continue reading “Assignment #2 Task #3”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Assignment #2 Task #2

Please note, this blog entry is from a previous course. You might want to check out the current one.

Re-implement the descriptionOfProgram: method from the last lecture to display the passed program in a more user-friendly manner. Specifically …

a. It should display all single-operand operations using “function” notation. For example, 10 sqrt should display as sqrt(10).
b. It should display all multi-operand operations using “infix” notation if appropriate, else function notation. For example, 3 Enter 5 + should display as 3 + 5.
c. Any no-operand operations, like π, should appear unadorned. For example, π.
d. Variables (Required Task #1) should also appear unadorned. For example, x.

Any combination of operations, operands and variables should display properly.
Examples (E means “Enter key”) …

a. 3 E 5 E 6 E 7 + * – should display as 3 – (5 * (6 + 7)) or an even cleaner
output would be 3 – 5 * (6 + 7).
b. 3 E 5 + sqrt should display as sqrt(3 + 5).
c. 3 sqrt sqrt should display as sqrt(sqrt(3)).
d. 3 E 5 sqrt + should display as 3 + sqrt(5).
e. π r r * * should display as π * (r * r) or, even better, π * r * r.
f. a a * b b * + sqrt would be, at best, sqrt(a * a + b * b).

As you can see, you will have to use parentheses in your output to correctly display the program. For example, 3 E 5 + 6 * is not 3 + 5 * 6, it is (3 + 5) * 6. Try to keep extraneous parentheses to a minimum though (see Hints).

It might be that there are multiple things on the stack. If so, separate them by commas in the output with the top of the stack first, for example 3 E 5 E would display as “5, 3”. 3 E 5 + 6 E 7 * 9 sqrt would be “sqrt(9), 6 * 7, 3 + 5”.

Analog to the runProgram method we first have to check if we actually have a valid program. We use call descriptionOfTopOfStack which will work through the stack like popOperandOffStack already does. If after that there is something still on the stack we call descriptionOfProgram for the rest. To beautify the result we call stripUnneccessaryParenthesis.
Continue reading “Assignment #2 Task #2”

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail