Amazon.com Widgets Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 01:24:52 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
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 4
| | | |-+ Exercise 2
Pages: [1] Go Down
Print
Author Topic: Exercise 2 (Read 1044 times)
John Shirley
Newbie
*
Posts: 47






on: June 25, 2009, 01:42:40 AM

Hi all..  Just done exercise 2 which should have been an easy one but seemed to have taken a while  Sad

This is my first attempt

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

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

float a = 27;
float b = 32;
float celsius;

celsius = (a - b)/1.8;   // convertion
NSLog (@"The temperature degrees F is: %f ", celsius);

    [pool drain];
    return 0;
}

Ok this is keeping it simple and when I looked here on the forum I got the feeling I was missing the point of  the exercise some what.  So I started it again.. Have to say I struggled a little with it and I am not sure if I am not getting the Classes, Methods thing and getting it all messed up.  After struggling on a little I came back to the forum to look at what others had done and to see where I was going wrong.  where I was struggling a little was the Interface section and Implementation section.  Think I might need to go back and read up on thoes 2 again..

If anyone can offer some advice/help on my program I would be very greatful

Thanks in advance

John

This is my code now

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

// Interface Section
@interface Tempconverter: NSObject
{
float Celsius;
float Fahrenheit;
}
-(float) Celsius;
-(float) Fahrenheit;
-(void) setFahrenheit: (float) f;
-(void) calculateCelsius;
@end

//  implementation section
@implementation Tempconverter
-(float) Fahrenheit
{
return Fahrenheit;
}

-(void) setFahrenheit: (float) f
{
Fahrenheit = f;
}
-(void) calculateCelsius
{
Celsius = (Fahrenheit - 32)/1.8;
}
-(float) Celsius
{
return Celsius;
}
@end
//  Program Section
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Tempconverter *mytempconvert =[[Tempconverter alloc] init];
[mytempconvert setFahrenheit: 27];
[mytempconvert calculateCelsius];
NSLog(@"Temp convertion =:%f", [mytempconvert Celsius]);
[mytempconvert release];
    [pool drain];
    return 0;
}

Last Edit: June 25, 2009, 03:47:28 AM by John Shirley Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: June 26, 2009, 10:01:18 AM

Yes, your first approach was fine.  The second approach, using classes, is certainly more ambitious.  I looked at your code.  It's fine.  I have no suggestions.

Cheers,

Steve Kochan
Logged
John Shirley
Newbie
*
Posts: 47






Reply #2 on: June 26, 2009, 11:05:59 AM

Thanks for taking the time to look over it

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.