As some of you may have seen me experimenting with a program converting from Fahrenheit to Celsius and visa versa in Objective-C based off of console input and output. I'm just doing this to better learn the language and I think its a good way to demonstrate some of the things I've been exposed to.
So, anyway, I am looking to incorporate both the Kelvin and Rankine scales into my program, a good suggestion I'd like to take up on proposed by dreamlax.
Now, up until this point, my program has functioned relatively well for converting between Fahrenheit and Celsius, but if I need to convert between these 4 different scales, I'm going to have to work at it a different way in my opinion.
First off, I need to provide the end-user with the choice of their starting temperature, the temperature they want to convert from.
In order to do this, I need to prompt them with an NSLog or printf statement instructing them to do this.
Now, I would rather have them type out Fahrenheit, Celsius, Kelvin or Rankine, based upon which ever one they would like, rather than 0-3. Now, I don't think the scanf is capable, but if I'm wrong, I'd like to know if it either has this functionality, or this is another method I can take advantage of to add this to my program.
Next, I'm kind of lost. I obviously would need to instruct them to pick their target temperature, in other words, the temperature they want to convert to.
I need some help on a recommendation on how to organize this in my program.. Should I use only 1 class? Also, I guess I could set it up this way:
string baseTemp
string targetTemp
NSLog(@"Please type Fahrenheit, Celsius, Kelvin or Rankine to select your base temperature");
scanf or some other method here("string format specifier here" &baseTemp);
NSLog(@"Please type Fahrenheit, Celsius, Kelvin or Rankine to select your target temperature");
scanf or some other method here("string format specifier here" &targetTemp);
Now, I need a decision maker to decide if Fahrenheit == baseTemp and Celsius == targetTemp, then set up a certain formula accordingly.
This could get tedious, and I first off need that special decision maker, or just another simpler method.
Sorry for the probably quite awkward code, I'm trying to use what I've learned thus far here.
Any suggestions, tweaks, ideas and anything in between is greatly appreciated!
Thanks!!
UPDATE UPDATE: UPDATE:
The code for the class.h, class.m and main.m program files contain the final code, at least for this project, which dreamlax worked with me greatly to help us both achieve the goal I had. Thanks again dreamlax!!
class.h:
#import <Cocoa/Cocoa.h>
@interface Temperature : NSObject
{
double kelvin;
}
//Instance methods to convert argument to kevin
-(void) setFahrenheitValue: (double) f;
-(void) setCelsiusValue: (double) c;
-(void) setKelvinValue: (double) k;
-(void) setRankineValue: (double) r;
//Instance methods for extracting the kelvin value using any scale
-(double) fahrenheitValue;
-(double) celsiusValue;
-(double) kelvinValue;
-(double) rankineValue;
@end
class.m:
#import "class.h"
@implementation Temperature;
-(void) setFahrenheitValue: (double) f
{
kelvin = ((f + 459.67)*(5.0/9.0));
}
-(void) setCelsiusValue: (double) c
{
kelvin = (c + 273.15);
}
-(void) setKelvinValue: (double) k
{
kelvin = k;
}
-(void) setRankineValue: (double) r
{
kelvin = (r*(5.0/9.0));
}
-(double) fahrenheitValue
{
return ((kelvin*(9.0/5.0)) - 459.67);
}
-(double) celsiusValue
{
return (kelvin - 273.15);
}
-(double) kelvinValue
{
return (kelvin);
}
-(double) rankineValue
{
return (kelvin * (9.0/5.0));
}
@end
main.m:
#import <Cocoa/Cocoa.h>
#import "class.h"
int main(int argc, char *argv[])
{
int result;
int prompt;
double sourceTemp;
printf("Please choose a source temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
result = scanf("%i", &prompt);
if (result != 1)
printf("I couldn't understand your input, I need only one number!");
else if (result == EOF)
printf("I apologize, I encountered an error when trying to read your input.");
else if (result == 1)
{
printf("\nNow, please enter the temperature you would like to convert:\n\n");
scanf("%lf", &sourceTemp);
Temperature *converter = [[Temperature alloc] init];
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
break;
}
printf("\nNow, please choose a target temperature scale:\n[1] Fahrenheit\n[2] Celsius\n[3] Kelvin\n[4] Rankine\n\n");
scanf("%i", &prompt);
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
printf("%lf degrees Fahrenheit\n", [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("%lf degrees Celsius\n", [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("%lf degrees Kelvin\n", [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("%lf degrees Rankine\n", [converter rankineValue]);
break;
}
}
}
Just an idea I'm trying to implement is to get the final prompt which displays the converted temperature to display like this:
Suppose the end-user selected Fahrenheit as his/her source temperature, wishing to convert 212 degrees into his/her target temperature scale of Celsius. The conversions should equal 100 degrees Celsius obviously, but I think the program would be better of displaying the result like this:
212 degrees Fahrenheit is 100 degrees Celsius.
Now, I've made the values that need to be replaced by variables in bold. I have the 212 and 100 values solved easily, because 100 has been there in the first place, and 212 can easily be remedied by replacing it with the string formatter of the sourceTemp variable.
Now, the Fahrenheit string is a bit different.
I have tried to establish something new in our original switch like so:
switch (prompt)
{
case 1:
//end-user chooses Fahrenheit
[converter setFahrenheitValue:sourceTemp];
sourceTempText = 1;
break;
case 2:
//end-user chooses Celsius
[converter setCelsiusValue:sourceTemp];
sourceTempText = 2;
break;
case 3:
//end-user chooses Kelvin
[converter setKelvinValue:sourceTemp];
sourceTempText = 3;
break;
case 4:
//end-user chooses Rankine
[converter setRankineValue:sourceTemp];
sourceTempText = 4;
break;
}
OK, so I have added to each different case, setting a new variable named sourceTempText to either 1-4, the same value that the end-user chose to pick his/her source temperature.
Now, here is how I tried to display the final prompt to the end-user with the final switch:
switch (prompt2)
{
case 1:
//end-user chooses Fahrenheit
printf("%lf degrees sourceTempText is %lf degrees Fahrenheit\n", sourceTemp, [converter fahrenheitValue]);
break;
case 2:
//end-user chooses Celsius
printf("%lf degrees sourceTempText is %lf degrees Celsius\n", sourceTemp, [converter celsiusValue]);
break;
case 3:
//end-user chooses Kelvin
printf("%lf degrees sourceTempText is %lf degrees Kelvin\n", sourceTemp, [converter kelvinValue]);
break;
case 4:
//end-user chooses Rankine
printf("%lf degrees sourceTempText is %lf degrees Rankine\n", sourceTemp, [converter rankineValue]);
break;
}
I am not sure now, if I can insert sourceTempText into the string like I have here, instead, maybe I have to use a string formatter, but I'm not sure. It should be an easy fix, I just wanted to throw it out here! :)
Hopefully the next project will be... GUI GUI GUI! xD