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

Thread: Question about methods, temperature conversion program

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about methods, temperature conversion program

    this is how the program is supposed to print out when it is finished Screen Shot 2012-11-23 at 9.04.15 AM.png

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about methods, temperature conversion program

    Quote Originally Posted by curmudgeon View Post
    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

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Question about methods, temperature conversion program

    Quote Originally Posted by aivory616 View Post
    thanks for the tips, check back later and i will update you
    Good. More specific tips, this line of code:

    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:
    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?

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about methods, temperature conversion program

    i appreciate your help heres my code so far

    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 Screen Shot 2012-11-23 at 9.04.15 AM.png

    and i am using double instead of int because i may have decimal numbers.

  7. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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?

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Question about methods, temperature conversion program

    Quote Originally Posted by aivory616 View Post
    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

    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.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about methods, temperature conversion program

    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.

  11. #11
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

Similar Threads

  1. Temperature Conversion Chart
    By cb12991 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 8th, 2012, 12:11 AM
  2. Question to do with methods.
    By Dragonkndr712 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 31st, 2011, 11:56 PM
  3. numerical conversion methods..
    By Neo in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 17th, 2011, 11:51 PM
  4. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM
  5. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM