Amazon.com Widgets Methods that require two arguments (Chapter 8 Exercise 7)
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 01:57:34 PM
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
| |-+  Chapter 8
| | |-+  Methods that require two arguments (Chapter 8 Exercise 7)
Pages: [1]   Go Down
Print
Author Topic: Methods that require two arguments (Chapter 8 Exercise 7)  (Read 398 times)
krislwright
Newbie
*
Posts: 7


Email




« on: May 20, 2012, 07:21:35 PM »

I'm having the most difficult time understanding how to set up my method for Exercise 7 (intersecting rectangles) in chapter 8. I know it's a conceptual problem that I'm hoping someone will be able to explain.

I'm sure everyone who has worked this problem knows that the idea is to write a method that will calculate the origin, width and height of a rectangle that is derived from the intersection of two other rectangles.

As I understand the problem, I have been working under the assumption that the method that should be written would take the two intersecting rectangles as an argument and return the area of intersection as the new rectangle.

However, from reviewing the way I've seen people working this problem, everyones method takes a form of this type:

Code: (Objective-C)
-(Rectangle *) intersect: (Rectangle *) rec2;

How does this take two rectangles as arguments? And if it doesn't, how the heck are you supposed to find the origin and dimensional information of an intersected rectangle using only one rectangle for an argument???

Please someone explain this to me. I don't want to progress further until I feel I completely understand taking multiple arguments for a method, as I'm sure it's going to be vital to my understanding of future concepts.
Logged
tomtom
Newbie
*
Posts: 10






« Reply #1 on: May 22, 2012, 08:33:46 AM »

Hey buddy, I feel your pain.  I have now just finally solved this exercise, to my immense satisfaction Cool  I purposefully avoid checking other people's answers though until completing the exercises myself; I think it's important to push yourself through the process and get those synapses firing.

Anyway, for your first question:
Quote
How does this [method] take two rectangles as arguments?

I had to remind myself of this too when I started this exercise.  The answer is, as I think you realise, that it doesn't take two arguments.  It just takes the one argument called, in your example, rec2 which is a Rectangle object.  However, the point that you're missing is that this is a method in your Rectangle class and, as such, it will have been called from one of the two Rectangle objects that you (should have) instantiated in your main program.  Perhaps you are thinking of it in terms of a function of your main program? Whereas it is actually a method of your Rectangle class.

In other words, the first Rectangle object - the receiver of the message - is taking the second Rectangle object into its own intersect method, to see if they have any intersection.  Thus there are two Rectangle objects with which the comparison can be performed.

Perhaps try looking at pp 137 (Operations on Fractions) and the self keyword on pp145 again.  I think that's where this was first covered.

I hope that helps  Smiley
« Last Edit: May 22, 2012, 08:44:47 AM by tomtom » Logged
krislwright
Newbie
*
Posts: 7


Email




« Reply #2 on: May 22, 2012, 02:13:07 PM »

Thanks,

After powering through some other exercises and chapters, I figured out that I was looking at the function in a skewed way. As soon as I started seeing a bunch of dot.operators that continued to use 'self' (as in self.origin.x) along with the second argument (as in rec2.origin.x), I realized that the function really was taking two rectangles to complete the function.

As to your first point, I do the exercises without looking, until I'm completely stuck, then I come here for help. I think that's the best way to learn the material.
Logged
tomtom
Newbie
*
Posts: 10






« Reply #3 on: May 25, 2012, 08:47:12 AM »

Quote
As to your first point, I do the exercises without looking, until I'm completely stuck, then I come here for help. I think that's the best way to learn the material.

Yes, me too :-)
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.