Amazon.com Widgets Exercise 6 Chapter 7
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 23, 2014, 12:48:26 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 7
| | | |-+ Exercise 6 Chapter 7
Pages: [1] Go Down
Print
Author Topic: Exercise 6 Chapter 7 (Read 945 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: January 23, 2012, 12:18:08 PM

Code: (Objective-C)
[code][code]#import <Foundation/Foundation.h>

@interface Complex : NSObject

@property double real, imaginary;

-(void) print;  // displays a + bi

-(Complex *) add: (Complex *) complexNum;

@end



Code: (Objective-C)
#import "Complex.h"

@implementation Complex

@synthesize real, imaginary;


-(void) print;
{
    NSLog(@"The complex number is %g+%gi",real, imaginary);
}

-(Complex *) add: (Complex *) complexNum;
{
    Complex *resultComplex = [[Complex alloc] init ];
    
    resultComplex.real = real + complexNum.real;
    resultComplex.imaginary = imaginary + complexNum.imaginary;

    return resultComplex;
}

@end

Code: (Objective-C)
#import "Complex.h"

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

    @autoreleasepool
    {
        
        Complex * inComplex1 = [[Complex alloc] init];
        Complex * inComplex2 = [[Complex alloc] init];
        Complex * resultComplex = [[Complex alloc] init];
        
        
        [inComplex1 setReal: 5.3];
        [inComplex1 setImaginary:7];
        
        [inComplex2 setReal: 2.7];
        [inComplex2 setImaginary: 4];
        
        resultComplex = [inComplex1 add: inComplex2];
        
        [resultComplex print];
        
        
        }
    return 0;
}

[/code][/code]
Last Edit: January 23, 2012, 12:19:48 PM by alexsom Logged
clouded
Full Member
***
Posts: 123






Reply #1 on: May 14, 2012, 08:55:07 PM

Here's my version of the assignment:

Code: (Objective-C)
//
//  main.m
//  Complex

#include <Foundation/Foundation.h>
#import "Complex.h"

int main(int argc, const char *argv[]) { 
   
    @autoreleasepool { 
        Complex *myComplex = [[Complex alloc]init];
        Complex *totalComplex = [[Complex alloc]init];
        Complex *yourComplex = [[Complex alloc]init];
       
        myComplex.real = 5.3; 
        myComplex.imaginary = 7; 
        [myComplex print];
        NSLog(@"+");
       
        yourComplex.real = 2.7; 
        yourComplex.imaginary = 4; 
        [yourComplex print];
        NSLog(@"=");
       
        totalComplex = [myComplex add: yourComplex];
        [totalComplex print];
    } 
    return 0; 
}

//  Complex.h

#include <Foundation/Foundation.h>

@interface Complex : NSObject   
@property double real, imaginary;
-(void) print;
-(Complex *) add: (Complex *) complexNum;
-(Complex *) subtract: (Complex *) complexNum;
-(Complex *) multiply: (Complex *) complexNum;
-(Complex *) divide: (Complex *) complexNum;
@end

//  Complex.m

#import "Complex.h"

@implementation Complex 
@synthesize real, imaginary;

-(void) print
{
    NSLog(@"%f + %fi", real, imaginary);
}
-(Complex *) add: (Complex *) complexNum
{
    Complex *result = [[Complex alloc]init];
    result.real = real + complexNum.real; 
    result.imaginary = imaginary + complexNum.imaginary;
    return result;
}
-(Complex *) subtract: (Complex *) complexNum
{
    Complex *result = [[Complex alloc]init];
    result.real = real - complexNum.real; 
    result.imaginary = imaginary - complexNum.imaginary;
    return result;
}
-(Complex *) multiply: (Complex *) complexNum
{
    Complex *result = [[Complex alloc]init];
    result.real = real * complexNum.real; 
    result.imaginary = imaginary * complexNum.imaginary;
    return result;
}
-(Complex *) divide: (Complex *) complexNum
{
    Complex *result = [[Complex alloc]init];
    result.real = real / complexNum.real; 
    result.imaginary = imaginary / complexNum.imaginary;
    return result;
}
@end

Output:

5.300000 + 7.000000i
+
2.700000 + 4.000000i
=
8.000000 + 11.000000i
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.