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 3 of 3

Thread: Please help me with my program

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help me with my program

    Hi, I'm new to programming and I have a simple Java program I am trying to run where I convert Celsius to Fahrenheit and vice versa. My program compiles using jgrasp, but I do not get the correct figures. Can anyone help me out? Below is my driver and class respectively.
       import java.util.Scanner;
     
        public class Driver
       {
           public static void main(String args[])
          {
          // Declare variables for user input
     
             double degrees;
             double Celsius;
             String scale;
             String endScale;
             double getCelsius;
             double getFahrenheit;
          // Create a Scanner object to read from the keyboard
             Scanner keyboard = new Scanner (System.in);
     
          // Get the temperature and scale from user
             System.out.print("Enter the temperature: ");
             degrees = keyboard.nextDouble();
             scale = keyboard.nextLine().trim();
     
          // Declare and instantiate an object reference variable
     
             Temperature outsideTemp = new Temperature();
     
          // Set the outside temperature based on the user input
             if (scale.equalsIgnoreCase("C"))
                outsideTemp.setCelsius(degrees);
             else
                outsideTemp.setFahrenheit(degrees);
     
             System.out.print("How would you like the temperature displayed (C or F)?");
             endScale = keyboard.nextLine();
          // Get the scale to display the temperature from the user
     
             if (endScale.equalsIgnoreCase("C"))
                System.out.print("The temperature is " + outsideTemp.getCelsius());
             else
                System.out.print("The temperature is " + outsideTemp.getFahrenheit());
     
          // Display the temperature based on the user's desired scale
             System.out.print(" degrees " + endScale);
          }
     
       }

        public class Temperature
       {
       // Instance variable
          private double degreesKelvin;      // degrees in Kelvin
          double Celsius;      // degrees in Celsius    
          double Fahrenheit;
     
       // Constructor method: initialize degreesKelvin to zero
           public Temperature()
          {
             degreesKelvin = 0;
     
          }
     
       // Convert and save degreesCelius in the Kelvin scale
           public void setCelsius(double Celsius)
          {
             degreesKelvin = Celsius + 273.16;
          }
     
       // Convert degreesKelvin to Celsius and return the value
           public double getCelsius()
          {
             Celsius = degreesKelvin - 273.16;
             return Celsius;
          }
     
       // Convert and save degreesFahrenheit in the Kelvin scale
           public void setFahrenheit(double Fahrenheit)
          {
             degreesKelvin = (( 5 / 9 ) * (Fahrenheit - 32)) + 273.16;
          }
     
       // Convert degreesKelvin to Fahrenheit and return the value
     
           public double getFahrenheit()
          {
             Fahrenheit    = ((degreesKelvin - 273.16) * (9 / 5 )) + 32;
             return Fahrenheit;
          }
     
       }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Please help me with my program

    It' not necessary to keep track of 3 different temparatures, you only need to keep track of one of them and then you can compute the rest.

    private double degreesKelvin;
     
    public Temperature()
    {
         degreesKelvin = 0;
    }
     
    public void setFahrenheit(double degrees)
    {
        degreesKelvin = 5*(degrees - 32)/9 + 273.16;
    }
     
    public void setCelsius(double degrees)
    {
         degreesKelvin = degrees + 273.16;
    }
     
    public double getFahrenheit()
    {
         return 9*(degreesKelvin-273.16)/5+32;
    }
     
    public double getCelsius()
    {
         return degreesKelvin - 273.16;
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Please help me with my program

    helloworlds modified code works around this, but based upon your original code make sure to add decimals or casts to all your numbers, otherwise its doing integer math and not floating point math...for example, the following code will print this:
    0.0
    0.55555
    	double test = 5/9;
    	System.out.println(test);
    	test = 5.0/9.0;
    	System.out.println(test);
    Last edited by copeg; November 5th, 2009 at 11:21 AM.