# conversions among Fahrenheit, Centigrade and Kelvin systems

Page 1 of 1

## 4 Replies - 3660 Views - Last Post: 28 February 2009 - 03:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=89726&amp;s=3ddef6cdeaa6d02c02f4bbe9cdce0e54&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mmccaule

Reputation: 0
• Posts: 5
• Joined: 28-February 09

# conversions among Fahrenheit, Centigrade and Kelvin systems

Posted 28 February 2009 - 03:07 PM

Here is what the error states:The code you submitted is incorrect . Can someone please explain what I did wrong in a that a dummy like me can understand.

Example Test Case

temperature + degrees C = convertTemp + degrees K

```class studentSubmission {
double convertTemperature(double temperature,
char fromSystem, char toSystem) {
double convertTemp = 0.0;
double degrees = 0;
switch(fromSystem) {
case 'F':
if (toSystem == 'F')
{
System.out.println("no conversion needed, Fahrenheit to fahrenheit requested");
convertTemp = temperature;
break;
}
if (toSystem == 'C')
{
convertTemp = 5 * (degrees - 32)/9;
System.out.println("temperature + degrees F =  + convertTemp + degrees C");
break;
}
if (toSystem == 'K')
{
convertTemp = ((degrees - 273) * (9/5)) + 32;
System.out.println("temperature + degrees F = + convertTemp + degrees K");
break;
}
case 'C':
if (toSystem == 'C')
{
convertTemp = temperature;
break;
}
if (toSystem == 'F')
{
convertTemp = (degrees - 32) * (5 / 9);
System.out.println("temperature + degrees C = convertTemp + degrees C");
convertTemp = temperature;
break;
}
if (toSystem == 'K')
{
convertTemp = (degrees - 273);
System.out.println("temperature + degrees C = convertTemp + degrees K");
convertTemp = temperature;
break;
}
case 'K':
if (toSystem == 'K')
{
System.out.println("no conversion needed, Kelvin to Kelvin requested");
convertTemp = temperature;
break;
}
if (toSystem == 'F')
{
convertTemp = (degrees - 32) * (5 / 9) + 273;
System.out.println("temperature + degrees K = convertTemp + degrees F");
convertTemp = temperature;
break;
}
if (toSystem == 'C')
{
convertTemp = degrees + 273;
System.out.println("temperature + degrees K = convertTemp + degrees C");
convertTemp = temperature;
break;
}
}
return convertTemp;
}
}

```

- Core

Is This A Good Question/Topic? 0

## Replies To: conversions among Fahrenheit, Centigrade and Kelvin systems

### #2 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: conversions among Fahrenheit, Centigrade and Kelvin systems

Posted 28 February 2009 - 03:13 PM

Quote

Here is what the error states:The code you submitted is incorrect

And where did you submit it?

Are you getting any errors from the program?

You can edit your post for this.

### #3 mmccaule

Reputation: 0
• Posts: 5
• Joined: 28-February 09

## Re: conversions among Fahrenheit, Centigrade and Kelvin systems

Posted 28 February 2009 - 03:23 PM

BigAnt, on 28 Feb, 2009 - 02:13 PM, said:

Quote

Here is what the error states:The code you submitted is incorrect

And where did you submit it?

Are you getting any errors from the program?

You can edit your post for this.

[/quote]
I submitted my code in "Gradiance Online Accelerated Learning". It formats the beginning code. I have to fill in the proper code for it to compile and run correctly. It told me that it compiled/built correctly but did not get the proper results. Here is what the problem states:

This method takes a temperature value, a character indicating the temperature system from which to convert and another character indicating the target temperature system. The method returns the value of the temperature in the target system.

The method handles conversions among Fahrenheit, Centigrade and Kelvin systems. These systems are represented by ''F'', ''C'' and ''K'' respectively. The formula to convert from Fahrenheit to Centigrade is: C = (F - 32) * (5/9). The formula to convert from Kelvin to Centigrade is: K = C + 273. From these two formulas, we can derive the other formulas as: C = K - 273; F = (C * (9/5)) + 32; K = ((F -32) * (5/9)) + 273; F = ((K - 273) * (9/5)) + 32.

The method uses the above formulas to do the temperature conversion. For instance, given inputs 20, ''C'', ''F'', the method returns 68.

The following is the method prototype for the method:
double convertTemperature(double temperature, char fromSystem, char toSystem)

If either fromSystem or toSystem is not ''F'', ''C'' or ''K'', then the method should return the temperature unchanged (that is the output value is the same as the input value).

class studentSubmission {
double convertTemperature(double temperature,
char fromSystem, char toSystem) {

```double convertTemp = 0.0;
double degrees = 0;
switch(fromSystem) {
case 'F':
if (toSystem == 'F')
{
System.out.println("no conversion needed, Fahrenheit to fahrenheit requested");
convertTemp = temperature;
break;
}
if (toSystem == 'C')
{
convertTemp = 5 * (degrees - 32)/9;
System.out.println("temperature + degrees F =  + convertTemp + degrees C");
break;
}
if (toSystem == 'K')
{
convertTemp = ((degrees - 273) * (9/5)) + 32;
System.out.println("temperature + degrees F = + convertTemp + degrees K");
break;
}
case 'C':
if (toSystem == 'C')
{
convertTemp = temperature;
break;
}
if (toSystem == 'F')
{
convertTemp = (degrees - 32) * (5 / 9);
System.out.println("temperature + degrees C = convertTemp + degrees C");
convertTemp = temperature;
break;
}
if (toSystem == 'K')
{
convertTemp = (degrees - 273);
System.out.println("temperature + degrees C = convertTemp + degrees K");
convertTemp = temperature;
break;
}
case 'K':
if (toSystem == 'K')
{
System.out.println("no conversion needed, Kelvin to Kelvin requested");
convertTemp = temperature;
break;
}
if (toSystem == 'F')
{
convertTemp = (degrees - 32) * (5 / 9) + 273;
System.out.println("temperature + degrees K = convertTemp + degrees F");
convertTemp = temperature;
break;
}
if (toSystem == 'C')
{
convertTemp = degrees + 273;
System.out.println("temperature + degrees K = convertTemp + degrees C");
convertTemp = temperature;
break;
}
}
return convertTemp;

```

[/quote]

### #4 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: conversions among Fahrenheit, Centigrade and Kelvin systems

Posted 28 February 2009 - 03:35 PM

Well the degrees variable is always zero, why do you need this, just use the temperature variable passed in and then there is no need for the degrees variable at all.

### #5 mmccaule

Reputation: 0
• Posts: 5
• Joined: 28-February 09

## Re: conversions among Fahrenheit, Centigrade and Kelvin systems

Posted 28 February 2009 - 03:45 PM

BigAnt, on 28 Feb, 2009 - 02:35 PM, said:

Well the degrees variable is always zero, why do you need this, just use the temperature variable passed in and then there is no need for the degrees variable at all.

I give it a shot thank you.