views:

49

answers:

1

Project file here if you want to download: http://files.me.com/knyck2/918odc

So I am working on the book "programming in Objective c 2.0" and working on exercise 8.5, where you have to build a bunch of classes and subclass a homemade abstract class into a square class a triangle class and a circle class. you also have to calculate the area and perimiter/circumference through methods on each object and display it on nslog.

Everything is pretty much set, but some weird values are being returned when i display the size of each side from my triangle class and I'm not sure why. one of the sides display as 0.000000 and the other two display as these huge floats/doubles:

521556413499497987605515504756958465074645713473678706698073680750705671496313493175192637227720116636212033961634738517717834352497805734697302078530634879721347399811072.000000

and

661675923659048238438515844320261245751425513854584815925028483891542482488445146590620251584462848.000000

When i go to debug it appears as if all values assign correctly so i am at a loss as to why this is displaying so funkily.

Thanks for any help you can provide,

Nick

here's the main:

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Triangle.h"
#import "Circle.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Rectangle *myRect = [[Rectangle alloc] init];
    Circle    *myCircle = [[Circle alloc] init];
    Triangle *myTriangle = [[Triangle alloc] init];

    myRect.fillColor = 12345;
    myRect.filled = NO;
    myRect.lineColor = 29999;
    [myRect setWidth:15.3 andHeight:22.3];

    NSLog(@"the rectangle has a filled color of %i,line color of %i",
            myRect.fillColor, myRect.lineColor);

    if (myRect.filled == YES) 
        NSLog(@"The rectangle is also filled");
    else if (myRect.filled == NO) 
    NSLog(@"The rectangle is not filled");

    NSLog(@" rectangle %f, %f, area %f, perimeter %f",
           myRect.w , myRect.h, myRect.area, myRect.perimeter);                    

    myCircle.fillColor = 15555;
    myCircle.filled = NO;
    myCircle.lineColor = 32349;
    [myCircle setR:15.2];

    NSLog(@"the circle has a radius of %f ,color of %i, line color of %i",
           myCircle.r, myCircle.fillColor, myCircle.lineColor);

    NSLog(@"Also the circles area is %f and the circumference is %f",
            myCircle.area, myCircle.circumference  );


    myTriangle.fillColor = 71611;
    myTriangle.filled = NO;
    myTriangle.lineColor = 78998;
    [myTriangle setSide1:13];
    [myTriangle setSide2:19];
    [myTriangle setSide3: 27.5];

    NSLog(@"triangle sides %g, %g and %g."),  // << error
            myTriangle.side1 , myTriangle.side2 , myTriangle.side3 ;
    NSLog(@"triangle area %f, perimeter %f.",
            myTriangle.area, myTriangle.perimeter );
    NSLog(@"triangle fill color %i, line color %i",
            myTriangle.fillColor, myTriangle.lineColor);

    [myCircle release];
    [myRect release];
    [myTriangle release];

    [pool drain];
    return 0;

}

and here's the interface and implementation:

#import <Foundation/Foundation.h>
#import "GraphicObject.h"

@interface Triangle : GraphicObject 
{
    double side1;
    double side2;
    double side3;
}

@property double side1, side2, side3;

-(void) setSides: (double)s1: (double)s2 : (double)s3;
-(double) area;
-(double) perimeter;

@end

Triangle.m

#import "Triangle.h"
#import <math.h>

@implementation Triangle
@synthesize side1, side2, side3;

-(void) setSides: (double) s1: (double) s2:(double) s3
{
    side1 = s1; 
    side2 = s2;
    side3 = s3;
}

-(double) area
{
    return sqrt(  (side1 + side2 + side3) 
                * (side1 + side2 - side3)
                * (side2 + side3 - side1)
                * (side3 + side1 - side2) / 16);
}

-(double) perimeter
{
    return side1 + side2 + side3;
}
@end

also here is the super class, GraphicObject.h

#import <Foundation/Foundation.h>    
@interface GraphicObject : NSObject 
{
    int fillColor; 
    BOOL filled; 
    int lineColor; 
}

@property int fillColor, lineColor;
@property BOOL filled;
@end

GraphicObject.m

#import "GraphicObject.h"
@implementation GraphicObject
@synthesize fillColor, lineColor, filled;
@end
+3  A: 

The problem is this line:

NSLog(@"the triangle has the sides of sizes %g, %g and %g."), myTriangle.side1 , myTriangle.side2 , myTriangle.side3 ;

Using ) before the , means that your call to NSLog provides no values to substitute for those %g when formatting for output. It appears unintentional, as my old friend used to say "All errors stem from copy and paste.... all of them".

The fix is to move the ) to just before the ; like so:

NSLog(@"the triangle has the sides of sizes %g, %g and %g.", myTriangle.side1 , myTriangle.side2 , myTriangle.side3);
slf
doh, thank you, i think ive been looking at code too long, or is that even possible?
nickthedude
I admit I had a "wtf, that should work" moment looking at your code too
slf
thanks again, i just came back to this because I was running into the same issue today and realized i had the wrong identifier %i or %g instead of what I needed which was a double identifier %f etc. funny how two totally different issues caused a similar problem.Anyway just happy I figured that out!Thanks againNick
nickthedude