Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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 http://www.kochan-wood.com
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
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
 

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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.
 

Attachments

  • examples.zip
    65.4 KB · Views: 153
  • Answers.txt
    45.6 KB · Views: 227

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

Jeremy1026

macrumors 68020
Nov 3, 2007
2,215
1,029
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?
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
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

macrumors 68020
Nov 3, 2007
2,215
1,029
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

macrumors regular
Apr 1, 2006
150
0
California
Exercises, errata, etc.

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

macrumors 6502a
Jul 4, 2007
700
12
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.
 

MacsAttack

macrumors 6502a
Jul 2, 2006
825
0
Scotland
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;
}
 

kwjohns

macrumors 6502a
Jul 4, 2007
700
12
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;
}

That did it. Thanks!
 

NumT

macrumors newbie
Sep 18, 2006
4
0
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.
 

skochan

macrumors regular
Apr 1, 2006
150
0
California
: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

macrumors regular
Mar 22, 2008
141
0
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! :)
 

csimmons

macrumors 6502
Nov 19, 2002
252
0
Stuttgart, Germany
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.