# Temperature Conversion Problem

Page 1 of 1

## 11 Replies - 1468 Views - Last Post: 28 September 2011 - 07:57 AMRate 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=248623&amp;s=53673f49a5a9304cfca70eb3f134091c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

# Temperature Conversion Problem

Posted 26 September 2011 - 07:51 AM

My project is to write a Temperature class. In addition to converting between Celsius and Fahrenheit also include Kelvin. The class has read(), add(Temperature), subtract(Temperature), multiply(Temperature), divide(double), equals(Temperature), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Methods add, subtract, multiply, and divide all return a Temperature.
This is what I have so far:
```import java.util.Scanner;

public class Tempertaure {
private double tempertaure;

public Tempertaure()
{
tempertaure = 0;
}

public Tempertaure(double n)
{
tempertaure = n;
}

{
Scanner scan = new Scanner(System.in);
int n = 0;
n = scan.nextInt();
}
public double get()
{
return tempertaure;
}
public boolean equals(Tempertaure n)
{
return(this.tempertaure == n.tempertaure);
}

{
Tempertaure result  = new Tempertaure();
result.tempertaure = this.tempertaure + n;
return result;
}
public Tempertaure subtract(double n)
{
Tempertaure result = new Tempertaure();
result.tempertaure = this.tempertaure - n;
return result;
}
public Tempertaure multiply (double n)
{
Tempertaure result = new Tempertaure();
result.tempertaure = this.tempertaure * n;
return result;
}
public Tempertaure divide (double n)
{
Tempertaure result = new Tempertaure();
result.tempertaure = this.tempertaure/n;
return result;
}

public Tempertaure toKelvin(double n)
{
Tempertaure result = new Tempertaure();
result.tempertaure = (5/9) * (n - 32) + 273;
return result;

}
public Tempertaure toFahrenheit(double n)
{

}
public Tempertaure toCelsius(double n)
{
Tempertaure result = new Tempertaure();
result.tempertaure = (5/9) * (n - 32);
return result;
}
public String toString()
{
return (Integer.toString((int) tempertaure));
}

}

```

I know all the equations to convert the temperatures to one another. My problem is I don't know how to put them into the program because I could do this if it was only Celsius and Fahrenheit but because Kelvin was added I'm not sure how to fit all the equations into it. I also need a set method as well, but I'm not sure how to start that. Any hints would be greatly appreciated.

Thank you

Is This A Good Question/Topic? 0

## Replies To: Temperature Conversion Problem

### #2 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

## Re: Temperature Conversion Problem

Posted 26 September 2011 - 08:00 AM

I'm not sure what the class is actually supposed to do. Is it only to convert between temperatures, who or do you have to have Add, Subtract methods as well?

Are you supposed to have for example celsiusToFahrenheit, celsiusToKelvin... etc methods in this class?

I would think your set method would just take a parameter of a new temperature, and to set the field to that value.

<Also you spelled 'temperature' wrong (sorry it's just bugging me )>

This post has been edited by Ryano121: 26 September 2011 - 08:03 AM

### #3 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 26 September 2011 - 11:18 AM

my fault, i forgot to post the demo here it is:
```public class TemperatureDemo
{
public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(120.0, “C”);
Temperature temp2 = new Temperature(100, “F”);
Temperature temp3 = new Temperature(50.0, “C”);
Temperature temp4 = new Temperature(232.0, “K”);
Temperature tempAve = new Temperature(0.0, “C”);
// create array
// fill array with empty temperatures
// this will be discussed in class
System.out.println("Temp1 is " + temp1);
temp1 = temp2.toKelvin();
System.out.println("Temp1 to Kelvin is " + temp1);
if (temp1.equal(temp3))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
System.out.println("Temp1 is " + temp1);
System.out.println("Temp2 is " + temp2);
System.out.println("Temp3 is " + temp3);
System.out.println("Temp4 is " + temp4);
tempAve = tempAve.divide(4);
System.out.println("the average temperature is " + tempAve );
tempAve = getAverage(tempArray); // must write this method
System.out.println("the average of the array of temperatures is " + tempAve );
}
}

```

and yeah i just realized I spelt it wrong too lol

### #4 cmpshr

Reputation: 42
• Posts: 120
• Joined: 22-August 11

## Re: Temperature Conversion Problem

Posted 26 September 2011 - 11:20 AM

The easiest way is to always keep temperature in one of the unit like celsius
You might use a flag in the constructor top speciy with type you want

```static final int CELSIUS = 0, FAR = 1, KELVIN = 2;
public Tempertaure(double value, int unit)
{
if(unit == CELSIUS)
tempertaure = value;
else if(unit == FAR)
tempertaure = (5.0/9.0) * (value - 32);
else if(unit == KELVIN)
tempertaure = value + 273;
else
throw new IllegalArgumentException(unit + " is not a valid unit value");
}

double getCelsius() {
return tempertaure;
}

double getKelvin() {
return tempertaure + 273;
}

```

### #5 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 26 September 2011 - 01:24 PM

cmpshr, on 26 September 2011 - 11:20 AM, said:

The easiest way is to always keep temperature in one of the unit like celsius
You might use a flag in the constructor top speciy with type you want

```static final int CELSIUS = 0, FAR = 1, KELVIN = 2;
public Tempertaure(double value, int unit)
{
if(unit == CELSIUS)
tempertaure = value;
else if(unit == FAR)
tempertaure = (5.0/9.0) * (value - 32);
else if(unit == KELVIN)
tempertaure = value + 273;
else
throw new IllegalArgumentException(unit + " is not a valid unit value");
}

double getCelsius() {
return tempertaure;
}

double getKelvin() {
return tempertaure + 273;
}

```

We aren't suppose to use get methods we are only allowed to have the methods that are listed in the problem, so I'm not sure if our solution would work for the specified problem.

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Temperature Conversion Problem

Posted 26 September 2011 - 01:42 PM

It's not clear what you need help with. Do what you know and ask for help when you get stuck.

### #7 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 27 September 2011 - 04:58 PM

So with help from my professor I changed up my code and have come up with this. However I run into an error when trying to run the demo program.
```public class Temperature {

private int temperature;
private char scale;

public Temperature(int temperature, char scale)
{
this.temperature=temperature;
this.scale=scale;
}

public Temperature toKelvin()
{
int converted_Temp;

if(scale == 'C')
{
converted_Temp=temperature+273;
Temperature temp = new Temperature(converted_Temp, 'K');
return temp;
}

if(scale == 'F')
{
converted_Temp=(int)((temperature+459.67)*5.0/9);
Temperature temp = new Temperature(converted_Temp, 'K');
return temp;
}

return this;
}

public Temperature toCelsius()
{
int converted_Temp;

if(scale == 'K')
{
converted_Temp=temperature-273;
Temperature temp = new Temperature(converted_Temp, 'C');
return temp;
}

if(scale == 'F')
{
converted_Temp=(int)((temperature-32)*5.0/9);
Temperature temp = new Temperature(converted_Temp, 'C');
return temp;
}

return this;
}

public Temperature toFahrenheit()
{
int converted_Temp;

if(scale == 'K')
{
converted_Temp=(int)(temperature*9.0/5 -459.67);
Temperature temp = new Temperature(converted_Temp, 'F');
return temp;
}

if(scale == 'C')
{
converted_Temp=(int)(temperature*9.0/5 +32);
Temperature temp = new Temperature(converted_Temp, 'F');
return temp;
}

return this;
}

{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();

return new Temperature(temp1.temperature+temp2.temperature, 'K');
}

public Temperature subtract(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();

return new Temperature(temp1.temperature-temp2.temperature, 'K');
}

public Temperature multiply(Temperature n)
{
Temperature temp1 = this.toKelvin();
Temperature temp2 = n.toKelvin();

return new Temperature(temp1.temperature*temp2.temperature, 'K');
}

public Temperature divide(double d)
{
Temperature temp1 = this.toKelvin();
int new_temp=(int)(temp1.temperature/d);

return new Temperature(new_temp, 'K');
}

public boolean equals(Temperature n)
{
return this.temperature==n.temperature&&this.scale==n.scale;
}

@Override
public String toString()
{
return "Temperature{" + "temperature=" + temperature + "scale=" + scale + '}';
}

{
System.out.println(this);
}
}

```

DEMO CLASS
```public class TemperatureDemo {

public static final int ARRAY_SIZE = 5;
public static void main(String[] args)
{
int x;
Temperature temp1 = new Temperature(120.0, “C”);
Temperature temp2 = new Temperature(100, “F”);
Temperature temp3 = new Temperature(50.0, “C”);
Temperature temp4 = new Temperature(232.0, “K”);
Temperature tempAve = new Temperature(0.0, “C”);
// create array
// fill array with empty temperatures
// this will be discussed in class
System.out.println("Temp1 is " + temp1);
temp1 = temp2.toKelvin();
System.out.println("Temp1 to Kelvin is " + temp1);
if (temp1.equals(temp3))
{
System.out.println("These two temperatures are equal");
}
else
{
System.out.println("These two temperature are not equal");
}
System.out.println("Temp1 is " + temp1);
System.out.println("Temp2 is " + temp2);
System.out.println("Temp3 is " + temp3);
System.out.println("Temp4 is " + temp4);
tempAve = tempAve.divide(4);
System.out.println("the average temperature is " + tempAve );

}
}

```

I'm getting errors in the demo at
```Temperature temp1 = new Temperature(120.0, “C”);
Temperature temp2 = new Temperature(100, “F”);
Temperature temp3 = new Temperature(50.0, “C”);
Temperature temp4 = new Temperature(232.0, “K”);
Temperature tempAve = new Temperature(0.0, “C”);

```

The message I get is that the "C" "F" "C" "K" "C" "Syntax error on tokens, Expression expected instead" can someone tell me how i can fix this or what's wrong in my class or if something is wrong with the demo my professor provided

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Temperature Conversion Problem

Posted 27 September 2011 - 05:05 PM

Your scale parameter is a char not a String so

Temperature temp2 = new Temperature(100, “F”);

should be

Temperature temp2 = new Temperature(100, 'F');

### #9 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 27 September 2011 - 05:12 PM

oh okay I understand now, I can't believe I missed that thank you for the help, it works just fine now!

### #10 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 28 September 2011 - 07:22 AM

hmm i just went over my conversions and checked to see if everything was right. However my class doesn't seem to be reading in the temperature from the demo. The first conversion tokelvin just comes out to 273 instead of 393. Can anyone tell me why it isn't reading the temperature from the demo?

### #11 karabasf

• D.I.C Regular

Reputation: 202
• Posts: 417
• Joined: 29-August 10

## Re: Temperature Conversion Problem

Posted 28 September 2011 - 07:27 AM

Take a look here:

```System.out.println("Temp1 is " + temp1);
temp1 = temp2.toKelvin();
System.out.println("Temp1 to Kelvin is " + temp1);

```

So what you're doing is, grab Temperature() temp2, convert it to Kelvin (instead of temp1). As a result you get 393 K, as temp 2 is 100 degrees Fahrenheit.

You should be able to fix this

Edit:
I suspect more is going wrong, but I am at my other computer (no JAVA JDK and IDE on it), so I can't evaluate this properly for you. Did you try to set up break points to see what's going on?

This post has been edited by karabasf: 28 September 2011 - 07:48 AM

### #12 shortman12012

Reputation: 0
• Posts: 37
• Joined: 09-February 11

## Re: Temperature Conversion Problem

Posted 28 September 2011 - 07:57 AM

oh okay the demo was provided by my professor so I didn't know if that was wrong or not but it works now. But when I change the string of temp1 to F it doesn't convert correctly and just equals 273 when it should equal 321. Is my if else statements working correctly or are they wrong?