Results 1 to 8 of 8
  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default conversion between feet and meters

    I am writing a class that contains the following two methods

    public static double footToMeter(double foot)
    public static double meterToFoot(double meter)

    This is what I have



    public class conversionsBetweenFeetAndMeters
    {
    public static void main(String[] args)
    {
    // assigning variables
    double meter;
    double foot;
    double x;
    double y;

    //Header for meters to foot
    System.out.println("Meters Feet");

    //for loop to call meter method and print out table
    for(x = 1; x <=10; x++)
    {
    meter = footToMeter(x);
    System.out.printf("5.1f%20.3f" , x, foot);
    }

    //Header for foot to meters
    System.out.println("Feet Meters");

    //for loop to call foot method and print out table
    for(y = 20; y <= 65; y = y+5)
    {
    foot = meterToFoot(y);
    System.out.printf("5.1f%20.3f" , y, meter);
    }
    }

    //Converts from feet to meters
    public static double footToMeter(double foot)
    {
    return 0.305 * foot;
    }

    //Converts from meters to feet
    public static double meterToFoot(double meter)
    {
    return 3.281 * meter;
    }
    }

    I am keep getting this syntax error message: variable foot might not have been initialized.

  2. #2
    Maulin Patel is offline Member
    Join Date
    Oct 2010
    Location
    Vadodara
    Posts
    2
    Rep Power
    0

    Default

    In Java, class instance variables and static variables have default values: null for all object types, false for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults

    There is rule that local variables must be first initialized before they are first read.

    so u might have to declare like this:

    double meter =0, foot =0;

  3. #3
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Maulin Patel View Post
    In Java, class instance variables and static variables have default values: null for all object types, false for boolean primitive and 0 for numeric primitives. But local variables inside a method have no defaults

    There is rule that local variables must be first initialized before they are first read.

    so u might have to declare like this:

    double meter =0, foot =0;
    ok. now when I run the program my formula is incorrect

    Java Code:
    public class conversionsBetweenFeetAndMeters
    {
        public static void main(String[] args)
        {
            // assigning variables
            double meter = 0;
            double foot =  0;
            double x;
            double y;
            
            //Header for meters to foot
            System.out.println("  Feet                Meters");
            
            //for loop to call meter method and print out table
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                System.out.printf("%5.1f%20.3f" , x, foot);
                System.out.println();
            }
            
            //Header for foot to meters
            System.out.println("Meters               Feet");
            
            //for loop to call foot method and print out table
            for(y = 20; y <= 65; y = y+5)
            {
                foot = meterToFoot(y);
                System.out.printf("%5.1f%20.3f" , y, meter);
                System.out.println();
            }
        }
        
        //Converts from feet to meters
        public static double footToMeter(double foot)
        {
          return 0.305 * foot;  
        }
        
        //Converts from meters to feet
        public static double meterToFoot(double meter)
        {
           return 3.281 * meter; 
        }
    }

    Java Code:
     Feet                Meters
      1.0               0.000
      2.0               0.000
      3.0               0.000
      4.0               0.000
      5.0               0.000
      6.0               0.000
      7.0               0.000
      8.0               0.000
      9.0               0.000
     10.0               0.000
    Meters               Feet
     20.0               3.050
     25.0               3.050
     30.0               3.050
     35.0               3.050
     40.0               3.050
     45.0               3.050
     50.0               3.050
     55.0               3.050
     60.0               3.050
     65.0               3.050

    It should be

    Java Code:
    feet                meters
    1.0                 0.305
    2.0                 0.61     and etc...all the way to 10 and
    
    
    meters            feet
    20.0               65.574
    25.0               81.967    and etc...all the way down to 65
    Moderator Edit: Code tags added
    Last edited by Fubarable; 10-10-2010 at 07:24 PM. Reason: Moderator Edit: Code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:
    Java Code:
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                System.out.printf("%5.1f%20.3f" , x, foot);
                System.out.println();
            }
    You're printing out foot result, but your code never changes foot, but rather it sets x and meter.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Much luck!

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    .....

    meter = footToMeter(x);
    System.out.printf("%5.1f%20.3f" , x, foot);
    and
    foot = meterToFoot(y);
    System.out.printf("%5.1f%20.3f" , y, meter);

    --->

    meter = footToMeter(x);
    System.out.printf("%5.1f%20.3f" , x, meter);
    and
    foot = meterToFoot(y);
    System.out.printf("%5.1f%20.3f" , y, foot);

  6. #6
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Here:
    Java Code:
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                System.out.printf("%5.1f%20.3f" , x, foot);
                System.out.println();
            }
    You're printing out foot result, but your code never changes foot, but rather it sets x and meter.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag
    Java Code:
     above your pasted code and the tag
    below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Much luck!
    Java Code:
    public class conversionsBetweenFeetAndMeters
    {
        public static void main(String[] args)
        {
            // assigning variables
            double meter = 0;
            double foot =  0;
            double x;
            double y;
            
            //Header for meters to foot
            System.out.println("  Feet                Meters");
            
            //for loop to call meter method and print out table
            for(x = 1; x <=10; x++)
            {
                meter = footToMeter(x);
                System.out.printf("%5.1f%20.3f" , x, meter);
                System.out.println();
            }
            
            //Header for foot to meters
            System.out.println("Meters               Feet");
            
            //for loop to call foot method and print out table
            for(y = 20; y <= 65; y = y+5)
            {
                foot = meterToFoot(y);
                System.out.printf("%5.1f%20.3f" , y, foot);
                System.out.println();
            }
        }
        
        //Converts from feet to meters
        public static double footToMeter(double foot)
        {
          return 0.305 * foot;  
        }
        
        //Converts from meters to feet
        public static double meterToFoot(double meter)
        {
           return 3.281 * meter; 
        }
    }

    Thank you and did I place the code tags correctly?

  7. #7
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Thank you guys.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. XLS to PDF conversion
    By nitin2k2k in forum Advanced Java
    Replies: 18
    Last Post: 07-02-2013, 11:17 AM
  2. program with converting feet to meters
    By Smitty300 in forum New To Java
    Replies: 5
    Last Post: 03-16-2010, 11:41 AM
  3. Getting my feet wet (looking for a mentor as well)
    By fresh83 in forum New To Java
    Replies: 9
    Last Post: 12-18-2009, 06:12 AM
  4. Doc to Pdf conversion
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2009, 12:27 PM
  5. Conversion from wav to vox
    By bozovilla in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2008, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •