convert Fahrenheit to Celsius: Get correct Output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raitoHiong
    New Member
    • Nov 2013
    • 7

    convert Fahrenheit to Celsius: Get correct Output

    i want to get correct output:

    Code:
    public class Temperature
    {
       private String myScale; //valid values are "F" or "C"
       private double myDegrees;
       double celsius;
    
       //default constructor
       public Temperature()
       { 
          myScale = "";
          myDegrees = 0.0;
       }
    
       //constructor with specified degrees and scale
       public Temperature(double degrees, String scale)
       {
          myDegrees = degrees;
          myScale = scale;
       }
    
       //a method to convert Fahrenheit to Celsius
       
       public Temperature toCelsius()
       {
          
          Temperature tempCelsius;
          celsius = ((myDegrees - 32) * 5) / 9; 
          tempCelsius = new Temperature(celsius, "C");
          return tempCelsius;
    
       }
       // a method to display the temperature
       public void displayTemperature()
       {
       System.out.println("The temperature in C is " + celsius);
       }
    }
    
    import java.util.Scanner;
    
    public class TemperatureDemo
    {
       public static void main (String [] args)
       {
          Temperature tempCelsius;
          Scanner input = new Scanner(System.in);
          String scale = ""; double degree;
          System.out.print("Enter temperature scale: ");
          scale = input.next();
          System.out.print("Enter number of degrees: ");
          degree = input.nextDouble();
          //Create a Temperature object based on user input
          Temperature Answer = new Temperature();
          //call toCelsius() method
          Answer.toCelsius();
    
          //call displayTemperature() method
          Answer.displayTemperature();
       
       }
    }
    Output:
    Enter temperature scale: F
    Enter number of degrees: 212
    The temperature in C is -17.777777777777 78

    (My output of temperature in C should be 100)

    Any help and response will be appreciate.

    Regards!
    Attached Files
    Last edited by raitoHiong; Nov 8 '13, 12:10 AM. Reason: more convenient to read
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    OK, we know what you want to get. What are you getting? What's working and what isn't?

    Also, please use [code]...[/code] tags when posting code. It makes it much easier to read.

    Comment

    • raitoHiong
      New Member
      • Nov 2013
      • 7

      #3
      Sorry for the inconvenience. i edit it alreaddy.....My output for the temperature in C is -17.777777777777 78 (wrong).... i want to get The temperature in C is 100

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        You're not passing the values you read to the Temparature object; replace what is displayed as line 53 here with
        [code=java]Temperature Answer = new Temperature(deg ree, scale);[/code] and it should work.

        Comment

        • raitoHiong
          New Member
          • Nov 2013
          • 7

          #5
          It work!

          Thanks you very much, Nepomuk

          Comment

          Working...