Fahrenheit to Celsius conversion yields only 0.0 and -0.0

I'm on the 8th chapter (Methods, Constructors, and Fields) of my Java methods book and I'm having a problem with one of my exercises.

The provided code is Temperature.java

  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import java.text.DecimalFormat;

  public class Temperature extends JApplet
   implements ActionListener {
      private JTextField displayF, displayC;
      private static DecimalFormat displayFormat = new DecimalFormat("0.0");

      public void init() {
          Container c = getContentPane();
          c.setBackground(Color.white);
          c.setLayout(new GridLayout(2, 2, 10, 0));

          c.add(new JLabel("  Fahrenheit:"));
          c.add(new JLabel("  Celsius:"));

          displayF = new JTextField(6);
          displayF.setBackground(Color.yellow);
          displayF.addActionListener(this);
          c.add(displayF);

          displayC = new JTextField(6);
          displayC.setBackground(Color.yellow);
          displayC.addActionListener(this);
          c.add(displayC);
      }

      public void actionPerformed(ActionEvent e) {
          String s;
          double degrees;
          FCConverter fc = new FCConverter();

          if ((JTextField) e.getSource() == displayF) {
              s = displayF.getText().trim();
              degrees = Double.parseDouble(s);
              fc.setFahrenheit(degrees);
              degrees = fc.getCelsius();
              displayC.setText(displayFormat.format(degrees));
          } else {
              s = displayC.getText().trim();
              degrees = Double.parseDouble(s);
              fc.setCelsius(degrees);
              degrees = fc.getFahrenheit();
              displayF.setText(displayFormat.format(degrees));
          }
      }
  }

And from there I'm supposed to write and test FCConverter.java I've come up with:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class FCConverter
{
  private double Celsius;
  private double Fahrenheit;
  public double setFahrenheit (double degrees)
  {
      Celsius = (degrees - 32) * (5 / 9);
      return 0;
  }
  public double getCelsius ()
  {
      return Celsius;
  }
  public double setCelsius (double degrees)
  {
  Fahrenheit = (degrees * 9 / 5 + 32);
          return 0;
  }
  public double getFahrenheit ()
  {
      return Fahrenheit;
  }
}

It's probably horrendous in terms of style, but I just wanted to get some working code that got past the compiler (hence the "return 0;"s which I'm fairly certain can be circumvented in a much more fashionable manner).

What I want to know is, when I test this in appletviewer or in browser form, the Celsius to Fahrenheit conversion works fine, but the Fahrenheit to Celsius conversion yields only 0.0 and -0.0. I don't understand what's wrong since the part I've designed seems fairly symmetrical. Shouldn't it be the case that both or neither conversion method works?


In integer math (5 / 9) = 0.

and (degrees - 32) * (5 / 9); will always be 0 you need to use doubles or something.


The problem seems to be 5/9. They are seen as an integer, and 5/9 is 0.something which is 0 as an integer. So whatever * 0 = +-0 :)

Place a "d" after the numbers, (5d/9d) forcing the compiler to consider them doubles.


change (5 / 9) to (5.0 / 9.0) to let java know, that you want real numbers division.


If you don't return anything set the return type to void. If you like to return doubles, you have to use doubles in calculations. 5/9 is always zero because 5 and 9 are integers. 5.0/9.0 is a different calculation on doubles.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class FCConverter
{
  private double Celsius;
  private double Fahrenheit;
  public void setFahrenheit (double degrees)
  {
     Celsius = (degrees - 32.0) * (5.0 / 9.0);
  }
  public double getCelsius ()
  {
      return Celsius;
  }
  public void setCelsius (double degrees)
  {
      Fahrenheit = (degrees * 9.0 / 5.0 + 32.0);
  }
  public double getFahrenheit ()
  {
      return Fahrenheit;
  }
}


import java.util.Scanner;
public class HelloWorld {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);  // Reading from System.in

        System.out.println("Enter  the number of Fahrenheit temperatures to convert to Kelvins");
// opts for the user to enter a number 

        int F = reader.nextInt() ; // Scans the next token of the input
        double f = 5/9d;
        System.out.println(f);
        double x = F -32;
        System.out.println(x);
        double K = (f*x)+ 273.0; // the formulae to calculate temperature  in Kelvin
        System.out.println( +F + " Fahrenheit is equal " +K + " Kelvins" ) ;
        reader.close();




    }       

}