• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

program with the dreaded for loop

 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HELLO!!!
I am writing a program to convert Celsius to Fahrenheit using a for loop. Since I have such problems with for loops and LOGIC in general, I need help! Here is a sample of what I've written so far. There is an output in the textbook that needs to be mirrored in the program that decrements the C temp from 40 to 31 and prints out the F equivalent. Please give me some clues and pointers!!!

package Chapter4;
import Chapter2.MyInput;
public class ConvertTemp
{
//Main method
public static void main(String[] args)
{
System.out.println("Cels.Temp. Fahr.Temp.");
System.out.println("------------------------------");
}
}
//Class containing C to F conversion method
class CelsToFahr{
//Find the Fahrenheit equivalent for Celsius value
public static double CelsToFahr(double Cels)
{
double Fahr = 32.00;
// Fahr= ((9/5)*Cels)+32;
// for(Cels = 0; Cels < Fahr; Cels++)
do
{
Fahr = ((9.0/5.0)*Cels)+32;
Cels = (Fahr - 32.0)*(5.0/9.0);
//if(Cels == 40.00)
// Fahr= ((9/5)*Cels)+32;
}
while(Cels == 40.00);
// return Fahr;
System.out.println(Fahr);
}
}

 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear MaryEllen,
Check out the tutorial at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html for help with a for loop. Here's a start:
 
Matts Smith
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
What I would recommend is to write an algorithm before you code.
in this case it would be something like that:
-----
get the value in celsius (can be something like 'double cels = 32.0' or user input)
convert it to farhaneit (i.e. apply the calculation)
return the result (System.out)
-----
no for loops are needed to solve your problem.
hope it helps.
 
MaryEllen Volb
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt,
thank you for you reply....however, the assignment is a PAIN in that it requires the use of a for loop. I can usually solve them without, but can NEVER solve them with!!! Thanks, keep helping!!!
Mary Ellen
 
Ambi Raj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mary,
I agree with Matt that it is infinitely better to have an algorithm beforehand. As I see it, you need to use the for loop as part of your exercise. So your psuedocode would be something like this.
int celsius;
for(celsius=42;celsius>31;celsius--)
{
double result= celsiusToFaren(celsius);
display(result)
}
Obviously, you will need to write the definitions of the
methods for "celciusToFaren" and "display".
Hope this helps
Ambi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic