Amazon.com Widgets Recent Posts
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 22, 2015, 01:27:53 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
|-+  Recent Posts
Pages: 1 2 [3] 4 5 ... 10

 21 
 on: May 04, 2015, 04:32:20 PM 
Started by HaoleJon - Last post by HaoleJon
So after an assist from BrianLawson, this is what I came up with.  I figured that if the origin x value for either rectangle fell on a line created by the width of either rectangle, and same for the y values and the height lines, then the triangles had to intersect.  Using this I built a series of else-ifs that, if satisfied, created a new rectangle with the appropriate origin, height and width.

The intersect method:

Code: (Objective-C)
-(void) intersect: (Rectangle *) rectangle;
{
    int setX;
    int setY;
    int setW;
    int setH;
   
    //check to see if the rectangle xPoint falls within the original rectangle xLine
    if (origin.x <= rectangle.origin.x && rectangle.origin.x <= (origin.x + _width)) {
        setX = rectangle.origin.x; //rectangle xPoint falls on xLine, sets new originX
        setW = (origin.x + _width) - rectangle.origin.x; //determines new width value
        }
        //since the rectangle xPoint did not work, does the origin xPoint fall within rectangle xLine?
        else if (rectangle.origin.x <= origin.x && origin.x <= rectangle.origin.x + (rectangle.width))
        {
        setX = origin.x;
        setW = (rectangle.origin.x + rectangle.width) - origin.x;
        }
        else { //if either origin does not fall on the other's line, sets X to -1 (not zereo, because a zero value would mean a tangent, which is technically an overlap
            setX = -1;
            setW = -1;
        }
    if (origin.y <= rectangle.origin.y && rectangle.origin.y <= (origin.y + _height)) {
        setY = rectangle.origin.y;
        setH = (origin.y + _height) - rectangle.origin.y;
       
    }
        else if (rectangle.origin.y <= origin.y && origin.y <= rectangle.origin.x + (rectangle.width)){
        setY = origin.y;
        setH = (rectangle.origin.y + rectangle.height) - origin.y;
    }
        else{//if either origin does not fall on the other's line, sets Y to -1 (not zereo, because a zero value would mean a tangent, which is technically an overlap
        setY = -1;
        setH = -1;
    }
    //Check to see if there is a -1 in either setX or setY
    if (setX > -1 && setY > -1) {//create new Rectagnle object if the x or y is 0 or positive
        Rectangle *intersectRectangle = [Rectangle new];
        XYPoint *intersectOrigin = [XYPoint new];
        [intersectOrigin setX: setX andY:setY];
        intersectRectangle.origin = intersectOrigin;
        [intersectRectangle setWidth: setW andHeight: setH];
        NSLog(@"The intersecting rectangle's origin is (%i, %i)", intersectRectangle.origin.x, intersectRectangle.origin.y);
        NSLog(@"The intersecting rectangle's width is %i and its height is %i.", intersectRectangle.width, intersectRectangle.height);
    }
    else //if the setX or setY is -1, then the rectangles do not overlap
        NSLog(@"The rectangles do not intersect.");
}


The main section:

Code: (Objective-C)
Rectangle *rectOne = [Rectangle new];
        XYPoint *rectOneOrig = [XYPoint new];
        [rectOneOrig setX:200 andY:420];
        rectOne.origin = rectOneOrig;
        [rectOne setWidth:250 andHeight:75];
       
        Rectangle *rectTwo = [Rectangle new];
        XYPoint *rectTwoOrig = [XYPoint new];
        [rectTwoOrig setX:400 andY:300];
        rectTwo.origin = rectTwoOrig;
        [rectTwo setWidth:100 andHeight:180];
        NSLog(@"Does rectangle two intersect rectangle one?");
        [rectOne intersect:rectTwo];
        NSLog(@"Does rectangle one intersect rectangle two?");
        [rectTwo intersect:rectOne];
       
        Rectangle *rectThree = [Rectangle new];
        XYPoint *threeOrig = [XYPoint new];
        [threeOrig setX:455 andY:422];
        rectThree.origin = threeOrig;
        [rectThree setWidth:5 andHeight:40];
        NSLog(@"Does rectangle three intersect rectagnle one?");
        [rectOne intersect:rectThree];
        NSLog(@"Does rectangle three intersect rectangle two?");
        [rectThree intersect:rectTwo];

The output:

2015-05-04 16:23:06.197 Rectangle[1109:75137] Does rectangle two intersect rectangle one?
2015-05-04 16:23:06.198 Rectangle[1109:75137] The intersecting rectangle's origin is (400, 420)
2015-05-04 16:23:06.198 Rectangle[1109:75137] The intersecting rectangle's width is 50 and its height is 60.
2015-05-04 16:23:06.198 Rectangle[1109:75137] Does rectangle one intersect rectangle two?
2015-05-04 16:23:06.198 Rectangle[1109:75137] The intersecting rectangle's origin is (400, 420)
2015-05-04 16:23:06.198 Rectangle[1109:75137] The intersecting rectangle's width is 50 and its height is 60.
2015-05-04 16:23:06.198 Rectangle[1109:75137] Does rectangle three intersect rectagnle one?
2015-05-04 16:23:06.199 Rectangle[1109:75137] The rectangles do not intersect.
2015-05-04 16:23:06.199 Rectangle[1109:75137] Does rectangle three intersect rectangle two?
2015-05-04 16:23:06.199 Rectangle[1109:75137] The intersecting rectangle's origin is (455, 422)
2015-05-04 16:23:06.199 Rectangle[1109:75137] The intersecting rectangle's width is 45 and its height is 58.

 22 
 on: May 04, 2015, 03:06:19 PM 
Started by HaoleJon - Last post by HaoleJon
Nope, and that makes perfect sense to do it that way.  Thanks.  I'll get a hang of this eventually.

 23 
 on: May 04, 2015, 03:01:29 PM 
Started by HaoleJon - Last post by BrianLawson
Did you try passing a pointer to a Rectangle object?
Code: (Objective-C)
-(void) intersect: (Rectangle *) rectangle

 24 
 on: May 04, 2015, 01:51:57 PM 
Started by HaoleJon - Last post by HaoleJon
I'm stuck on exercise 7 in chapter 8.  It says to write a Rectangle method called intersect: that takes a rectangle as an argument.  I must have missed something about passing objects as arguments, but I keep getting errors.  I tried
Code: (Objective-C)
-(void) intersect: (id) rectangle;

and
Code: (Objective-C)
-(void) intersect: (NSObject *) rectangle;

but when I try to access the rectangle, such as rectangle.origin.x, I get an error that "property not found on object of type '_strong ID'" or ".. object type 'NSObject'".  I can't find in the text what I'm supposed to do to pass the object as an argument in this type of case.

** Please don't post any full code, I'm still working out a good algorithm to calculate the new rectangle, I don't want any hints on that (yet). Thanks**

 25 
 on: May 02, 2015, 02:20:15 PM 
Started by Jackiepie - Last post by djt
I used the Boolean variable in a slightly different way to test for the end-of-program condition.

Extraneous code has been omitted. The program was written in C for the book "Programming in C, 4th edition."

Code: (Objective-C)

/* simple "printing" calculator exercise

*/
...

int main (...)
{
   ...
   bool exitFlag = false;

   ... ("Begin Calculations\n");  

   do {

      scanf ("%f %c", &number, &operator);

      switch (operator)
      {
         case '+':
            ...
         case '-':
            ...
         case '*':
            ...
         case '/':
            ...
         case 'S':
            ...
         case 'E':
         case 'e':
            exitFlag = true;
            break;
         default:
            ...
      }

      ... ("= %f\n", accumulator);

   }
   while ( ! exitFlag );

   ... ("End of Calculations.\n");  

   return 0;
}


The statement
while ( ! exitFlag );
is equivalent to
while ( exitFlag == false );

djt

 26 
 on: May 02, 2015, 09:51:45 AM 
Started by Jackiepie - Last post by Jackiepie
You can use Boolean Variables also.
The modified part.

Code: (Objective-C)
int main(int argc, char * argv[]){
    @autoreleasepool {
        double yourNum;
        char operator;
        BOOL isE;
        Calculator *deskCalc =[[Calculator alloc] init];
        
        NSLog(@"Type in your expression.");
        scanf("%lf %c", &yourNum, &operator);
        
        // Use Boolean Variables
        
        while (isE == NO)
        {
            switch (operator){
                case '+':
                    [deskCalc add: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '-':
                    [deskCalc subtract: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '*':
                case 'x':
                case 'X':
                    [deskCalc multipy: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '/':
                    [deskCalc divide: yourNum];
                    break;
                case 'S':
                case 's':
                    [deskCalc setAccumulator: yourNum];
                    NSLog(@"= %lf", yourNum);
                    break;
                default:
                    NSLog(@"Unknow operator.");
                    break;
            }
            
            // No need to use "case 'E'", When you enter the express with E, the loop will test first, and then teminated.
            
            scanf("%lf %c", &yourNum, &operator);  // Put this after the switch statement. You can have the situation that you enter "10 -/+/*/'/'" first, instead of "10 S/s".
            switch (operator) {  // Must stay behind scanf()
                case 'E':
                case 'e':
                    isE = YES;
                    break;
                    
                default:
                    break;
            }
        }
        
        NSLog(@"= %.2f", [deskCalc accumulator]);
        NSLog(@"End of Calculation.");
        
        
    }
    
    return 0;
}

 27 
 on: May 02, 2015, 09:46:54 AM 
Started by Jackiepie - Last post by Jackiepie
Code: (Objective-C)
//
//  main.m
//  6,4
//
//  Created by Jackie Lee on 15/5/2.
//  Copyright (c) 2015年 Jackie Lee. All rights reserved.
//
#import <Foundation/Foundation.h>

@interface Calculator : NSObject

-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multipy: (double) value;
-(void) divide: (double) value;

@end

@implementation Calculator
{
    double accumulator;
}

-(void) setAccumulator:(double)value
{
    accumulator = value;
}

-(void) clear
{
    accumulator = 0;
}

-(double) accumulator
{
    return accumulator;
}

-(void) add:(double)value
{
    accumulator += value;
}

-(void) subtract:(double)value
{
    accumulator -= value;
}

-(void) multipy:(double)value
{
    accumulator *= value;
}

-(void) divide:(double)value
{
    if (value == 0) {
        NSLog(@"Error!Division by zero.");
        NSLog(@"Please enter your expression again.");
    }
    else{
        accumulator /= value;
        NSLog(@"= %lf", accumulator);
}


@end

int main(int argc, char * argv[]){
    @autoreleasepool {
        double yourNum;
        char operator;
        Calculator *deskCalc =[[Calculator alloc] init];
        
        NSLog(@"Type in your expression.");
        scanf("%lf %c", &yourNum, &operator);

        // When you use "while (operator != 'E' || operator != 'e')", actualy, it will never be teminated.
        //Use '&&', Operator ≠ E & e at the same time, So the loop can continue. Operator = E or Operator = e, the loop will be teminated.
          while (operator != 'E' && operator != 'e'){
              switch (operator){
                case '+':
                    [deskCalc add: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '-':
                    [deskCalc subtract: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '*':
                case 'x':
                case 'X':
                    [deskCalc multipy: yourNum];
                    NSLog(@"= %lf", [deskCalc accumulator]);
                    break;
                case '/':
                    [deskCalc divide: yourNum];
                    break;
                case 'S':
                case 's':
                    [deskCalc setAccumulator: yourNum];
                    NSLog(@"= %lf", yourNum);
                    break;
                default:
                    NSLog(@"Unknow operator.");
                    break;
              }
              
            // No need to use "case 'E'", When you enter the express with E, the loop will test first, and then teminated.
              
                scanf("%lf %c", &yourNum, &operator);  // Put this after the switch statement. You can have the situation that you enter "10 -/+/*/'/'" first, instead of "10 S/s".
              
          }
            
            NSLog(@"= %lf", [deskCalc accumulator]);
            NSLog(@"End of Calculation.");
          
            
        }

    return 0;
}

 28 
 on: April 30, 2015, 07:21:21 AM 
Started by jonr - Last post by Jackiepie
Code: (Objective-C)
//
//  main.m
//  C6,3
//
//  Created by Jackie Lee on 15/4/30.
//  Copyright (c) 2015年 Jackie Lee. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Fraction: NSObject

-(void) setNumerator: (int) a;
-(void) setDenominator: (int) b;
-(void) print;

@end

@implementation Fraction
{
    int numerator;
    int denominator;
}

-(void) setNumerator:(int)a
{
    numerator = a;
}

-(void) setDenominator:(int)b
{
    denominator = b;
}

-(void) print // When denominator=1 or numerator=0, first NSLog display, otherwise the second one display.
{
    if (denominator == 1 || numerator == 0)
        NSLog(@"%i", numerator);
    else
        NSLog(@"%i/%i", numerator, denominator);
}

@end

int main(int argc, char * argv[]){
    @autoreleasepool {
        Fraction *aFrac = [[Fraction alloc] init];
        int a, b;
       
        NSLog(@"Type in your Numerator and Denominator");
        scanf("%i %i", &a, &b);
       
        [aFrac setNumerator: a];
        [aFrac setDenominator: b];
       
        NSLog(@"The value of aFrac is:");
        [aFrac print];
       
    }
    return 0;
}

 29 
 on: April 29, 2015, 06:24:21 PM 
Started by jonr - Last post by jonr
The concept of Categories in chap. 11 is easy enough to understand both conceptually and syntactically.  However, the chapter seemed to make a big point about how much easier they were to implement than using a subclass.  How so?  At this point I don't fully understand how using categories is superior to subclassing as far as ease of use goes.  Alto, it seems some of the traits of using categories are rather trivial: for instance, creating groups or categories of related methods.  I mean you could do that without categories by using comments and putting related methods together between the comment blocks.  What am I missing?  Thanks for any info.
Cheers,
jonR

 30 
 on: April 29, 2015, 02:24:22 PM 
Started by HaoleJon - Last post by BrianLawson
Ah, I see what you did. I misread the first condition of the test. Sorry about that. Smiley

Pages: 1 2 [3] 4 5 ... 10


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.