Amazon.com Widgets Exercise 4.6
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 21, 2014, 03:10:06 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
| |-+ Chapter 4
| | |-+ Exercise 4.6
Pages: [1] Go Down
Print
Author Topic: Exercise 4.6 (Read 1098 times)
Nir
Newbie
*
Posts: 27






on: February 10, 2013, 07:38:21 PM

is that good enough?

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

@interface Complex : NSObject

-(void) setReal: (double) a;
-(void) setImagin: (double) b;
-(void) print; //display as a + bi
-(double) getReal;
-(double) getImagin;

@end

@implementation Complex

{
    double real;
    double imagin;
}

-(void) setReal: (double) a // set the real number
{
    real = a;
}

-(void) setImagin: (double) b // set the imaginary number
{
    imagin = b;
}

-(void) print // print in the complex number format
{
    NSLog(@"%g + %gi", real, imagin);
}

-(double) getReal // getting the real number
{
    return real;
}

-(double) getImagin // getting the imaginary number
{
    return imagin;
}

@end

// the main program to preform

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

    @autoreleasepool {
       
       
        Complex *myComplex = [[Complex alloc] init];
       
        [myComplex setImagin:3.3];
        [myComplex setReal:5.5];
        [myComplex print];
               
       
    }
    return 0;
}

thanks in advance!
Logged
Coda50
Newbie
*
Posts: 16


Email




Reply #1 on: February 26, 2013, 11:19:24 AM

This is my 4.6, not much different than yours.  Before I start these, I'm lost as can be, but after I get it typed, it all makes sense!  Huh

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

-(void) setReal: (double) a;
-(void) setImaginary: (double) b;
-(double) real;
-(double) imaginary;
-(void) print;   // display as 'a + bi
@end

@implementation Complex
{
    double real;
    double imaginary;
}

-(void) setReal: (double) a
{
    real = a;
}
-(void) setImaginary:(double)b
{
    imaginary = b;
}
-(double) real;
{
    return real;
}
-(double) imaginary
{
    return imaginary;
}
-(void) print
{
    NSLog(@"%.2f + %gi", real, imaginary);
}

@end

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

    @autoreleasepool {
       
        Complex *myNumber = [[Complex alloc] init];
       
        [myNumber setReal: 2];
        [myNumber setImaginary:5];
       
        [myNumber print];
       
        }
    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.