Amazon.com Widgets My version of excercise Two
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 04:31:24 PM
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
| | | |-+  My version of excercise Two
Pages: [1]   Go Down
Print
Author Topic: My version of excercise Two  (Read 578 times)
Spencer
Newbie
*
Posts: 9






« on: July 04, 2009, 12:04:10 AM »

Hi,

I was initially having trouble with setters and getters - but I think I'm getting the hang of it. Thanks to everyone who's put up their examples and commenting.

Please have a look at my attempt if you wouldn't mind. I'd be interested on comments of any kind.

Thanks,
Spencer.

//  --  Program to convert temperature from Celsius to Fahrenheit and vice versa

#import<Foundation/Foundation.h>

//  --  @interface section

@interface Converter: NSObject

{
   float Celsius;
   float Fahrenheit;
}

-(void)   setCelsius: (float) c;
-(void)   setFahrenheit: (float)f;
-(void)   convert_c_to_f;
-(void)   convert_f_to_c;
-(float)   Celsius;
-(float)   Fahrenheit;

@end

// --  @implementation section

@implementation Converter

-(void) setCelsius: (float) c
{
   Celsius = c;
}

-(void)   setFahrenheit: (float)f
{
   Fahrenheit = f;
}

-(void) convert_c_to_f                      //  --  Formula to convert Celsius to Fahrenheit
{                                 
   Fahrenheit = (Celsius * 9) / 5 + 32;                  //  --  Multiply Celsius by 9
}                                  //  --  Divide result by 5.
                                  //  --  Add 32.

-(void) convert_f_to_c                      //  --  Formula to convert Fahrenheit to Celsius
{
   Celsius = (Fahrenheit - 32) / 9 * 5;                  //  --  Subtract 32 from Fahrenheit figure.
}                                  //  --  Divide by 9.
                                  //  --  Multiply by 5.

-(float) Celsius
{
   return Celsius;
}

-(float) Fahrenheit
{
   return Fahrenheit;
}

@end

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

{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   Converter *my_Converter;
   
   my_Converter = [[Converter alloc] init];
   
   [my_Converter setCelsius:27];
   [my_Converter convert_c_to_f];
   
   NSLog(@"\n\n%f celsius is %f fahrenheit", [my_Converter Celsius], [my_Converter Fahrenheit]);
   
   [my_Converter setFahrenheit:50];
   [my_Converter convert_f_to_c];
   
   NSLog(@"\n\n%f fahrenheit is %f celsius", [my_Converter Fahrenheit], [my_Converter Celsius]);
   
   [my_Converter release];
   [pool drain];
   return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: July 04, 2009, 11:14:32 AM »

Spencer, looks good.  Glad it's starting to gel for you.

Cheers,

Steve Kochan
Logged
Spencer
Newbie
*
Posts: 9






« Reply #2 on: July 04, 2009, 04:42:20 PM »

Yay! Thanks Steve!

Cheers,
Spencer.
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.