PDA

View Full Version : kochan book

jamesapp
Mar 7, 2008, 12:20 PM
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

kainjow
Mar 7, 2008, 12:22 PM
There's a thread here (http://forums.macrumors.com/showthread.php?t=279095) about it. Looks like the site bit the dust.

jamesapp
Mar 7, 2008, 12:28 PM
tried the links given.
i need the exercises from the book.

kainjow
Mar 7, 2008, 12:32 PM
Well if you're getting stuck at a certain point in the book, feel free to post here and we'll help you out.

jamesapp
Mar 7, 2008, 01:21 PM
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

kainjow
Mar 7, 2008, 01:28 PM
Here's how the implementation (Complex.m) would look, but you'd need to fill in the methods:

#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

jamesapp
Mar 7, 2008, 01:40 PM
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?

kainjow
Mar 7, 2008, 01:47 PM
Put your code in between ... 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.

jeremy.king
Mar 7, 2008, 03:45 PM
question: is there a way to look at all the questions i have posted on this site?

I usually just do an Advanced search and find Threads started by me

jeremy.king
Mar 7, 2008, 03:54 PM
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.

jamesapp
Mar 10, 2008, 07:01 PM
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.

kainjow
Mar 10, 2008, 08:41 PM
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.

-(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:

-(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.

Jeremy1026
Mar 11, 2008, 11:26 AM
I'm having a problem with the kochan book examples...

Program 3.2

//
// main.m
// Chapter3-3.2
//
// Created by Jeremy Curcio on 3/11/08.
//

#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?

jeremy.king
Mar 11, 2008, 11:53 AM
I'm having a problem with the kochan book examples...

Program 3.2

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?

Ahem. hint: spelling

@implementation Fration;

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

Jeremy1026
Mar 11, 2008, 11:55 AM
Ahem. hint: spelling

@implementation Fration;

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

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

skochan
Mar 12, 2008, 06:37 PM
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.

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

kwjohns
Mar 15, 2008, 12:33 PM
I'm running into a problem with the 3.7 exercise. Here is my 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.

MacsAttack
Mar 15, 2008, 07:20 PM
Looks like Point is now a used elsewhere... Try renaming the class to MyPoint...

#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;
}

kwjohns
Mar 15, 2008, 11:08 PM
Looks like Point is now a used elsewhere... Try renaming the class to MyPoint...

#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;
}

That did it. Thanks!

NumT
Mar 18, 2008, 04:51 PM
: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.

skochan
Mar 21, 2008, 07:52 PM
: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.

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

zippyfly
Mar 26, 2008, 10:25 PM
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! :-)

jeremy.king
Mar 27, 2008, 09:33 AM
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! :-)

http://forums.macrumors.com/showpost.php?p=5120655&postcount=10 ;)

csimmons
Apr 30, 2008, 01:31 PM
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

so, that means that there are no answers to the even numbered exercises available, correct?

skochan
Apr 30, 2008, 05:20 PM
so, that means that there are no answers to the even numbered exercises available, correct?

Yes, at least there are none available from me. Sorry.

Cheers,

Steve Kochan