kochan book

Discussion in 'Mac Programming' started by jamesapp, Mar 7, 2008.

  1. macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    does anyone know where i could get a list of exercises from a book:
    Programming in Objective-C by Stephen G. Kochan

    in the book they give a website that i found is not the one for the book.
    the website i tried was www.kochan-wood.com
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    There's a thread here about it. Looks like the site bit the dust.
     
  3. thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #3
    tried the links given.
    i need the exercises from the book.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Well if you're getting stuck at a certain point in the book, feel free to post here and we'll help you out.
     
  5. thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #5
    on page 178 of the kochan book it talks about an implementation file which i would call Complex.m. I have the interface file from the book.

    interface Complex.h
    {code}
    //Interface file for Complex class

    #import <objc/Object.h>

    @interface Complex: Object
    {
    double real;
    double imaginary;
    }

    -(void) print;
    -(void) setReal: (double) a;
    -(void) setImaginary: (double) b;
    -(void) setReal: (double) a andImaginary: (double) b;
    -(double) real;
    -(double) imaginary;
    -(Complex *) add: (Complex *) f;
    @end
    {code}

    and the book also includes the test program which i called prog9.1.m

    {code}
    //Shared Method Names: Polymorphism

    #import "Fraction.h"
    #import "Complex.h"

    int main (int argc, char *argv[])
    {
    Fraction *f1 = [[Fraction alloc] init];
    Fraction *f2 = [[Fraction alloc] init];
    Fraction *fracResult;
    Complex *c1 = [[Complex alloc] init];
    Complex *c2 = [[Complex alloc] init];
    Complex *comResult;

    [f1 setTo: 2 over: 5];
    [f2 setTo: 1 over: 4];

    [c1 setReal: 10.0 andImaginary: 2.5];
    [c2 setReal: -5.0 andImaginary: 3.2];

    //add and print 2 complex numbers

    [c1 print]; printf (" + "); [c2 print];
    printf (" = ");
    compResult = [c1 add: c2];
    [compResult print];
    printf ("\n");

    [c1 free];
    [c2 free];
    [compResult free];

    // add and print 2 fractions

    [f1 print]; printf (" + "); [f2 print];
    printf (" = ");
    fracResult = [f1 add: f2];
    [fracResult print];
    printf ("\n");

    [f1 free];
    [f2 free];
    [fracResult free];

    return 0;
    }
    {code}

    i just need the implementation file which i would call Complex.m in the book it says that you should have already written it in exercise 7 from Chapter 4.

    i am having trouble writing the implementation file
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Here's how the implementation (Complex.m) would look, but you'd need to fill in the methods:

    Code:
    #import "Complex.h"
    
    @implementation Complex
    
    -(void) print {
    }
    
    -(void) setReal: (double) a {
    }
    
    -(void) setImaginary: (double) b {
    }
    
    -(void) setReal: (double) a andImaginary: (double) b {
    }
    
    -(double) real {
    }
    
    -(double) imaginary {
    }
    
    -(Complex *) add: (Complex *) f {
    }
    
    @end
     
  7. thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #7
    question: how do you post code on these forums?
    question: is there a way to look at all the questions i have posted on this site?
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    Put your code in between [code] ... [/code] tags.

    You can subscribe to threads, automatically and/or manually. Go to your User CP to configure it and list the the threads you've subscribed to.
     
  9. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #9
    I usually just do an Advanced search and find Threads started by me
     
  10. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #10
    Found it.

    Attached you will find examples.tar and the answers.txt as captured by the Internet Archive for the companion website. I'll probably persist these to my own site too.
     

    Attached Files:

  11. thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #11
    i am wondering about an implementation file:
    i was given the implementation file and told to fill in the methods
    i will include what i came up with, i know it is wrong, and i haven't tried to compile it. i am reading from a book by Stephen G. Kochan and
    it talks about complex numbers, from the book "Complex numbers are numbers that contain two components:
    a real and an imaginary part, if a is the real component, and b is the imaginary component, the notation
    a + bi
    is used to represent the number."

    i think what the test program does is add not only two fractions but also two complex numbers like i said i never got the program to run here is what i have for the implementation file. i looked at the implementation for the fraction part of an implementation file for reference any help would be appreciated.

    Code:
    #import "Complex.h"
    
    @implementation
    
    -(void) print
    {
      printf (" % g" + "%gi",Real, imaginary);
    }
    -(void) setReal:(double) a
    {
      Real = a;
    }
    -(void) setimaginary:(double) b
    {
      imaginary = b;
    }
    -(void) setReal:(double) a andimaginary:(double)b
    {
      Real = a;
      imaginary = b;
    }
    
    -(double) real
    {
      real = a;
    }
    
    -(double) imaginary
    {
      imaginary = b;
    }
    
    -(Complex *) add: (Complex *) f
    {
      (a + bi) add: (a + bi)
    }
    
    @ end
    [code]
    
    again any help would be appreciated.
     
  12. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    Just curious, but why haven't you compiled the code yet? It'll point out your errors immediately and would help you learn faster, instead of just guessing.

    Code:
    -(Complex *) add: (Complex *) f
    {
    (a + bi) add: (a + bi)
    }
    The syntax is wrong here. The method add: should be returning a new Complex object. What you're doing is taking an existing Complex object, adding to it the values from another Complex object, and then returning the new object.

    Here's something to get you started:

    Code:
    -(Complex *) add: (Complex *) f
    {
        Complex *newComplex = [[[Complex alloc] init] autorelease];
        double newReal = ? ? ?
        double newImaginary = ? ? ?
        [newComplex setReal:newReal andimaginary:newImaginary];
        return newComplex;
    }
    Haven't tested this, but it should be what you need. You need to then fill in where the question marks are.
     
  13. macrumors 68000

    Joined:
    Nov 3, 2007
    #13
    I'm having a problem with the kochan book examples...

    Program 3.2

    Code:
    //
    //  main.m
    //  Chapter3-3.2
    //
    //  Created by Jeremy Curcio on 3/11/08.
    //  Copyright __MyCompanyName__ 2008. All rights reserved.
    //
    
    #import <stdio.h>
    #import <objc/Object.h>
    
    //Interface Section
    
    @interface Fraction: Object
    {
    	int numerator;
    	int denominator;
    }
    
    -(void) print;
    -(void) setNumerator: (int) n;
    -(void) setDenominator: (int) d;
    
    @end
    
    //Implement Section
    
    @implementation Fration;
    -(void) print
    {
    	printf (" %i/%i ", numerator, denominator);
    }
    
    -(void) setNumerator: (int) n
    {
    	numerator = n;
    }
    
    -(void) setDenominator: (int) d
    {
    	denominator = d;
    }
    
    @end
    
    //Program Section
    
    int main(int argc, char *argv[])
    {
    	Fraction *myFraction;
    	
    	//Create an instance of a Fraction
    	
    	myFraction = [Fraction alloc];
    	myFraction = [myFraction init];
    	
    	//Set fraction to 1/3
    	
    	[myFraction setNumerator: 1];
    	[myFraction setDenominator: 3];
    	
    	//Display the fraction
    	
    	printf ("The value of myFraction is:");
    	[myFraction print];
    	printf ("\n");
    	
    	[myFraction free];
    	
        return 0;
    }
    
    I am returning 4 error in the @implementation.

    All of which say that numerator and denominator aren't defined. Isn't that the point of the @interface section, to tell @implementation what to do?
     
  14. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
    Ahem. hint: spelling

    @implementation Fration;

    PS. You can download all the source examples from the second link in my signature.
     
  15. macrumors 68000

    Joined:
    Nov 3, 2007
    #15
    Thank you, I must have re-read my code 4-5 times before posting it. But I was focusing mainly on the spellings of numerator and denominator
     
  16. macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #16
    Exercises, errata, etc.

    Thanks for posting this. I moved the stuff here:

    http://web.mac.com/steve_kochan/My_Home/Objective-C.html

    but it's just the same as you have, plus the errata.

    Sorry for the inconvenience to everyone!

    Cheers,

    Steve Kochan
     
  17. macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #17
    I'm running into a problem with the 3.7 exercise. Here is my code:

    Code:
    #import <stdio.h>
    #import <objc/Object.h>
    
    @interface Point: Object 
    {
    int x;
    int y;
    }
    
    -(void) print;
    -(void) setX: (int) xVal;
    -(void) setY: (int) yVal;
    -(int) x;
    -(int) y;
    
    @end
    
    @implementation Point;
    -(void) print {
    
    printf("(%i, %i)", x, y);
    }
    
    -(void) setX: (int) xVal {
    x = xVal;
    }
    
    -(void) setY: (int) yVal {
    y = yVal;
    }
    
    -(int) x {
    return x;
    }
    
    -(int) y {
    return y;
    }
    
    @end
    
    int main(int argc, char *argv[])
    {
    	Point *point1 = [Point new];
    	Point *point2 = [Point new];
    	
    	[point1 setX: 0];
    	[point1 setY: 0];
    	[point2 setX: 3];
    	[point2 setY: 4];
    	
    	printf("The two points are ");
    	[point1 print];
    	printf(" and ");
    	[point2 print];
    	printf("\n");
    	
    	[point1 free];
    	[point2 free];
    	
    	return 0;
    }
    
    I'm getting error: 'Point' redeclared as different kind of symbol and error: redefinition of 'struct Point' around @interface. I even copied and pasted the answer Steve posted on his site and it gives the same two errors. Can anyone help? Thanks.
     
  18. macrumors 6502a

    Joined:
    Jul 2, 2006
    Location:
    Scotland
    #18
    Looks like Point is now a used elsewhere... Try renaming the class to MyPoint...

    Code:
    #import <stdio.h>
    #import <objc/Object.h>
    
    @interface MyPoint: Object 
    {
    int x;
    int y;
    }
    
    -(void) print;
    -(void) setX: (int) xVal;
    -(void) setY: (int) yVal;
    -(int) x;
    -(int) y;
    
    @end
    
    @implementation MyPoint;
    -(void) print {
    
    printf("(%i, %i)", x, y);
    }
    
    -(void) setX: (int) xVal {
    x = xVal;
    }
    
    -(void) setY: (int) yVal {
    y = yVal;
    }
    
    -(int) x {
    return x;
    }
    
    -(int) y {
    return y;
    }
    
    @end
    
    int main(int argc, char *argv[])
    {
    	MyPoint *point1 = [Point new];
    	MyPoint *point2 = [Point new];
    	
    	[point1 setX: 0];
    	[point1 setY: 0];
    	[point2 setX: 3];
    	[point2 setY: 4];
    	
    	printf("The two points are ");
    	[point1 print];
    	printf(" and ");
    	[point2 print];
    	printf("\n");
    	
    	[point1 free];
    	[point2 free];
    	
    	return 0;
    }
    
     
  19. macrumors 6502a

    kwjohns

    Joined:
    Jul 4, 2007
    #19
    That did it. Thanks!
     
  20. macrumors newbie

    Joined:
    Sep 18, 2006
    #20
    what about the even numbered exercises?!

    :confused: Almost every exercise I've had a problem with has been of the even numbered variety - are there only answers to the odd-numbered ones? If so thats... somewhat vexing to put it mildly.
     
  21. macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #21
    This has been a sticky issue. When the book first came out, I took a poll to see whether no answers, all answers, or alternate answers should be posted. The problem is that instructors want to be able to assign exercises from the text while self-learners want to check their answers to measure progress. The result is a compromise, admittedly imperfect.

    Regards,

    Steve Kochan
     
  22. macrumors regular

    Joined:
    Mar 22, 2008
    #22
    Version 2.0

    Steve, you should be writing an updated edition or a supplement (if even on lulu.com) to take into consideration the updates to the language (and IDE).

    I am sure many of us would be happy to even pre-order your book! :)
     
  23. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #23
  24. macrumors 6502

    Joined:
    Nov 19, 2002
    Location:
    Stuttgart, Germany
    #24
    so, that means that there are no answers to the even numbered exercises available, correct?
     
  25. macrumors regular

    Joined:
    Apr 1, 2006
    Location:
    California
    #25
    Yes, at least there are none available from me. Sorry.

    Cheers,

    Steve Kochan
     

Share This Page