8 Replies Latest reply: Apr 2, 2012 10:41 AM by gimbal2

# Program that Converts Fahrenheit to Celsius

Hello all,

I am brand new to the forum, so please advise me if I am posting a new thread "illegally" based on the forum rules.

I am creating a program for class that converts Fahrenheit to Celsius using a method called in a loop. Here is the question:

The formula for converting a temperature from Fahrenheit to Celsius is C = 5/9 * (F - 32), where F is the Fahrenheit temperature and C is the Celsius temperature. Write a method named celsius that accepts a Fahrenheit temperature as an argument. The method should return the temperature, converted to Celsius. Demonstrate the method by calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and their Celsius equivalents.

Here is the code that I have thus far:

public class ConvertTemp
{
public static void main(String[] args)
{
System.out.println("Fahrenheit\tCelsius");
System.out.println("=========================");
celsius();
}

// Create the celsius method.
public static double celsius(int F, double C)
{
for(F = 0; F <= 20; F++){
C = (5.0 / 9.0) * (F - 32.0);

return C;
}
}
}

When I attempt to compile, I get the following error message: "method celsius in class ConvertTemp cannot be applied to given types". Does this mean that the arguments int F and double C that I passed in the celsius method are not the correct data type to be returned to the main method?

Any help is much appreciated!

Thank you
• ###### 1. Re: Program that Converts Fahrenheit to Celsius
924743 wrote:
Hello all,

Write a method named celsius that accepts a Fahrenheit temperature as an argument. The method should return the temperature, converted to Celsius.
So do that. You haven't done it yet. There's nothing there about accepting C as another argument.
Demonstrate the method by calling it in a loop that displays a table of the Fahrenheit temperatures 0 through 20 and their Celsius equivalents.
There's nothing there about putting the test loop inside the method.
celsius();
There's nothing there that passes a Fahrenheit temperature as an argument.

• ###### 2. Re: Program that Converts Fahrenheit to Celsius
I think you want to write a program as below:
``````public class ConvertTemp
{
public static void main(String[] args)
{
System.out.println("Fahrenheit\tCelsius");
System.out.println("=========================");
for(int F = 0; F <= 20; F++)
{
double C = celsius(F);
System.out.println(F+"F"+" = "+C+"C");
}
}

// Create the celsius method.
public static double celsius(int F)
{

double C = (5.0 / 9.0) * (F - 32.0);
return C;
}
} ``````
Problem areas in ur code as I found are :
``public static double celsius(int F, double C)``
Why are you taking "double C" as an argument when your question clearly states that "Write a method named celsius that *accepts a Fahrenheit temperature as an argument*" . So here only F shoud be passed as an arguement.

Also why there is a loop inside celsius method?
The idea is to write a method which returns temperature in celsius and then call this method for 0 to 20 in a for loop not to write FOR loop in the method.

Hope that helps.

Edited by: J2EE_Life on Apr 1, 2012 9:31 AM
• ###### 3. Re: Program that Converts Fahrenheit to Celsius
J2EE_Life wrote:
I think you want to write a program as below:
Please don't do the OP's work for him. That does not help him to learn. It only encourages him to rely on others to do his work.
• ###### 4. Re: Program that Converts Fahrenheit to Celsius
Thank you very much. I am having trouble getting the concepts down. My logic was not right for this one, as my code clearly suggested!

So putting a for loop inside a method is never done for a problem like this? And I did understand the part about passing C (celsius) as an argument. The problem does state to pass F(fahrenheit) as the argument. So that you for pointing that out!
• ###### 5. Re: Program that Converts Fahrenheit to Celsius
924743 wrote:
So putting a for loop inside a method is never done for a problem like this?
A problem like what? What about this problem makes you think you wouldn't use a for loop? A for loop is used then you want to do something multiple times, usually based on some counter.
• ###### 6. Re: Program that Converts Fahrenheit to Celsius
So putting a for loop inside a method is never done for a problem like this?
That's not what I said and it's not what I meant. There are zillions of cases where you will do that, but there's nothing in this assignment that tells you to do so. Now you need to re-read what I said, which was to re-read what the assignment said, which was to test the method with a 'for' loop. That doesn't mean putting a 'for' loop inside the method.
• ###### 7. Re: Program that Converts Fahrenheit to Celsius
It happens when you start something new. Initially you will struggle with the concepts.
But always remember to READ question again and again untill you understand it. As pointed by me and others the solution of your assignment was trivial. It is code as question says. The question says write a method which accepts F as parameter, so do that. Then it asks to loop it from 0 to 20, so call that method in For loop and your program is ready.

As pointed by EJP above about FOR loop dont jump into conclusion early. Re-read each of comments again and understand their notion .
• ###### 8. Re: Program that Converts Fahrenheit to Celsius
J2EE_Life wrote:
As pointed by EJP above about FOR loop dont jump into conclusion early. Re-read each of comments again and understand their notion .
You too. Especially when I quote jverd:
Please don't do the OP's work for him. That does not help him to learn. It only encourages him to rely on others to do his work.