Amazon.com Widgets Chapter 4 Exercise 2
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 08:12:11 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
| | | |-+  Chapter 4 Exercise 2
Pages: [1]   Go Down
Print
Author Topic: Chapter 4 Exercise 2  (Read 1185 times)
adrianK
Newbie
*
Posts: 3


Email




« on: April 16, 2010, 08:05:23 AM »

Hello!
It first try for once!!

// Temperature  F, C

#import <Foundation/Foundation.h>

// Class section

@interface Degree: NSObject
{   
   float celsius;
}

// accumulator methode

-(void) setCelsius: (float) value;
-(void) clear;
-(float) celsius;


// arithmetic methode

-(void) fahrenheit: (float) value;
-(void) fahzero: (float) value;
-(void) result: (float) value;


@end

// Methods section

@implementation Degree

-(void) setCelsius: (float) value
{
   celsius = value;
}

-(void) clear
{
   celsius = 0;
}

-(float) celsius
{
   return celsius;
}

-(void) fahrenheit: (float) value
{
   celsius += value;
}

-(void) fahzero: (float) value
{
   celsius -= value;
}

-(void) result: (float) value
{
   celsius /= value;
}


@end
   
// Programming section
   
int main(int argc, const char *agrv[])
   {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      Degree *Temp;
      
      Temp = [[Degree alloc] init];
      
      [Temp clear];
      [Temp setCelsius: 0];
      [Temp fahrenheit: 27];
      [Temp fahzero: 32];
      [Temp result: 1.8];
      
      
      NSLog (@" (27:where? I don't know...) Fahenheit to Celsius: %g",
            [Temp celsius]);
      
      [Temp release];
      [pool drain];
      
      return 0;
}

NSLog (@" (27:where? I don't know...) Fahrenheit to Celsius: %g", [Temp celsius]);



[Session started at 2010-04-17 00:09:25 +0900.]
2010-04-17 00:09:25.470 Prog6[1484:10b]  (27:where? I don't know...) Fahrenheit to Celsius: -2.77778

The Debugger has exited with status 0.
« Last Edit: April 16, 2010, 08:13:59 AM by adrianK » Logged
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #1 on: April 16, 2010, 08:18:22 AM »

I think you've made the solution to this exercise overly-complex by modeling it after the Calculator class.   Other readers have also tackled this problem by defining a new class.

This problem can be more easily solved without defining a new class and by just evaluating the formula given in the exercise.   I think I'll state that in the next edition of the book!

Cheers,

Steve Kochan

Logged
adrianK
Newbie
*
Posts: 3


Email




« Reply #2 on: April 16, 2010, 09:53:44 AM »

Hello Steve.

Thanks for confirming. Smiley
Logged
jdelamater
Newbie
*
Posts: 3






« Reply #3 on: April 16, 2010, 10:04:57 AM »

adrian, if it helps you out at all, here's my solution...not saying it is the best way, just what i came up with.

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

@interface TempConvert : NSObject {
int farenheit;
float celcius;
}

-(void) setF: (int) value;
-(int) farenheit;
-(float) celcius;

@end

@implementation TempConvert

-(void) setF: (int) value {
farenheit = value;
}

-(int) farenheit {
return farenheit;
}

-(float) celcius {
return celcius = (float) (farenheit - 32) / 1.8;
}

@end


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

TempConvert *tempChange;
tempChange = [[TempConvert alloc] init];

[tempChange setF: 54];

NSLog (@" %iF is %fC", [tempChange farenheit], [tempChange celcius]);

[pool drain];
return 0;
}
Logged
adrianK
Newbie
*
Posts: 3


Email




« Reply #4 on: April 16, 2010, 10:49:30 AM »

Thank you! jdelamater.

Wow, a lot of sources has been reduced. I'll see. Cheesy
Logged
iLikeMyiPhone
Newbie
*
Posts: 12






« Reply #5 on: April 25, 2010, 07:19:19 PM »

I think Stephen was referring to this. I am glad my C basics are still in place, compiled int he first run!

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

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

    // insert code here...
//c = (f-32)/1.8
double c = 0, f = 62;
c = (f-32)/1.8;
NSLog(@"The value in Celsius is %g", c);
    [pool drain];
    return 0;
}
Logged
MBB
Newbie
*
Posts: 1







« Reply #6 on: December 02, 2010, 05:27:43 PM »

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

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

/* Write a program that converts 27˚ from degress Fahrenheit (F) to degrees Celcius (C)
using the following formula: c = (F-32) / 1.8 */

double f = 27, c;
c = (f - 32) / 1.8;

NSLog(@"27˚F converted to ˚C is %g", c);

    [pool drain];
    return 0;
}
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.