Amazon.com Widgets Chapter 9 Exercise 3
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2014, 09:39:19 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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 9
| | | |-+ Chapter 9 Exercise 3
Poll
Question: Was this helpful?
Yes
No
Somewhat
I'm even more confused

Pages: [1] Go Down
Print
Author Topic: Chapter 9 Exercise 3 (Read 707 times)
clouded
Full Member
***
Posts: 123






on: May 23, 2012, 09:24:47 AM

Here's what I have on this one:

Code: (Objective-C)
// Chapter 9 Exercise 3
// Add a print method to your XYPoint class defined in Chapter 8.
// Have it display the point in the format (x, y). Then modify
// Program 9.2 to incorporate an XYPoint object. Have the modified
// program create an XYPoint object, set its value, assign it to
// the id variable dataValue, and then display its value.

//  main.m

#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Complex.h"
#import "XYPoint.h"

int main (int argc, char * argv[])
{
    @autoreleasepool {
        id dataValue;
        Fraction *f1 = [[Fraction alloc] init];
        Complex *c1 = [[Complex alloc] init];
        XYPoint *xyP1 = [[XYPoint alloc] init];
       
        [f1 setTo: 2 over: 5];
        [c1 setReal: 10.0 andImaginary: 2.5];
        [xyP1 setX: 9 andY: 3];
       
        // first dataValue gets a fraction
       
        dataValue = f1;
        [dataValue print: 1];
       
        // now dataValue gets a complex number
       
        dataValue = c1;
        [dataValue print];
       
        // now dataValue gets a point
       
        dataValue = xyP1;
        [dataValue print];
    }
    return 0;
}
Code: (Objective-C)
//  XYPoint.h

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject
@property float x, y;
-(void) print;
-(void) setX: (float) xVal andY: (float) yVal;
@end
Code: (Objective-C)
//  XYPoint.m

#import "XYPoint.h"

@implementation XYPoint
@synthesize x, y;
-(void) print
{
    NSLog(@"( %.0f , %.0f )", x, y);
}
-(void) setX: (float) xVal andY: (float) yVal
{
    x = xVal;
    y = yVal;
}
@end
Code: (Objective-C)
//  Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction : NSObject
@property int numerator, denominator;

-(void) print: (bool) b;
-(double) convertToNum;
-(void) setTo: (int) n over: (int) d;
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
-(void) negate;
-(void) reduce;
@end
Code: (Objective-C)
// Fraction.m

#import "Fraction.h"

@implementation Fraction
@synthesize numerator, denominator;

-(void) print: (bool) b
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator;
    result.denominator = denominator;
   
    if (numerator/denominator >= 1 || numerator/denominator <= -1) {
        if (result.numerator % result.denominator == 0) {
            NSLog(@"%i", result.numerator / result.denominator);
        } else if (result.numerator <= 0) {
            NSLog(@"%i %i/%i", result.numerator / result.denominator, -(result.numerator % result.denominator) , result.denominator);
        } else {
            NSLog(@"%i %i/%i", result.numerator / result.denominator, result.numerator % result.denominator , result.denominator);
        }
    } else if (b == 1) { //bool for forced reduction
        [result reduce];
        NSLog (@"%i/%i", result.numerator, result.denominator);
    } else {
        NSLog (@"%i/%i", numerator, denominator);
    }
}
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
-(void) setTo: (int) n over: (int) d
{
    numerator = n;
    denominator = d;
    if (d < 0) {
        [self negate];
    }
}
-(void) negate
{
    denominator *= -1;
    numerator *= -1;
}
-(Fraction *) add: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
   
    [result reduce];
    return result;
}
-(Fraction *) subtract: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator - denominator * f.numerator;
    result.denominator = denominator * f.denominator;
   
    [result reduce];
    return result;
}
-(Fraction *) multiply: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
   
    [result reduce];
    return result;
}
-(Fraction *) divide: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator;
    result.denominator = denominator * f.numerator;
   
    [result reduce];
    return result;
}
-(void) reduce
{
    int u = numerator;
    int v = denominator;
    int temp;
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
    numerator /= u;
    denominator /= u;
    if (denominator < 0) {
        [self negate];
    }
}
@end
Code: (Objective-C)
//  Complex.h

#import <Foundation/Foundation.h>

@interface Complex: NSObject
@property double real, imaginary;
-(void) print;
-(void) setReal: (double) a andImaginary: (double) b;
-(Complex *) add: (Complex *) f;
@end
Code: (Objective-C)
//  Complex.m

#import "Complex.h"

@implementation Complex
@synthesize real, imaginary;
-(void) print
{
    NSLog (@" %g + %gi ", real, imaginary);
}
-(void) setReal: (double) a andImaginary: (double) b
{
    real = a;
    imaginary = b;
}
-(Complex *) add: (Complex *) f
{
    Complex *result = [[Complex alloc] init];
    result.real = real + f.real;
    result.imaginary = imaginary + f.imaginary;
    return result;
}
@end

Output:

2/5
10 + 2.5i
( 9 , 3 )
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.