Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2009, 05:24 AM
Member
 
Join Date: Nov 2009
Posts: 1
Rep Power: 0
jhawks8 is on a distinguished road
Default Please help me with my program
Hi I am new to Java and I am using jgrasp. My assignment is to convert celsius to fahrenheit and vice versa. I can't seem to get the right calculations, can somebody please see what is wrong?

Here is my driver:
Code:
   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);
      }
   
   }
Here is my class:
Code:
    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;
      }
   
   }
Everything compiles, I just dont get the right answer.

Last edited by Eranga; 11-05-2009 at 10:34 AM. Reason: code tags added
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-05-2009, 05:52 AM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 355
Rep Power: 1
travishein is on a distinguished road
Default
I don't get why you created a class for Temperature with instance variables, where to get any conversion needs 2 calls, one to set the input and one to get the output.

would static functions work here ?
Code:
class Temperature {
  // no instance variables  (and in practice, we wouldnt create an instance even)
  
  public static double celsiusToKelvin(double celsius)
  {
    return celsius + 273.16;
  }
  
  public static double kelvinToCelsius(double degreesKelvin)
  {
   return degreesKelvin - 273.16;
  }
  
  public static double fahrenheitToKelvin(double fahrenheit)
  {
    return (( 5 / 9 ) * (fahrenheit - 32)) + 273.16;
  }
  
    public static double kelvinToFahrenheit(double degreesKelvin)
  {
   return ((degreesKelvin - 273.16) * (9 / 5 )) + 32;
  }

  public static double celciusToFahrenheit(double degreesCelsius) {
    return kelvinToFahrenheit(celsiusToKelvin(degreesCelsius));
  }
  
  public static double farenheitToCelsius(double degreesFahrenheit) {
    return kelvinToCelsius(farenheitToCelsius(degreesFahrenheit));
  }
}
so, to use this

Code:
    double degrees = keyboard.nextDouble();
    String scale = keyboard.nextLine().trim();

    double outsideTemp = 0;

    System.out.print("How would you like the temperature displayed (C or F)?");
    String endScale = keyboard.nextLine();

    // Set the outside temperature based on the user input
    if (scale.equalsIgnoreCase("C")) {
      if (endScale.equalsIgnoreCase("C")) {
        // wow, nothing to do. celcius in celcius out
        outsideTemp = degrees;
      }
      else {
        outsideTemp = Temperature.celciusToFahrenheit(degrees);
      }
    }
    else {
       if (endScale.equalsIgnoreCase("C")) {
        outsideTemp = Temperature.farenheitToCelsius(degrees);
      }
      else {
        // farenheit in, farenheit out
        outsideTemp = degrees;
      }
    }

    // Get the scale to display the temperature from the user

    System.out.print("The temperature is " + outsideTemp + " deg " + endScale);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-05-2009, 10:33 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Just an info to our thread starter. Please use code tags when you posting again. Un-formatted codes are hard to read.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute java program within java program popey New To Java 2 10-22-2009 06:32 PM
Execute A program from a Program! Moncleared Advanced Java 2 02-22-2009 05:17 PM
Executing a program within a program gibsonrocker800 New To Java 5 05-12-2008 09:24 AM
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +2. The time now is 10:38 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org