Assignment 2 fall2011 class methods & instance variables

139 views
Skip to first unread message

MobileDevVT

unread,
Feb 22, 2012, 9:37:12 AM2/22/12
to iPhone Application Development Auditors
Hi,
I am having a little bit of trouble with Assignment 2 of the Fall2011.
I am currently working on Task1.
So My initial idea was to create a NSSet of suported perations. The
set would consist of NSStrings it would contain *,/,+,-,sin,cos and
other supported operations.
I wanted to create a property for that NSSet called
supportedOperations. I would synthesize and create my own getter so I
can do lazy instantiation. Inside I would put all the supported
operations inside the set.

Then I realized that both methods from Task 1 are Class methods.
+ (double)runProgram:(id)program usingVariableValues:(NSDictionary
*)variableValues;
+ (NSSet *)variablesUsedInProgram:(id)program;

When I tried to call
[self supportedOperations]
I found out I cannot access my supportedOperations from within
runPorgram or variablesUsedInProgam methods because the are Class
methods and they don't have an instance.
The only thing that falls on my mind is to create a local variable
supportedOperations inside both methods. Is there something smarter I
could do?

8vius

unread,
Feb 22, 2012, 1:31:37 PM2/22/12
to iphone-appd...@googlegroups.com
If you just want to check if the operation is supported you can just create a class method that creates and NSSet within and checks if what is passed is one of your operations (this will come in handy, check hints). Your other option is fairly simple, simply create a class method that returns an NSSet with the operations you support. 

Though, I don't understand why exactly you are adding this property for task one, all you need is to add new API to your CalculatorBrain in order to support variables.


--
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.


MobileDevVT

unread,
Feb 22, 2012, 2:17:08 PM2/22/12
to iPhone Application Development Auditors
Hi 8ivus,
Thanks for taking time to help
1. My idea was to create
NSSet *supportedOperations = [[NSSet alloc]
initWithObjects:@"+",@"-",@"*",@"/",@"sin",@"cos",@"sqrt",@"π", nil];
In order to make method
+ (NSSet *)variablesUsedInProgram:(id)program;
work I have to go through all the elements from the program. If an
element is a number I just skip it. If an element is a string then i
check if my supportedOperations set contains this string. If it
contains this means my element contains an operations and I skip it
again. If supportedOperations does not contain the element then this
means I have found a variable. This is why I need a Set. I was
thinking it is a good practice create a property for everything even a
set like this.

2. My second question is regarding the same methid
+ (NSSet *)variablesUsedInProgram:(id)program;
last sentence of the Task 1 is : If the program has no variables
return nil from this method (not an empty set).
How do I do this? Lazy instantiation would be great for this. But
since I am inside Class Call I can't create a property.
First thing I did inside the method is
NSMutableSet *operatorsUsedInProgram = [[NSMutableSet alloc] init];
This creates an empty set. I cannot use it if I don't call alloc and
init.

8vius

unread,
Feb 22, 2012, 2:23:01 PM2/22/12
to iphone-appd...@googlegroups.com
Again, check the hints, they suggest doing an isOperation class method, like so:

+ (BOOL)isOperation:(NSString *)operation {

    NSSet *operationSet = [NSSet setWithObjects:@"+", @"-", @"*", @"/", @"cos", @"sin", @"sqrt", @"π", nil];

    return [operationSet containsObject:operation];

}


I have one to determine if it's a single, double or no operand operation, as well as to determine if it's a variable. Hope this helps.

8vius

unread,
Feb 22, 2012, 2:25:35 PM2/22/12
to iphone-appd...@googlegroups.com
Regarding your second question, it's quite simple, remember that variablesUsedInProgram is a class method as well, that receives your current program. You can check each element in the program and compare if it's a variable and add it to the set, after which you can count the elements in the set. If it has no elements simply return nil from the method.
Reply all
Reply to author
Forward
0 new messages