Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Won't display calculated fahrenheit and celsius when running.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Won't display calculated fahrenheit and celsius when running.

    Please help, can't figure out why it wont display. I think it has something to do with the setter method for DegreesInCelsius and fahrenheit. on output the answers for temperature 1 fahrenheit and temperature 2 celsius is incorrect.

    <
    public class Temperature
       {
       // Instance variable
          private double degreesInKelvin; // degrees in Kelvin
    		private double degreesInCelsius;
    		private double degreesInFahrenheit;
     
     
       // Constructor method: initialize degrees in Kelvin to zero
          public Temperature()
          {
             degreesInKelvin = 0;
    			degreesInCelsius = 0;
    			degreesInFahrenheit = 0;
          }
     
       // Convert and save degrees in Celsius in the Kelvin scale
          public void setDegreesFromCelsius(double celsius)
          {
             degreesInKelvin = celsius + 273.15;
          }
     
       // Convert and save degrees in Fahrenheit in the Kelvin scale
          public void setDegreesFromFahrenheit(double fahrenheit)
          {
             degreesInKelvin = (5.0 / 9.0) * (fahrenheit - 32) + 273.15;
          }  
     
    		// Convert and save degrees in Celsius in the Kelvin scale
          public void setDegreesToCelsius(double fahrenheit)
          {
             degreesInCelsius = (fahrenheit - 32) * (5 / 9);
          }
     
       // Convert and save degrees in Fahrenheit in the Kelvin scale
          public void setDegreesToFahrenheit(double celsius)
          {
             degreesInFahrenheit = (celsius - 273.15) * (9/5) + 32;
          } 
     
       // Getter method returns the degrees in Kelvin
          public double getDegreesInKelvin()
          {
            return degreesInKelvin;
          }
     
    		public double getDegreesInCelsius()
    		{
    			return degreesInCelsius;
    		}
     
    		public double getDegreesInFahrenheit()
    		{
    			return degreesInFahrenheit;
    		}
       }>


    <
     import java.util.Scanner;
       public class TemperatureDemo
       {
          public static void main(String args[])
          {
     
             final double MARGIN_OF_ERROR = 0.0001;
          // Declare variables
             double degrees;
             Temperature temperature1 = new Temperature();
             Temperature temperature2 = new Temperature();
     
     
          // Create a Scanner object to read from the keyboard
             Scanner keyboard = new Scanner (System.in);
     
          // Get the first temperature in Celsius from the user
             System.out.print("Enter a temperature in Celsius: ");
             degrees = keyboard.nextDouble();
             temperature1.setDegreesFromCelsius(degrees);
     
          // Get the second temperature in Fahrenheit from the user
             System.out.print("Enter a temperature in fahrenheit: ");
             degrees = keyboard.nextDouble();
             temperature2.setDegreesFromFahrenheit(degrees);
     
             double difference = temperature1.getDegreesInKelvin() - temperature2.getDegreesInKelvin();
     
           // Display the first temperature in Kelvin
             System.out.println("\nTemperature 1 in Kelvin is " + temperature1.getDegreesInKelvin());
             System.out.println("Temperature 1 in Ceslius is " + temperature1.getDegreesInCelsius());
             System.out.println("Temperature 1 in Fahrenheit is " + temperature1.getDegreesInFahrenheit());
     
     
          // Display the second temperature in Kelvin
             System.out.println("\nTemperature 2 in Kelvin is " + temperature2.getDegreesInKelvin());
             System.out.println("Temperature 2 in Ceslius is " + temperature2.getDegreesInCelsius());
             System.out.println("Temperature 2 in Fahrenheit is " + temperature2.getDegreesInFahrenheit());
     
             if (Math.abs(difference) <= MARGIN_OF_ERROR)
             {         
                System.out.println("\nThe two temperatures are equivalent"); 
             }	     
             else
             {        
                System.out.println("The two temperatures are not equivalent"); 
             }
          }
       }>
    Last edited by smithmar; February 17th, 2012 at 11:34 AM. Reason: Better but not finished


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Won't display calculated fahrenheit and celsius when running.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Can you post what your program outputs and add comments to the output showing what is wrong with it and show what you want the output to look like?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Won't display calculated fahrenheit and celsius when running.

    I tried running your program.. and I got this result:
    Ex.

    Temperature 1 in Kelvin is 303.15
    Temperature 1 in Ceslius is 0.0
    Temperature 1 in Fahrenheit is 0.0

    Temperature 2 in Kelvin is 272.0388888888889
    Temperature 2 in Ceslius is 0.0
    Temperature 2 in Fahrenheit is 0.0

    so if I'm not mistake your problem is getting the value of celsius and farenheit. You need to equate the input before computing the kelvin and invoke the method setDegreesToCelsius and setDegreesToFarenheit next to the code setDegreesFromCelsius and setDegreesFromFarenheit.. You need to cast the equation (5/9) to double so that the result will not always be zero.. take a look at this modified code:

    public void setDegreesFromCelsius(double celsius)
    {
    degreesInCelsius = celsius; //equate the value of your parameter to the instance var
    degreesInKelvin = celsius + 273.15;

    }

    // Convert and save degrees in Fahrenheit in the Kelvin scale
    public void setDegreesFromFahrenheit(double fahrenheit)
    {
    degreesInFahrenheit = fahrenheit; //equate the value of your parameter to the instance var
    degreesInKelvin = (5.0 / 9.0) * (fahrenheit - 32) + 273.15;

    }

    // Convert and save degrees in Celsius in the Kelvin scale
    public void setDegreesToCelsius(double fahrenheit)
    {

    degreesInCelsius = (fahrenheit - 32) * (double)5 / (double)9; //cast the digits so that it will not result to zero. by default the data type of 5 and 9 are integer.
    }

    // Convert and save degrees in Fahrenheit in the Kelvin scale
    public void setDegreesToFahrenheit(double celsius)
    {
    degreesInFahrenheit = (celsius - 273.15) * (9/5) + 32;
    }

    modifying the codes, the output will be like this:

    Enter a temperature in Celsius: 40
    Enter a temperature in fahrenheit: 50

    Temperature 1 in Kelvin is 313.15
    Temperature 1 in Ceslius is 40.0
    Temperature 1 in Fahrenheit is -201.14999999999998

    Temperature 2 in Kelvin is 283.15
    Temperature 2 in Ceslius is 10.0
    Temperature 2 in Fahrenheit is 50.0
    The two temperatures are not equivalent

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Won't display calculated fahrenheit and celsius when running.

    Please Edit your post and wrap your code with
    [code=java]<YOUR CODE HERE>[/code] to get highlighting
    What should the output look like?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Won't display calculated fahrenheit and celsius when running.

    Thanks mrowkoob. Didnt solve the whole problem but it does help.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Won't display calculated fahrenheit and celsius when running.

    Your welcome... I don't know the exact output you want it to be.. It was not stated well in you problem.

Similar Threads

  1. Temperature Table Celsius and Fahrenheit
    By Krodfish in forum Loops & Control Statements
    Replies: 2
    Last Post: February 2nd, 2012, 10:06 PM
  2. Conversions Between Celsius and Fahrenheit
    By baueml01 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2011, 07:49 PM
  3. Running out of ideas...
    By Cat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2011, 11:21 AM
  4. 'Run' isn't 'Running'
    By leonsas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 27th, 2010, 01:37 PM
  5. Replies: 1
    Last Post: March 3rd, 2009, 08:04 AM