Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Unhappy Help with fahrenheit to celsius conversion

    Hi.I have recently been given the task of creating a program that converts a Fahrenheit temp to a Celsius temp with the calculateCelsius() method. The formula for the conversion is :

    Celsius = (5/9) * (Fahrenheit -32)
    public static float calculateCelsius(float f)

    This is what I have so far, and take it easy on me I'm lost to say the least.

    import javax.swing.*;

    public class FahrenheitToCelsius {


    public static void main (String[] args) {

    // Add code to prompt user to enter a Fahrenheit temperature
    // add code to invoke the method calculateCelsius and to
    // display the results here.

    }


    public static float calculateCelsius(float f) {

    return 0.0f; // so incomplete file will compile
    // change the return statement to return the
    // calculated Celsius value.
    }
    }
    I'm having trouble with how to obtain the users Fahrenheit temp input and use the entered value with the supplied calculation and return an answer in another dialog box.

    Your help is much appreciated!!
    Thanks

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i have minimal experience with gui's, but seems like you need to learn them, specifically swing. here's a tutorial: Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Or are you in fact supposed to create a GUI? If you are this new to programming, my guess is that the instructor wants you to create a program that uses a console input and output. If so, look into using a java.util.Scanner object and get your input via the Scanner.

  4. #4
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    something along these lines may help get you started.

    if it needs to be a method just create a method which does the calculateions called "farToCelsius()" and call it by saying "result = farToCelsius(far, celsius)

    Java Code:
    package ...package name.......;
    
    import java util.Scanner;
    
    public class .............class name.......; {
    
    public static void main(String[] args) {
    
    float celsius, fahr;
    int lower, upper, step;
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter farenheit: ");
    fahr = scan.nextInt();
    
    	celsius = (5.0/9.0)*(fahr-32.0);
    	System.out.println("Farenheit is: " + fahr);
    	System.out.println("Celsius is: " + celsius);
    	fahr = fahr + step;
    
    }
    hope that helps a bit

  5. #5
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    Haven't tested this but you could also just use this in your main method:

    String g = JOptionPane.showInputDialog("Enter in farneheit temperature: ");
    float f = Float.parseFloat(g);

    and then

    public static float calculateCelsius(float f) {

    float celsius = (5/9) * (f -32);

    return celsius;
    }

Similar Threads

  1. Doc to Pdf conversion
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2009, 12:27 PM
  2. Fahrenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 07:50 AM
  3. farenheit to celsius
    By lalithalydia in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 08:44 AM
  4. Celsius Converter project
    By jpdamigaman in forum NetBeans
    Replies: 3
    Last Post: 11-14-2007, 12:20 AM
  5. Converts from Fahrenheit to Celsius
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •