Amazon.com Widgets Exercise 8.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 04:14:04 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
| |-+  Exercises
| | |-+  Chapter 8
| | | |-+  Exercise 8.5
Pages: [1]   Go Down
Print
Author Topic: Exercise 8.5  (Read 337 times)
littfam
Newbie
*
Posts: 8


Email




« on: September 26, 2012, 09:40:46 PM »

My Solution.  Feel free to critique.

Code: (Objective-C)
@interface XYPoint : NSObject

@property  float x, y, ax, ay, bx, by, cx, cy, area;


-(void) setX:(float)xVal andY:(float)yVal;



@end

#import "XYPoint.h"
@class Triangle;

@implementation XYPoint

@synthesize x, y, ax, ay, bx, by, cx, cy,area ;

-(void) setX:(float)xVal andY:(float)yVal
{
    x = xVal;
    y = yVal;
   
}

-(void) setAx:(float)axVal andAy:(float)ayVal;
{
    ax = axVal;
    ay = ayVal;
}

-(void) setBx:(float)bxVal andBy:(float)byVal;
{
    bx = bxVal;
    by = byVal;
   
}

-(void) setCx:(float)cxVal andCy:(float)cyVal;
{
    cx = cxVal;
    cy = cyVal;
}



@end

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


@interface Rectangle :  GraphicObject

@property CGFloat width, height, transx, transy;

-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) setWidth:(CGFloat) w  andHeight: (CGFloat) h;
-(void) translate: (XYPoint *) pt;
-(CGFloat) area;
-(CGFloat) perimeter;

@end

#import "Rectangle.h"
#import "XYPoint.h"

@implementation Rectangle


{
    XYPoint *origin, *translate;
}


@synthesize width, height, transx, transy;


-(void) setWidth:(CGFloat) w andHeight: (CGFloat) h
{
    width = w;
    height = h;
}

-(void)setOrigin:(XYPoint *)pt
{
    if (! origin)
        origin = [[XYPoint alloc] init];
   
    origin.x = pt.x;
    origin.y = pt.y;
}

-(void) translate:(XYPoint *)pt
{
    transx = origin.x + pt.x;
    transy = origin.y + pt.y;
}

-(CGFloat) area
{
    return width * height;
}

-(CGFloat) perimeter
{
    return (height + width) *2;
}

-(XYPoint *) origin
{
    return origin;
}

-(XYPoint *) translate
{
    return translate;
}


@end

#import <Foundation/Foundation.h>

@interface GraphicObject : NSObject

{
    int fillcolor;
    BOOL filled;
    int linecolor;
}

-(void) setFillColor:(int)acolor;
-(void) setLineColor:(int)acolor;
-(void) setFilled:(BOOL)isFilled;

-(int) fillColor;
-(int) lineColor;
-(BOOL) filled;

@end

#import "GraphicObject.h"

@implementation GraphicObject


-(void) setFillColor:(int)acolor;
{
    fillcolor = acolor;
}


-(void) setLineColor:(int)acolor;
{
    linecolor = acolor;
}


-(void) setFilled:(BOOL)isFilled;
{
    filled = isFilled;
}

-(int) fillColor;
{
    return fillcolor;
}

-(int) lineColor;
{
    return linecolor;
}

-(BOOL) filled;
{
    return filled;
}


@end

#import "GraphicObject.h"

@interface Circle : GraphicObject

@property float diameter, radius;

-(float) area;
-(float) circumference;
-(void) setDiameter:(float) d orRadius: (float) r;

@end

#import "Circle.h"

@implementation Circle

@synthesize diameter, radius;

-(void) setDiameter:(float)d orRadius:(float)r
{
    diameter = d;
    radius   = r;
   
    if (radius == 0)
        radius = diameter / 2;
   
}

-(float) area
{
    return M_PI * radius * radius;
}

-(float) circumference
{
    return 2 * M_PI * radius;
}


@end

#import "GraphicObject.h"
#import "XYPoint.h"

@interface Triangle : GraphicObject
@property  float  ax, ay, bx, by, cx, cy, x, y, area;

-(float) sideA;
-(float) sideB;
-(float) sideC;
-(float) perimeter;
-(float) triArea;

-(void) setAx:(float)axVal andAy:(float)ayVal;
-(void) setBx:(float)bxVal andBy:(float)byVal;
-(void) setCx:(float)cxVal andCy:(float)cyVal;

@end

#import "Triangle.h"

@implementation Triangle
@synthesize ax, ay, bx, by, cx, cy, x, y, area;


-(void) setAx:(float)axVal andAy:(float)ayVal;
{
    ax = axVal;
    ay = ayVal;
}

-(void) setBx:(float)bxVal andBy:(float)byVal;
{
    bx = bxVal;
    by = byVal;
   
}

-(void) setCx:(float)cxVal andCy:(float)cyVal;
{
    cx = cxVal;
    cy = cyVal;
}

-(float) sideA
{
    return  sqrt (ABS((ax - bx)*(ax - bx)) + (ay - by)*(ay - by));
}

-(float) sideB
{
    return  sqrt (ABS((bx - cx)*(bx - cx)) + (by - cy)*(by - cy));
}

-(float) sideC
{
    return  sqrt (ABS((cx - ax)*(cx - ax)) + (cy - ay)*(cy - ay));
}

-(float) perimeter

{
    return sqrt (ABS((ax - bx)*(ax - bx)) + (ay - by)*(ay - by))+sqrt (ABS((bx - cx)*(bx - cx)) + (by - cy)*(by - cy))+sqrt (ABS((cx - ax)*(cx - ax)) + (cy - ay)*(cy - ay));
}

-(float) triArea

{
    return ABS((ax * (by-cy) + bx * (cy - ay) + cx * (ay -by))/2);
}


@end

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


int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
        Rectangle *myRect   = [[Rectangle alloc] init];
        XYPoint   *myPoint  = [[XYPoint alloc] init];
        XYPoint   *vector   = [[XYPoint alloc] init];
        Circle    *myCirc   = [[Circle alloc] init];
        Triangle  *myTri    = [[Triangle alloc] init];
       
        [myPoint setX:100 andY:200];
        [vector  setX:10  andY:20];
        [myTri setAx:19 andAy:14];
        [myTri setBx:23 andBy:30];
        [myTri setCx:50 andCy:25];
        [myPoint setArea:200];
        [myRect setFillColor:3];
        [myRect setLineColor:1];
        [myRect setFilled:YES];
        [myRect setWidth:5 andHeight:8];
        [myCirc setDiameter:10 orRadius:5];   
         myRect.origin = myPoint;
       
        NSLog(@"Rectangle: w = %.2f, h = %.2f", myRect.width, myRect.height);
       
        NSLog(@"Origin at    %.2f,   %.2f", myRect.origin.x, myRect.origin.y);
       
        NSLog(@"Vector at     %.2f,    %.2f", vector.x, vector.y);
       
        NSLog(@"Translate at %.2f,   %.2f", vector.x + myRect.origin.x , vector.y + myRect.origin.y);
       
        NSLog(@"Area = %.2f, Perimeter = %.2f",
              [myRect area], [myRect perimeter]);
       
        NSLog(@"Area = %.2f  Circumference = %.2f", myCirc.area, myCirc.circumference);
       
        NSLog(@"Side A = %.2f",myTri.sideA);
       
        NSLog(@"Side B = %.2f",myTri.sideB);
       
        NSLog(@"Side C = %.2f",myTri.sideC);
       
        NSLog(@"Perimeter = %.2f",myTri.perimeter);
       
        NSLog(@"Area = %.2f",myTri.triArea);
           
        }
    return 0;
}
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.