Amazon.com Widgets Exercise 3
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 12:25:50 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 21
| | | |-+ Exercise 3
Pages: [1] Go Down
Print
Author Topic: Exercise 3 (Read 912 times)
Brabbeldas
Newbie
*
Posts: 10






on: January 31, 2012, 05:00:26 AM

- Added now BOOL bError.

- Added new method: doError
Code: (Objective-C)
-(void) doError
{
    [self clickClear];
    display.text = @"error";
    bError = YES;
}

- Changed method processDigit
Code: (Objective-C)
-(void) processDigit:(int)digit
{
    [b]// Check if denominator is 0 //
    if ( (!isNumerator) && (digit == 0) )
    {
        [self doError];
        return;
    }[/b]
   
    // Process digit //
    if(isMinus){
        currentNumber = 0 - digit;
        isMinus = NO;
    }
    else
        currentNumber = currentNumber * 10 + digit;
   
    [displayString appendString:[NSString stringWithFormat:@"%i", digit]];
     display.text = displayString;
}

-Changed method clickDigit
Code: (Objective-C)
-(IBAction) clickDigit:(UIButton *)sender
{
    [b]if (bError)
        [self clickClear];[/b]
   
    int digit = sender.tag;
   
    [self processDigit: digit];
}

- Added a check for bError to various methods, like clickOver
Code: (Objective-C)
-(IBAction) clickOver
{
    if(!bError)
    {
        [self storeFracPart];
        isNumerator = NO;
        [displayString appendString:@"/"];
        display.text = displayString;
    }
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: January 31, 2012, 04:20:40 PM

Hi,

Thanks for posting.

Does your processDigit: method allow me to enter a fraction such as 1/10?

Cheers,

Steve Kochan
Logged
Brabbeldas
Newbie
*
Posts: 10






Reply #2 on: February 01, 2012, 06:33:42 AM

It did not Smiley

Here is the changed processDigit: method
Code: (Objective-C)
-(void) processDigit:(int)digit
{
    // Check if denominator is 0 //
    if ( (!isNumerator) && (digit == 0) && (currentNumber == 0) )
    {
        [self doError];
        return;
    }
   
    // Process digit //
    if(isMinus){
        currentNumber = 0 - digit;
        isMinus = NO;
    }
    else
        currentNumber = currentNumber * 10 + digit;
   
    [displayString appendString:[NSString stringWithFormat:@"%i", digit]];
     display.text = displayString;
}

I added a check to see if currentNumber equals 0.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: February 01, 2012, 11:43:05 AM

That's better.  However, I'm not sure you even need to check for zero in that method.   Let them type in 01/05 if they want to.  It might be good enough to just check for division by zero or for a zero denominator.  But now it's simply a subjective design choice rather than a program flaw.

Good work!

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.