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

Thread: Conversions Between Celsius and Fahrenheit

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Conversions Between Celsius and Fahrenheit

    Ok, I am trying to write code to convert both celsius to fahrenheit and fahrenheit to celsius. I have to use the two methods public static double celsiusToFahrenheit(double celsius) and public static fahrenheitToCelsius(double fahrenheit) in the code. I am really lost as to where I am supposed to go in my program. I will attach my program hoping someone can help me get through this. This is only my second programming class so you will have to bear with me as I am new. Thanks for the help!

    //Michael Bauer Exercise 5.8
    public class ConversionsBetweenCelsiusAndFahrenheit{
    public static void main(String[] args){
    for(double celsius=40; celsius<=31.0; celsius--){
    System.out.print(celsiusToFahrenheit);
    for(double fahrenheit=120; fahrenheit<=30.0; fahrenheit--){
    System.out.print(fahrenheitToCelsius);
    }
    public static double celsiusToFahrenheit(double celsius){
    double fahrenheit;
    celsiusToFahrenheit=(5.0/9.0)*fahrenheit-32;
    return fahrenheit;
    }
    public static double fahrenheitToCelsius(double fahrenheit){
    double celsius;
    fahrenheit=(9.0/5)*celsius+32;
    return fahrenheitToCelsius;
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Conversions Between Celsius and Fahrenheit

    What is the problem? We can guess - mine being you don't know how to call the methods (If so, see Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects) ) - but guesses are just that. Ask a specific question, get a specific answer. Suggested reading: How to Help Yourself Get Help

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conversions Between Celsius and Fahrenheit

    //Michael Bauer Exercise 5.8
    public class ConversionsBetweenCelsiusAndFahrenheit{
    public static void main(String[] args){
    for(double celsius=40; celsius<=31.0; celsius--){
    System.out.print(celsiusToFahrenheit(fahrenheit));
    for(double fahrenheit=120; fahrenheit<=30.0; fahrenheit--){
    System.out.print(fahrenheitToCelsius(celsius));
    }
    public static double celsiusToFahrenheit(double celsius){
    return celsius=(5.0/9.0) * fahrenheit - 32;
    }
    public static double fahrenheitToCelsius(double fahrenheit){
    return fahrenheit=(9.0/5) * celsius + 32;
    }
    }
    }
    }

    I figured out how to call on methods. When I say I need help I need all around help with the whole program. I am a junior in college and this is my second java programming class. My first one was about 6-7 months ago. I know I lack experience and knowledge! That is why I came to this website, hoping to get hopes from experiences people on how to help me complete this program. Basically I need to finish this program and be able to compile it and run it because I have one more to write tonight (that's even harder) before tomorrow.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Conversions Between Celsius and Fahrenheit

    You say you need to finish the program, but have yet to tell us what the requirements are. Without knowing the destination, we can't give you directions (and should we try to guess, can quite probably lead you in the wrong direction). Did you read the suggested reading link I posted above?

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conversions Between Celsius and Fahrenheit

    ok here is the whole question in the book:

    (Conversions between Celsius and Fahrenheit) Write a class that contains the following two methods:
    //Converts from celsius to fahrenheit
    public static double celsiusToFahrenheit(double celsius)

    //Converts from fahrenheit to celsius
    public static double fahrenheitToCelsius(double fahrenheit)

    The formula for the conversion is:
    fahrenheit=(9.0/5) * celsius + 32

    Write a test program that invokes these two methods to display the following tables.
    Celsius Fahrenheit Celsius Fahrenheit
    40.0 104.0 120.0 48.89
    39.0 102.2 110.0 43.33
    .................................................. .......................
    32.0 89.6 40.0 4.44
    31.0 87.8 30.0 -1.11


    This is the exact question from the book. I hope that it helps out

  6. #6
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conversions Between Celsius and Fahrenheit

    What I have finished so far will not compile in cmd.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Conversions Between Celsius and Fahrenheit

    Quote Originally Posted by baueml01 View Post
    What I have finished so far will not compile in cmd.
    There are some syntax errors in the code you posted above, but I would first say to break the problem down. Concentrate on one requirement at a time. I would say strip out all code of the class, then build back up - take the first method to convert C to F
    public static double celsiusToFahrenheit(double celsius){
        return celsius=(5.0/9.0) * fahrenheit - 32;
    }

    Where has fahrenheit been declared (the compiler must know what the variables are)? Further, you might want to check the formula for this. It oftentimes helps to write out on paper how you would accomplish this, then move that to code. Try writing this method only - without the other pieces of code. Make sure it compiles before moving forward. Post back if it does not

Similar Threads

  1. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM
  2. conversions and excel
    By mkslt4 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 10th, 2010, 12:54 AM
  3. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  4. Java Variable Conversions
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM
  5. Java Variable Conversions
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: June 23rd, 2009, 05:03 AM