#2 required tasks

46 views
Skip to first unread message

Dave Kliman

unread,
Dec 16, 2011, 11:20:33 AM12/16/11
to iphone-appd...@googlegroups.com
uh oh...

my idea works great but technically i'm changing a public API

+ (double) runProgram:(id) program;

becomes

+(ReturnCookie) runProgram:(id) program;

where ReturnCookie is a c struct that includes a double and a BOOL for whether or not an error has occurred...

so do i have to think of another way to go about this and preserve the (double) in the API? or what do you think?  would I get points off?

-Dave


James

unread,
Dec 16, 2011, 11:25:22 AM12/16/11
to iPhone Application Development Auditors
Hi Dave, try returning type id and then back in the Controller, do
introspection on it. I think this is actually the Extra Credit in
assignment 2. Sounds like you're on the right path, though!

Regards,
James

Dave Kliman

unread,
Dec 16, 2011, 11:28:21 AM12/16/11
to iphone-appd...@googlegroups.com
when i tried returning type (id) i got into trouble... it gave me an error something like "this operation is not compatible with type id" when i tried to perform operations such as +/- etc.

this way i'm still using doubles and they are neatly in my struct ReturnCookie... it does work very nicely but it does violate the provision that i do not change the API of (double) runProgram:    :/

-Dave

--
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.
To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.



James

unread,
Dec 16, 2011, 11:32:46 AM12/16/11
to iPhone Application Development Auditors
Yes, in popOperandOffStack those will need to change to give
doubleValue. Something like:

if ([@"+" isEqualToString:op]) {
result = [[self popOperandOffStack:stack] doubleValue] +
[[self popOperandOffStack:stack] doubleValue];
}

Regards,
James

Dave Kliman

unread,
Dec 16, 2011, 1:16:15 PM12/16/11
to iphone-appd...@googlegroups.com
Ah. Ya I could do it like that but yet it still changes the API from (double) runProgram to (id) runProgram. Which breaks the first required task... :/
Sent from my iPhone
Steve Jobs 1955-2011 :-(

jonathan.nyc

unread,
Dec 26, 2011, 10:59:56 AM12/26/11
to iPhone Application Development Auditors
Throw an exception, which will allow you to preserve the API.


On Dec 16, 1:16 pm, Dave Kliman <dave.kli...@gmail.com> wrote:
> Ah. Ya I could do it like that but yet it still changes the API from (double) runProgram  to (id) runProgram. Which breaks the first required task... :/
> Sent from my iPhone
> Steve Jobs 1955-2011 :-(
>

Dave Kliman

unread,
Dec 26, 2011, 11:14:37 AM12/26/11
to iphone-appd...@googlegroups.com
that seems messy... oh well, for now i have my (ReturnCookie) api, especially considering that in the extra credit section he ironically suggests changing the runprogramwithvariables into type (id) which was my other choice but i liked the c-struct better... cleaner...

-Dave


jonathan.nyc

unread,
Dec 26, 2011, 12:55:48 PM12/26/11
to iPhone Application Development Auditors
Exception handling is part of this assignment. It is technically
impossible to complete one of the requirements of A2 without coding
for an exception.

It's an important skill to have, and it's a lot less messy than some
other Objective-C constructs.

Your own API may satisfy the functional requirements of the
assignment. But you'll learn a lot more if you comply with all the
coding requirements as well.

Dave Kliman

unread,
Dec 26, 2011, 1:04:40 PM12/26/11
to iphone-appd...@googlegroups.com
exception handling is in assignment 2?

ok i'll look into it i guess...

mean time... are you using the NSCopying protocol for your UNDO?

-Dave

jonathan.nyc

unread,
Dec 26, 2011, 4:20:40 PM12/26/11
to iPhone Application Development Auditors
Didn't use NSCopy for undo.

Dave Kliman

unread,
Dec 26, 2011, 5:37:02 PM12/26/11
to iphone-appd...@googlegroups.com
@jon

do you think keeping an array of copies of the brain is a bit lavish? i'm not sure what the memory usage of all that really would add up to but i'm not imagining much... i guess it would be more economical to just use the program stack but then when i do an all clear i can't just remove all the items from that...

-Dave

jonathan.nyc

unread,
Dec 27, 2011, 9:52:47 AM12/27/11
to iPhone Application Development Auditors
Dave, there's probably enough memory available to do that.

But, Paul encourages elegant and economical coding. An
unnecessary array with a few numbers and symbols can't do much
damage. If you're ever handling large amounts of data, audio, video,
etc. you will need write code that preserves the device's memory.

My own solution to undo was very simple. In this field, you
should be seeking the simplest solution possible for every task you
get.

Dave Kliman

unread,
Dec 27, 2011, 10:01:28 AM12/27/11
to iphone-appd...@googlegroups.com
Hm. Did you just use the program stack? If so how do you survive clears?

Also instead of try throw I just used if statements to detect and report errors.

Sent from my iPhone
Steve Jobs 1955-2011 :-(

Dave Kliman

unread,
Dec 27, 2011, 10:03:43 AM12/27/11
to iphone-appd...@googlegroups.com
I'm not sure how to implement the throw. Maybe I'll try today heh

Sent from my iPhone
Steve Jobs 1955-2011 :-(

jonathan.nyc

unread,
Dec 27, 2011, 12:34:34 PM12/27/11
to iPhone Application Development Auditors
You'll have to write code that is able to detect and report at runtime
that sin(cos(sqrt(1-x))) where x is positive, is erroneous.

That's a challenge, but it's worth doing.


On Dec 27, 10:01 am, Dave Kliman <dave.kli...@gmail.com> wrote:
> Hm. Did you just use the program stack? If so how do you survive clears?
>
> Also instead of try throw I just used if statements to detect and report errors.
>
> Sent from my iPhone
> Steve Jobs 1955-2011 :-(
>

Dave Kliman

unread,
Dec 27, 2011, 1:47:33 PM12/27/11
to iphone-appd...@googlegroups.com
i do have this code in the popOperandOffProgramStack: method. There is a similar type of if statement for / to report any divide by 0. It's like that because there really are only two possible errors that i can think of off the top of my head, such as divide by 0 and square root of a negative number. are there any other errors i should be looking out for? I suppose if i do the try/throw type thing, i can let the device find errors for me, but for one reason or another i'm uncomfortable even executing an error and letting it get that far.

// result is a c struct that has an operand type double and a BOOL for reporting an error.

        }  else if ([operation isEqualToString:@"sqrt"]) {
            double tempOperand = [self popOperandOffProgramStack:stack].operand;
            if (tempOperand>=0) {
                result.operand =sqrt(tempOperand);
            } else {
                result.gotMathError=YES;
            }

Paul did say we could change the api of runProgram:usingVariableValues so instead of type (id) i made it type (ResultCookie) which is that c struct i mentioned above.

so will the code i have there figure out that the string you gave me has an error? I guess it will get to that eventually, at which point everything comes to a halt and "Error" is displayed to the user. Did i miss anything there? i'm not sure... just a noob here plowing through.

-Dave

jonathan.nyc

unread,
Dec 27, 2011, 11:01:38 PM12/27/11
to iPhone Application Development Auditors
I see a potential flaw in your code, depending on what the rest of the
method looks like.

Have you tested it yet?

Dave Kliman

unread,
Dec 27, 2011, 11:07:39 PM12/27/11
to iphone-appd...@googlegroups.com, iPhone Application Development Auditors
Not in the context of assignment 2. But in assignment 1 I made it so it would display "error" if there was a divide by 0 or sqrt of a negative number. Im still just grasping the concepts of #2 as of today so it works as far as I've altered the calculator to do the recursive run program and popoperandoffprogramstack.

Sent from my iPhone
Steve Jobs 1955-2011 :-(

Dave Kliman

unread,
Dec 28, 2011, 12:24:35 AM12/28/11
to iphone-appd...@googlegroups.com
here it is in action...


-Dave

jonathan.nyc

unread,
Dec 28, 2011, 11:41:22 AM12/28/11
to iPhone Application Development Auditors
Have you tried the function I posted previously?
> ...
>
> read more »

Dave Kliman

unread,
Dec 28, 2011, 11:44:40 AM12/28/11
to iphone-appd...@googlegroups.com
could you paste it again?

while i'm here, why does this line crash?

NSLog(@"popOperandOffProgramStack Returning %@. Error condition? %@",result.operand,result.gotMathError);

result.operand and result.gotMathError are both defined by the time that executes, but it's in the class method popOperandOffProgramStack...


jonathan.nyc

unread,
Dec 28, 2011, 1:21:22 PM12/28/11
to iPhone Application Development Auditors
sin(cos(sqrt(1-x))) where x is positive

Check the types & format %@ match.
> ...
>
> read more »

Dave Kliman

unread,
Dec 28, 2011, 1:37:58 PM12/28/11
to iphone-appd...@googlegroups.com
that gets "Error" as long as x is greater than 1.

i have not implemented the variables yet... going to this afternoon (hopefully).. .so i manually entered 8 +/- sqrt which is about as far as i'd be able to get in your formula.

Duane Bender

unread,
Dec 29, 2011, 1:20:13 PM12/29/11
to iphone-appd...@googlegroups.com
You posted this:
 
NSLog(@"popOperandOffProgramStack Returning %@. Error condition? %@",result.operand,result.gotMathError);
 
This string format calls for two objects (the %@ placeholders), but your second parameter to that string (result.gotMathError) is a BOOL, not an object. Try replacing the second %@ with a %d.
 
Duane

Dave Kliman

unread,
Dec 29, 2011, 1:36:17 PM12/29/11
to iphone-appd...@googlegroups.com
oh, duh... hahah thanks for pointing that out...

since that i got rid of the whole c-struct just so that i could keep the runprogram(double) API and have a runprogram with variable values with (id)... although i will fondly look back on my c-struct calculator days...

-Dave

--
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.

To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.


sam

unread,
Jan 2, 2012, 8:41:34 AM1/2/12
to iPhone Application Development Auditors
Dave, Jonathan

The 'simple' version of my calculator has no error-detection code and
returned the popOperandOffStack and runProgram results as doubles as
per the original API, yet it successfully reports div/zero and
sqrt(-1) errors.

sin(cos(sqrt(1-x))), gives a 'nan' error for any x > 1 (I can't see
what's special about this function, it's going to fail anyway on the
innermost sqrt for x>1). Divide-by-zero returns as 'inf'. I am not
not checking valid input domains on div and sqrt functions (or any of
the others), the built-in C functions just deal with these errors and
those (error) values propagate through the calling chain
appropriately, ending up in the display.So I don't need to add any
code (in fact I removed some) to get basic error reporting. This is
great as regards economic coding. Am I missing something?

C's built-in error values: http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
(the doc mentions low-level exceptions as well, but we don't need to
use those)

Paul's code had this
if (divisor) result = [self
popOperandOffProgramStack:stack] / divisor;
To get basic error reporting, it is only necessary to remove the
safety check:
result = [self popOperandOffProgramStack:stack] / divisor;
When divisor is zero, result is 'inf' and this value ends up in the
display. Maybe this is bad coding practice, but it seems to work.

Here is my (unfinished) error-code-free version http://screencast.com/t/1pIaqw2JJ

Jonathan, you say that it is technically impossible to complete A2
without coding for an exception. Which requirement in A2 are you
referring to? I have now completed the assignment with all of the
functional and coding requirements and have not had to handle
exceptions. Do you mean we have to use @throw/@catch, NSException?

On Dec 28 2011, 6:21 pm, "jonathan.nyc" <jonathan....@gmail.com>
wrote:
> ...
>
> read more »

Dave Kliman

unread,
Jan 2, 2012, 10:27:24 AM1/2/12
to iphone-appd...@googlegroups.com
i think i had something like that at the very very beginning, but I wanted it to say "error" instead. users probably don't know what "nan" means in general... maybe there's something that can be done there.

while i'm here, maybe you've gotten proficient enough to answer my rotation question...

if i had let's say two views in a storyboard... and i wanted to switch to the other one with animation just like in apple's calculator... have you figured out how to do that?

I'm going to spend some time today on assignment 2... i haven't had time lately and i want to catch up with you guys...

-Dave

Duane Bender

unread,
Jan 3, 2012, 3:27:35 PM1/3/12
to iphone-appd...@googlegroups.com
Dave, open this Apple Developer document:


and search for the topic Creating an Alternate Landscape Interface. I think this is what you're looking for.

Duane

Dave Kliman

unread,
Jan 3, 2012, 3:32:20 PM1/3/12
to iphone-appd...@googlegroups.com
Duane,

Thanks! I'll check that out today :-) I'm so glad you smarter guys are around here to help me plod through heh

-Dave

--
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.

To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.


sam

unread,
Jan 3, 2012, 5:18:57 PM1/3/12
to iPhone Application Development Auditors
My latest version has a tailor-made error message for each condition -
but it is simply a case of checking the double value _after_ it has
been returned - using isnan()/isinf() - and changing the display to
any appropriate string ... no need for a struct or exception. My
display gets "ERROR (nan)" and "ERROR (inf)" which I suppose could be
changed to something more eloquent if I could think of it. It's true
that this approach will not supply any fine-grained analysis of
exactly which part of the function created the condition but that is
not a requirement of the task. At one point I did create an NSError
object to get some extra information out of the method, but in my
current version I don't seem to need it (it was a good learning
experience though). Jonathan seems to think exception-handling is a
coding requirement here, I am not so sure, still pondering that. I get
the impression we are not expected to know about exceptions and errors
at this stage in the course. I'll post something about this a little
later.

As regards the animation, I am not sure what the problem is. My calc
animates nicely on rotation without any special pleading. But I am not
switching xibs/views, just rearranging the button layout for the
orientation. As you are aiming to recreate the iPhone calc this could
get overly complex for you with all those extra buttons. Duane has
posted you a useful link in the Apple docs for switching views (and
controllers) for orientation. This looks somewhat beyond the scope of
what we are expected to do in A3. Required task 9 allows you to
complete the task with iPad rotation but ignoring iPhone rotation
(much simpler, the calc stays portrait in both modes). I'll keep you
informed if I come up with more, I may rethink my button-shifting...
> > Here is my (unfinished) error-code-free versionhttp://screencast.com/t/1pIaqw2JJ
> ...
>
> read more »

Dave Kliman

unread,
Jan 3, 2012, 5:58:30 PM1/3/12
to iphone-appd...@googlegroups.com
yeah I didn't use error handling... I did get rid of the whole struct thing though.

as for your undo, does it survive all clears? I had a version at first that would undo right to when you run the app, but for some reason that seemed too lavish, and it wasn't what was in the required task, so i simplified it to just undo to the latest clear all...

-Dave

--
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.
To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.



sam

unread,
Jan 3, 2012, 6:07:42 PM1/3/12
to iPhone Application Development Auditors
My undo undoes the stack until there's none left - it's final act is
to display a slightly inelegant 'stack empty' error (well, one more
click and it does a true clear). As you say that is all the task
requires. I suppose - as you were doing - you could accumulate a stack
of stacks but that might get more confusing for the user? More useful
could be a redo (it's very easy to undo further than you intended)
which should be fairly straightforward?

How is your parenthesis-tidying going?
> ...
>
> read more »

Dave Kliman

unread,
Jan 3, 2012, 7:02:30 PM1/3/12
to iphone-appd...@googlegroups.com
i look upon that paren thing with a bit of trepidation... i haven't attacked that yet heh... i just made a validOperations dictionary that has an NSString for the operation ("pi", "*", etc) keyed with an integer that identifies it as uniary, two-operand, etc... but it seems if i'm doing that, why can't i also have a string that describes the operation itself so i don't have to have all those if/then/else statements in popOperandOffStack? I ought to try to get faster at this without all the cogitation.

-Dave

--
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.
To post to this group, send email to iphone-appd...@googlegroups.com.
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en.



jonathan.nyc

unread,
Jan 3, 2012, 11:18:26 PM1/3/12
to iPhone Application Development Auditors
OK, it is possible to this without throwing an exception.

Still, I believe it is the most elegant means to handle invalid math
in the recursive procedure.

On Dec 26 2011, 12:55 pm, "jonathan.nyc" <jonathan....@gmail.com>
wrote:
> Exception handling is part of this assignment.  It is technically
> impossible to complete one of the requirements of A2 without coding
> for an exception.
>
> It's an important skill to have, and it's a lot less messy than some
> other Objective-C constructs.
>
> Your own API may satisfy the functional requirements of the
> assignment.  But you'll learn a lot more if you comply with all the
> coding requirements as well.
>
> On Dec 26, 11:14 am, Dave Kliman <dave.kli...@gmail.com> wrote:
>
>
>
>
>
>
>
> > that seems messy... oh well, for now i have my (ReturnCookie) api, especially considering that in the extra credit section he ironically suggests changing the runprogramwithvariables into type (id) which was my other choice but i liked the c-struct better... cleaner...
>
> > -Dave
>
> > On Dec 26, 2011, at 10:59 AM, jonathan.nyc wrote:
>
> > > Throw an exception, which will allow you to preserve the API.
>
> > > On Dec 16, 1:16 pm, Dave Kliman <dave.kli...@gmail.com> wrote:
> > >> Ah. Ya I could do it like that but yet it still changes the API from (double) runProgram  to (id) runProgram. Which breaks the first required task... :/
> > >> Sent from my iPhone
> > >> Steve Jobs 1955-2011 :-(
>
> > >> On Dec 16, 2011, at 11:32 AM, James <iphone...@11thdimension.com> wrote:
>
> > >>> Yes, in popOperandOffStack those will need to change to give
> > >>> doubleValue.  Something like:
>
> > >>>        if ([@"+" isEqualToString:op]) {
> > >>>            result = [[self popOperandOffStack:stack] doubleValue] +
> > >>>                     [[self popOperandOffStack:stack] doubleValue];
> > >>>        }
>
> > >>> Regards,
> > >>> James
>
> > >>> On Dec 16, 8:28 am, Dave Kliman <dave.kli...@gmail.com> wrote:
> > >>>> when i tried returning type (id) i got into trouble... it gave me an error something like "this operation is not compatible with type id" when i tried to perform operations such as +/- etc.
>
> > >>>> this way i'm still using doubles and they are neatly in my struct ReturnCookie... it does work very nicely but it does violate the provision that i do not change the API of (double) runProgram:    :/
>
> > >>>> -Dave
>
> > >>>> On Dec 16, 2011, at 11:25 AM, James wrote:
>
> > >>>>> Hi Dave, try returning type id and then back in the Controller, do
> > >>>>> introspection on it.  I think this is actually the Extra Credit in
> > >>>>> assignment 2.  Sounds like you're on the right path, though!
>
> > >>>>> Regards,
> > >>>>> James
>
> > >>>>> On Dec 16, 8:20 am, Dave Kliman <dave.kli...@gmail.com> wrote:
> > >>>>>> uh oh...
>
> > >>>>>> my idea works great but technically i'm changing a public API
>
> > >>>>>> + (double) runProgram:(id) program;
>
> > >>>>>> becomes
>
> > >>>>>> +(ReturnCookie) runProgram:(id) program;
>
> > >>>>>> where ReturnCookie is a c struct that includes a double and a BOOL for whether or not an error has occurred...
>
> > >>>>>> so do i have to think of another way to go about this and preserve the (double) in the API? or what do you think?  would I get points off?
>
> > >>>>>> -Dave
>

Dave Kliman

unread,
Jan 3, 2012, 11:34:09 PM1/3/12
to iphone-appd...@googlegroups.com
@jon that's where I do it at least... I suppose that if there were a lot of calculations, having a couple if statements all the way down there would probably not be so great, but at least for this one little app, the vast majority of time is spent waiting for the user to do something.

I wonder how much overhead there is to a try, catch block vs an if then block.. (assuming you're doing try/catch here)

@sam my undo will result, in the end, in a 0 being displayed and nothing on the program stack.

-Dave


sam

unread,
Jan 4, 2012, 12:00:33 AM1/4/12
to iPhone Application Development Auditors
In Obj-C, use of @throw/@catch and NSException is highly inefficient.
The general idea is that these are for tracking down development
crashes, the kind of stuff that should be eliminated before shipping a
product. NSError is preferred for routine runtime stuff...

In A3 we are going to need efficient code. We're going to be drawing
graphs that can be live-panned and zoomed, and this involves callling
your runProgram routines repeatedly for each dot on the graph in
realtime.

From Apple...
"Important: In many environments, use of exceptions is fairly
commonplace. For example, you might throw an exception to signal that
a routine could not execute normally—such as when a file is missing or
data could not be parsed correctly. Exceptions are resource-intensive
in Objective-C. You should not use exceptions for general flow-
control, or simply to signify errors. Instead you should use the
return value of a method or function to indicate that an error has
occurred, and provide information about the problem in an error
object. For more information, see Error Handling Programming Guide."
(Apple 'The Objective-C Programming Language' ,Chapter 10, Exception
Handling)
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocExceptionHandling.html

See also "a note on errors and exceptions"
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html%23//apple_ref/doc/uid/TP40001806-CH204-SW6
Reply all
Reply to author
Forward
0 new messages