# Question about methods, temperature conversion program

• November 23rd, 2012, 07:53 AM
aivory616
Question about methods, temperature conversion program
Hey everyone i want to start by saying i am very new to java so i had an issue i wanted to see if anyone can give me a solution. i basically have to make a program that displays a table that shows temperatures from celsius to fahrenheit from 40 degrees celsius down to 31 degrees celsius and convert them to fahrenheit on one side of the table, and on the other side of the table have 120 fahrenheit down to 30 degrees fahrenheit and convert them to celsius.

here is my code so far (i have not yet done the print line to display any of this in a table yet)

public class celsius{

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 am getting a cannot find symbol error in line 7 i think it has something to do with not declaring the variable fahrenheit somewhere but i dont know where.

any help is appreciated
• November 23rd, 2012, 08:05 AM
aivory616
Re: Question about methods, temperature conversion program
this is how the program is supposed to print out when it is finished Attachment 1547
• November 23rd, 2012, 11:07 AM
curmudgeon
Re: Question about methods, temperature conversion program
Suggestions:
• Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.
• Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.
• Plan out the logic of your program before committing it to code as this will save you most of the above grief.
• When posting code here, you'll want to wrap your code in code tags: [code] goes above your code block and [/code] goes below it. This will help your code retain its formatting and thus be readable.
• Keep trying. If you still get stuck, show us your latest code (with code tags), and ask your questions.
• November 23rd, 2012, 11:12 AM
aivory616
Re: Question about methods, temperature conversion program
Quote:

Originally Posted by curmudgeon
Suggestions:
• Don't use variables before declaring them. You are correct in that you're trying to use a variable, fahrenheit before it has been declared. The intro to Java tutorials will show you how to properly declare and use variables.
• Check your for loop conditions. Your first for loop will never loop since its condition, that celsius must be less than 31.0 will not be true when the loop begins.
• Plan out the logic of your program before committing it to code as this will save you most of the above grief.
• When posting code here, you'll want to wrap your code in code tags: [code] goes above your code block and [/code] goes below it. This will help your code retain its formatting and thus be readable.
• Keep trying. If you still get stuck, show us your latest code (with code tags), and ask your questions.

thanks for the tips, check back later and i will update you
• November 23rd, 2012, 11:26 AM
curmudgeon
Re: Question about methods, temperature conversion program
Quote:

Originally Posted by aivory616
thanks for the tips, check back later and i will update you

Good. More specific tips, this line of code:

Code java:

`for(double celsius=40; celsius<=31.0; celsius--) {`

states that you are creating a double variable named celsius, you are assigning it a value of 40, you state that the loop will continue to re-loop as long as celsius is less than or equal to 31 (which is impossible at the start of the loop since it starts at 40 -- maybe you want to change this condition), and you state that with each iteration of the loop, you will subtract 1 from celsius (that looks OK).

I suggest changing that condition that makes the loop not work. I also suggest that you make celsius an int, since for loops work better with ints in general. You can always convert it to double by casting when you want it treated as a double.

Then here:
Code java:

`System.out.print(celsiusToFahrenheit(fahrenheit));`

You are saying, take a variable that doesn't exist and pass it into a method, and then print out the results of that method. Perhaps you don't want to use fahrenheit since the method name suggests that it converts a celsius value to a fahrenheit value. What temp would make more sense to pass into this method? celsius or fahrenheit? You already have a variable that you're looping with -- what does it hold? Can you use it instead?
• November 23rd, 2012, 11:35 AM
aivory616
Re: Question about methods, temperature conversion program
i appreciate your help heres my code so far

Code :

```public class celsius{   public static void main(String[] args){ double celsius = 40; double fahrenheit = 120;   for(celsius=40; celsius>=31.0; celsius--){ System.out.print(celsiusToFahrenheit(fahrenheit)); for(fahrenheit=120; fahrenheit>=30.0; fahrenheit--){ System.out.print(fahrenheitToCelsius(celsius)); } }   System.out.printf "Celsius Fahrenheit | Fahrenheit Celsius"+ "\n-------------------------------------------------------------------"); }   public static double celsiusToFahrenheit(double celsius){ double fahrenheit= 120; return celsius=(5.0/9.0) * fahrenheit - 32; }   public static double fahrenheitToCelsius(double fahrenheit){ double celsius= 40; return fahrenheit=(9.0/5) * celsius + 32; }   }```

it compiles now but does not print anything. By the end of my program i need it to look like this Attachment 1548

and i am using double instead of int because i may have decimal numbers.
• November 23rd, 2012, 12:00 PM
curmudgeon
Re: Question about methods, temperature conversion program
The code you've posted won't compile. When you fix it so that it does compile, then you'll find that it does in fact print something. Your job is to fix it so that what it prints out makes sense. Are you allowed to use arrays?
• November 23rd, 2012, 12:04 PM
aivory616
Re: Question about methods, temperature conversion program
yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish. i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.
• November 23rd, 2012, 12:10 PM
curmudgeon
Re: Question about methods, temperature conversion program
Quote:

Originally Posted by aivory616
yeah we are allowed to use arrays we just started learning something. when i compiled it it would just print out a bunch of jibberish.

It's printing the values obtained from your method but without formatting

Quote:

i feel like i am on the right track i just cant seem to figure it out and i need to hand it in soon.
We can't do this for you, and your deadline is your issue not ours, but we can help you. I suggest,
• Consider creating an 2-dimensional array of double[10][4], meaning an array with 10 rows and 4 columns which should correspond to your desired output.
• Your for loops are currently nested one inside of the other. Don't do this as it doesn't make logical sense. I would use one for loop to calculate the first two columns of the double array, and then a second for loop to calculate the 2nd two columns of the double array.
• Then after filling the array, print out the result using a for loop.
• November 23rd, 2012, 12:22 PM
aivory616
Re: Question about methods, temperature conversion program
Code :

```public static void main(String[] args){   double fahrenheit = 120; double celsius = 40;   System.out.printf ("Celsius Fahrenheit | Fahrenheit Celsius"+ "\n-------------------------------------------------------------------");   for(celsius=40; celsius>=31.0; celsius--){ System.out.print(celsiusToFahrenheit(fahrenheit)); }   for(fahrenheit=120; fahrenheit>=30.0; fahrenheit--){ System.out.print(fahrenheitToCelsius(celsius)); }   }   public static double celsiusToFahrenheit(double celsius){ double fahrenheit= 120; return celsius=(5.0/9.0) * fahrenheit - 32; }   public static double fahrenheitToCelsius(double fahrenheit){ double celsius= 40; return fahrenheit=(9.0/5) * celsius + 32; }   }```

i am going to try and avoid using an array because we learned this after this assignment was given and i think he wants us to solve it without it.
• November 23rd, 2012, 12:27 PM
curmudgeon
Re: Question about methods, temperature conversion program
Then you will want to use a single for loop that does for (int i = 0; i < 10; i++) and calculate the four numeric values inside of the loop, and prints out the row of data. You will need to reduce your celsius1 and your fahrenheit1 variables inside the loop and use this to calculate your fahrenheit2 and celsius2 results, then print out celsius1 fahrenheit2 fahrenheit1 celsius1 on each line, with proper formatting of course.