iOS Dev – Assignment #1

We provide you with a template for a calculator that already adds numbers (at least 1 and 2).
Take a close look at the implementation of this template and write the necessary code to have a fully working calculator.

Let’s have a look at the number buttons:

iOS Dev - Assignment #1 - Number Actions
iOS Dev – Assignment #1 – Number Actions


Button 1 and 2 are linked to the numberEntered: method of the ViewController class. Connect the other buttons (0, 3 .. 9) by displaying the nib file and the header file at the same time and Control-drag the buttons to the methods.

The numberEntered: method uses the tags of the buttons to know which button has been presses:

- (IBAction)numberEntered:(UIButton *)sender {
...
	else {
		self.numberTextField.text = [self.numberTextField.text
                    stringByAppendingFormat:@"%i", 
                    sender.tag];
	}
}

Only the tags of button 1 and 2 are currently set to 1 and 2, the other buttons are set to zero. Thus change the tags of the other buttons as well.

When you start testing now you will notice that 123456789 + 1 will equal to 123456792 which is a feature of using floats in the methods of the provided template …

You will also notice that the leading zero is not cleared when entering additional numbers. The code holds a feature to clear the text field using the variable textFieldShouldBeCleared. Set this variable at the appropriate positions:

- (void)viewDidLoad
{
    ...
    textFieldShouldBeCleared = YES;
}

- (IBAction)resultButtonPressed:(id)sender {
    ...
    textFieldShouldBeCleared = YES;
}

- (IBAction)numberEntered:(UIButton *)sender {
	if (textFieldShouldBeCleared) {
            ...
            if (sender.tag != 0)
                textFieldShouldBeCleared = NO;
	}
	...
}

- (IBAction)clearDisplay:(id)sender {
    ...
    textFieldShouldBeCleared = YES;
}

The comma button has no functionality at the moment. Create a new action method and link it to the button. If the text field is marked to be cleared display “0.”, otherwise check if there is already a comma and display it if necessary:

- (IBAction)commaPressed:(UIButton *)sender {
    if (textFieldShouldBeCleared) {
        self.numberTextField.text = @"0.";
        textFieldShouldBeCleared = NO;
    } else if ([self.numberTextField.text rangeOfString:@"."].location == NSNotFound) {
        self.numberTextField.text = [self.numberTextField.text stringByAppendingString:@"."];
    }
}

Concerning the operation buttons all but the multiply button are already connected to the operationButtonPressed action method. Do so now, and change its tag to 13 as the other ones are already set to 11, 12, and 14.

Create two new constants for multiplication and divisions (addition and subtraction are already in the code):

#define OP_MUL	13
#define OP_DIV	14

Finally add the code for those operations and repair it for the NOOP operation:

- (float)executeOperation:(char)operation withArgument:(float)firstArgument andSecondArgument:(float)secondArgument;
{
	switch (operation) {
		...
		case OP_MUL:
			return firstArgument * secondArgument;
		case OP_DIV:
 			if (!secondArgument) return NAN;
 			return firstArgument / secondArgument;
		default:
			return secondArgument;
			break;
	}
}
FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.