-1

I am making a program to convert one temperature to another temperature. In example, Fahrenheit to Celsius, Celsius to Fahrenheit, Celsius to kelvin, etc.

I've got the GUI set up, but now i'm getting errors during conversion. I've looked for conversion methods, but can't seem to find any. So i'm writing them based on formulas i've seen online.

private static class EventHandler implements ActionListener {
    /**
     * this method gets called when an object we are listening to is interacted with
     *
     * @param evt    ActionEvent that interacted with
     */
    public void actionPerformed(ActionEvent evt) {
        //creates the formating we would like for the numbers
        DecimalFormat df = new DecimalFormat("#.##");
        //if the event triggered was celInput than
        if (evt.getSource() == cText) {
            String strcText = cText.getText();
            double cTemp = Double.parseDouble(strcText);
            fText.setText("" + convertCtoF(cTemp));
            kText.setText("" + convertCtoK(cTemp));
        }else if(evt.getSource() == fText) {
           String strfText = fText.getText();
           double fTemp = Double.parseDouble(strfText);
           cText.setText("" + convertFtoC(fTemp));
           kText.setText("" + convertFtoK(fTemp));
        }else if(evt.getSource() == kText) {
           String strkText = kText.getText();
           double kTemp = Double.parseDouble(strkText);
           cText.setText("" + convertKtoC(kTemp));
           fText.setText("" + convertKtoF(kTemp));
        }       
    }//end actionPerformed method
}
public static double convertCtoF(double c) {
    return (c / (5/9)) + 32;
}
public static double convertFtoC(double f) {
    return (5/9) * (f - 32);
}
public static double convertCtoK(double c) {
    return c + 273.15;
}
public static double convertFtoK(double f) {
    return convertFtoC(f) + 273.15;
}
public static double convertKtoC(double k) {
    return k - 273.15;
}
public static double convertKtoF(double k) {
    return ((9/5) * convertKtoC(k)) + 32;
}

That's all the code I think would be necessary in order to find the logic error.

Any help would be greatly appreciated, as I cannot see why this is occurring.

TaylorTDHouse
  • 243
  • 2
  • 4
  • 8

3 Answers3

2

This is a classic case of your divisions (e.g. (5/9)) being performed in integer arithmetic (which will discard the remainder).

You need to promote at least one of the literals to a double type prior to the division; e.g.

5.0 / 9

If the compiler detects an int and a double in the operands of an operator then the int type gets promoted to a double prior to the computation.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Yea, that's what I thought before, but it didn't completely fix the issue then or now, which leads me to believe it's an issue somewhere else. – TaylorTDHouse Jan 07 '14 at 08:37
  • That part at the end about Centigrade and Celsius is not quite true. You have misunderstood the Wikipedia article. The terms "Centigrade" and "Celsius" are exact synonyms - they do not differ by 0.01. – Dawood ibn Kareem Jan 07 '14 at 08:38
  • 1
    According to the article that you linked to :-) the name was changed from "centigrade" to "Celsius" to avoid confusion with the word in various languages for a measure of angle. The meaning is identical. And the difference between Kelvins and degrees Celsius is 273.15. It is NOT 273.16. – Dawood ibn Kareem Jan 07 '14 at 08:43
  • Look at the table in the middle of the wikipedia article. It gives the triple point of water as 273.16 Kelvins and also as 0.01 degrees Celsius. So the difference is 273.15. – Dawood ibn Kareem Jan 07 '14 at 08:47
  • No. You should upvote my answer if you feel it is a good answer. Not just to be nice. Nice people ruin the whole Stack Overflow system! – Dawood ibn Kareem Jan 07 '14 at 08:54
0

bathsheba is right changing 5 to 5.0 will do the trick.I wrote below code and it was working /* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    double cTemp = Double.parseDouble("27");
        System.out.println("" + convertCtoF(cTemp));
        System.out.println("" + convertCtoK(cTemp));
}
 public static double convertCtoF(double c) {
  return (c / (5.0/9.0)) + 32;
 }
 public static double convertCtoK(double c) {
 return c + 273.15;
}
}

If this does not solves your problem then i am pretty sure problem is not in your logic its some where else.

  • This is only PART of the problem. The other important part of the problem is that OP has swapped the fields around - see my answer for details. – Dawood ibn Kareem Jan 07 '14 at 17:03
-1

Instead of (c / (5/9)) + 32, write c * 9 / 5 + 32. Since c is a double, that expression will be a double throughout, and you won't have trouble with integer division.

Instead of (5/9) * (f - 32), write (f - 32) * 5 / 9. Since f is a double, that expression will be a double throughout, and you won't have trouble with integer division.

Update

Also, instead of (9/5) * convertKtoC(k), write convertKtoC(k) * 9 / 5, for the same reason.

Update again

The above changes all need to be done. BUT you've got a different problem too. I believe you've just mislabelled your text fields. 27 Fahrenheit is 270.2222 Kelvin, which is -2.7777 Celsius. So you've just swapped the three fields around. Your Kelvin field is labelled Fahrenheit, your Celsius field is labelled Kelvin and your Fahrenheit field is labelled Celsius.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110