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






on: February 24, 2012, 02:15:09 AM

Hi guys

Just looking for some feedback on my program.  The output seems to be fine but it looks a little different to what others have written.  Please comment on how I should improve my program if it should be written differently and if so why.

Thanks


#import <Foundation/Foundation.h>


//---- @interface section ----

@interface Complex : NSObject

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

@end

//----@implemetation section ----

@implementation Complex
{
    double real;
    double imaginary;
}

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

-(void) setReal:(double)a
{
    real = a;
}

-(void) setImaginary:(double)b
{
    imaginary = b;
}

-(double) real
{
    return real;
}

-(double) imaginary
{
    return imaginary;
}

@end

//---- program section ----


int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        Complex *myComplex = [[Complex alloc] init];
       
        Complex *myOtherComplex = [[Complex alloc] init];
       
        // Set real and imaginary
        [myComplex setReal:2];
        [myComplex setImaginary:5];
       
        [myOtherComplex setReal:5];
        [myOtherComplex setImaginary:7];
       
       
        // Display Complex instances
        [myComplex print];
       
        [myOtherComplex print];
       
    }
    return 0;
}

Logged
seerex
Full Member
***
Posts: 177


Email




Reply #1 on: February 24, 2012, 07:19:24 AM

It looks good, and the wonders of programming is the possibility to solve the same issue in 100 different ways, and neither one is "right" or "wrong". So i'd say you nailed it Smiley keep it up!

Obviously it can be "enhanced / extended" by having a method such as:
-(void) setReal:(double)a andImaginary:(double)b;

But i can't remember how far in the text this gets taught (having a method take several arguments), but you can try it out if you want Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
eesa1980
Newbie
*
Posts: 3






Reply #2 on: February 24, 2012, 07:58:20 AM

Thanks for responding Seerex.  I've been really confused about objective C as I am virtually a complete programming newbie. I've felt like giving up a few times so knowing I got that right is a big confidence booster for me Grin .

By the way, I have come across that way of writing the method before elsewhere but didn't have a clue what it ment until you mentioned it here in the way you did so thanks again!  I've amended my code below to include it.

#import <Foundation/Foundation.h>


//---- @interface section ----

@interface Complex : NSObject

-(void)     print; //display as a+bi
-(void)     setReal:(double)a andImaginary:(double)b;
-(double)   real;
-(double)   imaginary;

@end

//----@implemetation section ----

@implementation Complex
{
    double real;
    double imaginary;
}

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

-(void)     setReal:(double)a andImaginary:(double)b;
{
    real = a;

    imaginary = b;
}

-(double) real
{
    return real;
}

-(double) imaginary
{
    return imaginary;
}

@end

//---- program section ----


int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
        Complex *myComplex = [[Complex alloc] init];

       
        // Set real and imaginary
        [myComplex setReal:5 andImaginary:10];
       
       
        // Display code to test output
        [myComplex print];
       
    }
    return 0;
}
Last Edit: February 24, 2012, 09:43:58 AM by eesa1980 Logged
seerex
Full Member
***
Posts: 177


Email




Reply #3 on: February 24, 2012, 10:30:52 AM

Great job Smiley thats exactly what i meant. It cuts down your code by a few lines and looks "smoother" Smiley just be careful not to over-extend this, so you end up with methods like "setA:(int)a andB:(int)b andC(int)c andD(int)d andE(int)e andF(int)f" etc etc etc, coz then it will only get confusing Smiley but you will find your own "line" when you develop a need for it.

Don't give up! it's a hard road, and it took me 1 year in order to have my first app in the app store, even though it was a simple game it still took a year to learn the language, and then some of the cocoa stuff etc. It's a hard road, and i didn't have any experience with programming before i began either. Just keep going! You will get it eventually! If you feel too "stressed out" by it all, try to cut down the amount you read a day. For instance, i sat myself a goal - to read and do a little objective-c every single day (of course you can't do it EVERY day, but most of em). Some days i only read 2 pages, others 20, but i did a little every day, and that goal kept me going, until i eventually finished the book (or most of it) and there you go - then you know objective-c Smiley

Keep at it chap! The rewards are WAY bigger than the hurdle getting em!
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
moundmaggit
Newbie
*
Posts: 1


Email




Reply #4 on: March 21, 2012, 03:18:19 AM

Just a quick thanx to seerex for the keep at it words, moral support is always welcome, especially when you feel like giving up. Its always nice to read , thanx.
Logged
JackRabbit
Jr. Member
**
Posts: 54



Email




Reply #5 on: May 09, 2012, 06:02:04 AM

Hello eesa1980.

I notice that your program produces 2 complex numbers. Did you do it just for the exercise or did you have another reason?

And yes moundmaggit, seerex's words are good to read. I too need encouragement and it helps reading it even if the post isn't directed at me.

I did this exercise with and with a bit of work - succeeded. But it wasn't comfortable. It was the "complex number" thing. When I read the exercise, I thought "What is a complex number anyhow?"

So I went to Wikipedia and it told me that it was a hybrid of a real number and an imaginary number. (I thought ALL numbers were imaginary - but I won't get philosophical here.) It went on to say that a real number is along a number line. (re: the "new math" introduced in the 60's) OK! I can wrap my mind around that one. Then it spoiled it all by going into what an imaginary number is. Well that is where I got off the bus. It was too much for me.

The concern I am alluding to is this; will I need to know this sort of thing to be a successful Objective-C programmer? If so, that means I have to go into math refresher courses. And time wise - that would be a nightmare! I hope I am not drifting too far from the original topic of this part of the forum. I respect the focus that we are asked to adhere to. But I really need to air out this growing concern of mine.

JR
Last Edit: May 09, 2012, 06:04:46 AM by JackRabbit Logged
clouded
Full Member
***
Posts: 123






Reply #6 on: May 09, 2012, 09:31:47 AM

Here's my answer for Chapter 4 Exercise 6, looks a lot like eesa1980's solution with a few exceptions.

I ultimately thought that the exercise was meant to learn how to create/pass objects back and forth, so I didn't concentrate on the finding the 'i' part of the calculation if i entered a square root of -5 per say. That would take a program that would dissect the equation and decide in the proper 'i' placement in the print method through many calculations.

Code: (Objective-C)
// Chapter 4 Exercise 6.
// Complex numbers are numbers that contain two components:
// a real part and an imaginary part. If a is the real component
// and b is the imaginary component, this notation is used to
// represent the number:
//                      a+bi
//
// Write an Objective-C program that defines a new class called
// Complex. Following the paradigm established for the Fraction
// class, define the following methods for your new class:
//      -(void) setReal: (double) a;
//      -(void) setImaginary: (double) b;
//      -(void) print; // display as a + bi
//      -(double) real;
//      -(double) imaginary;
//
// Write a test program to test your new class and methods.

#include <Foundation/Foundation.h>

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

@implementation Complex
{
    double real, imaginary;
}

-(void) setReal: (double) a
{
    real = a;
}

-(void) setImaginary: (double) b
{
    imaginary = b;
}

-(void) print
{
    NSLog(@"%f + %fi", real, imaginary);
}

-(double) real
{
    return real;
}

-(double) imaginary
{
    return imaginary;
}
@end

int main(int argc, const char *argv[]) {
    
    @autoreleasepool {
        Complex *myComplex = [[Complex alloc]init];
        [myComplex setReal: 5];
        [myComplex setImaginary: 10];
        [myComplex print];
    }
    return 0;
}
Output:
5.000000 + 10.000000i
Last Edit: May 14, 2012, 06:49:20 PM by clouded Logged
littfam
Newbie
*
Posts: 8


Email




Reply #7 on: May 30, 2012, 10:52:20 AM

Hi everyone.  This is my first post in this forum.  I have just now reached the end of chap. 4, and here is my answer to exercise 4.6.  I'm just now beginning to get a feel for the basic frame of mind it takes to write O-C, and am starting to get a better feel of the syntax.  seerex:  I like the advice you gave about setting a daily goal of trying to read and learn something each day (or at least most days, camping and fishing are my worst enemies is this regard).  There is so much to learn here, and I am finding that even 10 minutes of reading or tinkering with this is "ok". Anyway, I'll be a frequent poster and reader around here, and am looking forward to learning from many of you that are more knowledgeable at this than I am.    



Code: (Objective-C)
//
//  main.m
//  Exercise 4.6
//

//  complex class

#import <Foundation/Foundation.h>

@interface Complex: NSObject

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

@end

@implementation Complex

{
    double real;
    double imaginary;
}

-(void) print

{
    NSLog(@"%g + %gi", real, imaginary);
}

-(void) setReal:(double)a

{
    real = a;
}

-(void) setImaginary:(double)b

{
    imaginary = b;
}

-(double) real

{
    return real;
}

-(double) imaginary

{
    return imaginary;
}

@end

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

    @autoreleasepool {
        
        
        Complex *myComplex = [[Complex  alloc] init];
        
        [myComplex setReal:10];
        [myComplex setImaginary:20];
        [myComplex print];
        
    
        
          
          
        
    }
    return 0;
}
Last Edit: May 31, 2012, 08:14:55 AM by littfam 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.