1 2 Previous Next 16 Replies Latest reply: Oct 17, 2012 9:16 AM by 962826 RSS

    feet to meters using arrays and methods

    962826
      The assignment is to create two arrays with double data types: 1) 1-10 for feet to meters conversion and 2) 20-30 for meters to feet conversion.

      Then to write a program that uses two methods: 1) public static double footToMeter (double foot) and 2) public static double meterToFoot (double meter) within one class file outside of the main method.

      The formulas for the conversions are:
      Feet to Meters = 0.305 * foot (1 ft = 0.3048 meters ) -- 1.0 feet = 0.305 meters
      Meters to Feet = Meters * 3.280839895     -- 20.0 Meters = 65.574 Feet

      I am having coding paralysis. As a novice programmer, this may just be outside of my knowledge. I need help and a little explanation. I created the arrays. Here is my outline for solving this:

      1) put feet in array

      2) put meters in array

      3) write method feet to meters

      4) write method meters to feet

      5) take feet in array and initialize feet to meters method

      6) take meters in array and initialize meters to feet array

      7) Display results in a table
      public class FeetMeters {
           //Create feet array
           double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                            
           //Create meters array
           double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
           
           
           /** Convert from Feet to Meters **/
           public static double footToMeter(double foot){
                return (0.305*foot);     
           }
                
           /** Convert from Meters to Feet **/
           public static double meterToFoot(double meter){
                return (3.280839895*meter);
           }
      
           public static void main (String[] args) {
                
                
                
                
      }
      }
        • 1. Re: feet to meters using arrays and methods
          Kayaman
          Okay, looks good so far. You might want to add the static qualifier to your arrays, unless you intend to create an instance of the FeetMeters class.

          Now what's holding you back? You've got everything set up, why is your main method empty?
          • 2. Re: feet to meters using arrays and methods
            962826
            I guess I am not sure what you mean by adding a static modifier to my arrays. My problem is I dont know how to take each index of the arrays and perform the actual math.

            Questions:

            Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
            And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
            Do the methods need to return arrays?
            • 3. Re: feet to meters using arrays and methods
              Tolls
              restless8052 wrote:
              I guess I am not sure what you mean by adding a static modifier to my arrays. My problem is I dont know how to take each index of the arrays and perform the actual math.

              Questions:

              Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
              And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
              Do the methods need to return arrays?
              Your assignment (from your OP) is pretty clear on the methods:
              "1) public static double footToMeter (double foot) and 2) public static double meterToFoot (double meter)"

              which is what you've done.
              So the implication there is, yes, you have to loop through each array and then call the relevant method.

              I'd set that up first and output the results straight to the screen to ensure it all works.
              You can worry about how to format the output after that.

              Baby steps...
              • 4. Re: feet to meters using arrays and methods
                abillconsl
                restless8052 wrote:
                I guess I am not sure what you mean by adding a static modifier to my arrays. My problem is I dont know how to take each index of the arrays and perform the actual math.

                Questions:

                Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
                And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
                Do the methods need to return arrays?
                By static, I believe he means to use the same qualifier as you use on the methods - "static" ... that is, put static before before "double" on the array declarations. Why? because, if your methods that use the arrays are static, then in an example like this, the arrays should likely be static, as he said, unless you plan to create an instance of the class. Static is applies to the class and is not specific to an instance of the class.

                As to the other questions, yes, in this case you'll probably want to put your "for" loop in the main method and call the two methods from within the for loop, like:
                for (int i = 0; i < foot.length; i++) {
                  System.out.println(i+". Meters: "+footToMeter(foot)
                ...
                }
                • 5. Re: feet to meters using arrays and methods
                  962826
                  Thanks everyone - yeah this is my first programming class as you can tell if you look at some of the other topics I have created. I will go ahead with what you suggested and see what comes out of it. Expect a follow up post here soon :)
                  • 6. Re: feet to meters using arrays and methods
                    TPD-Opitz
                    restless8052 wrote:
                    I guess I am not sure what you mean by adding a static modifier to my arrays.
                    The point is that your arrays a member variables in an object but you never create one. This way your warrays are not accessible from your static methods.

                    The (IMHO) proper way was to remove the <tt>sattic</tt> modifier from the methods but we cannot break your assignment... :o(
                    Questions:
                    Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
                    System.out.println(foot[i] +" "+ footToMeter(foot));
                    this solves point 5 and half of point 7.
                    
                    
                    And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
                    For the ease of the moment this would go into the main method. In general it's a good idea to have verry basic errorhandling (just a <tt>try .. catch</tt> block in <tt>main</tt> that in case of an error outputs a stacktrace and gives a non *0* return value) and leave the real work to an instance of the object.
                    Do the methods need to return arrays?
                    No. Keep methods as simple and small as possible. Having them retuning an array would require more logic having nothing to do with their actual task of caculating a conversion. bye TPD                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                    • 7. Re: feet to meters using arrays and methods
                      962826
                      Tolls wrote:
                      restless8052 wrote:
                      I guess I am not sure what you mean by adding a static modifier to my arrays. My problem is I dont know how to take each index of the arrays and perform the actual math.

                      Questions:

                      Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
                      And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
                      Do the methods need to return arrays?
                      Your assignment (from your OP) is pretty clear on the methods:
                      "1) public static double footToMeter (double foot) and 2) public static double meterToFoot (double meter)"

                      which is what you've done.
                      So the implication there is, yes, you have to loop through each array and then call the relevant method.

                      I'd set that up first and output the results straight to the screen to ensure it all works.
                      You can worry about how to format the output after that.

                      Baby steps...
                      Ok so when I took the advice, I am getting a couple of errors. Here is my new code just to see if the results will print out before I go about formatting my results.
                      public class FeetMeters {
                           //Create feet array
                           static double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                            
                           //Create meters array
                           static double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
                           
                           
                           /** Convert from Feet to Meters **/
                           public static double footToMeter(double foot){
                                return (0.305*foot);     
                           }
                                
                           /** Convert from Meters to Feet **/
                           public static double meterToFoot(double meter){
                                return (3.280839895*meter);
                           }
                      
                           public static void main (String[] args) {
                                for (int i = 0; i < foot.length; i++) {
                                       System.out.println(foot[i]+".Meters: "+ footToMeter(foot));
                                
                                }
                                for (int i = 0; i < meter.length; i++) {
                                     System.out.println(meter[i]+".Meters: "+ meterToFoot(meter[i]));
                                }
                      }
                      }
                      Errors:
                      Lines 25 & 26: - foot cannot be resolved to a variable
                      
                      Lines 29 & 30: - foot cannot be resolved to a variable
                      
                      Again... stuck :-\                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                      • 8. Re: feet to meters using arrays and methods
                        Tolls
                        Your array is called 'feet'.
                        • 9. Re: feet to meters using arrays and methods
                          962826
                          restless8052 wrote:
                          Tolls wrote:
                          restless8052 wrote:
                          I guess I am not sure what you mean by adding a static modifier to my arrays. My problem is I dont know how to take each index of the arrays and perform the actual math.

                          Questions:

                          Do I set int i = 0 and then crawl through each array to perform the math? Ex. for (int i = 0; i < foot.length; i++) but then what?
                          And then does that go under the main method or under the repected methods for footToMeter and meterToFoot?
                          Do the methods need to return arrays?
                          Your assignment (from your OP) is pretty clear on the methods:
                          "1) public static double footToMeter (double foot) and 2) public static double meterToFoot (double meter)"

                          which is what you've done.
                          So the implication there is, yes, you have to loop through each array and then call the relevant method.

                          I'd set that up first and output the results straight to the screen to ensure it all works.
                          You can worry about how to format the output after that.

                          Baby steps...
                          Ok so when I took the advice, I am getting a couple of errors. Here is my new code just to see if the results will print out before I go about formatting my results.
                          public class FeetMeters {
                               //Create feet array
                               static double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                                
                               //Create meters array
                               static double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
                               
                               
                               /** Convert from Feet to Meters **/
                               public static double footToMeter(double foot){
                                    return (0.305*foot);     
                               }
                                    
                               /** Convert from Meters to Feet **/
                               public static double meterToFoot(double meter){
                                    return (3.280839895*meter);
                               }
                          
                               public static void main (String[] args) {
                                    for (int i = 0; i < foot.length; i++) {
                                           System.out.println(foot[i]+".Meters: "+ footToMeter(foot));
                                    
                                    }
                                    for (int i = 0; i < meter.length; i++) {
                                         System.out.println(meter[i]+".Meters: "+ meterToFoot(meter[i]));
                                    }
                          }
                          }
                          Errors:
                          Lines 25 & 26: - foot cannot be resolved to a variable
                          
                          Lines 29 & 30: - foot cannot be resolved to a variable
                          
                          Again... stuck :-\
                          Fixed myself:
                          public class FeetMeters {
                               //Create feet array
                               static double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                                
                               //Create meters array
                               static double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
                               
                               
                               /** Convert from Feet to Meters **/
                               public static double footToMeter(double foot){
                                    return (0.305*foot);     
                               }
                                    
                               /** Convert from Meters to Feet **/
                               public static double meterToFoot(double meter){
                                    return (3.280839895*meter);
                               }
                          
                               public static void main (String[] args) {
                                    for (int i = 0; i < feet.length; i++) {
                                           System.out.println(feet[i]+".meters: "+ footToMeter(feet));
                                    
                                    }
                                    for (int i = 0; i < meters.length; i++) {
                                         System.out.println(meters[i]+".feet: "+ meterToFoot(meters[i]));
                                    }
                          }
                          }
                          I forgot to change the names of the variables from foot to feet and meter to meters. So let me see what happens when I run it.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                          • 10. Re: feet to meters using arrays and methods
                            962826
                            Another quick question. In the example, I was given:

                            Meters to Feet = Meters * 3.280839895 and 20.0 Meters = 65.574 Feet

                            Feet     Meters          | Meters      Feet
                            1.0     0.305          | 20.0          65.574
                            2.0      0.61          | 30.0          81.967

                            My output for meters to feet is producing the following results (I still need to format into a table) and they seem a little off. Any idea why?

                            1.0 meters: 0.305 feet
                            2.0 meters: 0.61 feet
                            3.0 meters: 0.915 feet
                            4.0 meters: 1.22 feet
                            5.0 meters: 1.525 feet
                            6.0 meters: 1.83 feet
                            7.0 meters: 2.135 feet
                            8.0 meters: 2.44 feet
                            9.0 meters: 2.745 feet
                            10.0 meters: 3.05 feet
                            20.0 feet: 65.61679790000001 meters
                            21.0 feet: 68.89763779500001 meters
                            22.0 feet: 72.17847769000001 meters
                            23.0 feet: 75.45931758500001 meters
                            24.0 feet: 78.74015748000001 meters
                            25.0 feet: 82.02099737500001 meters
                            26.0 feet: 85.30183727000001 meters
                            27.0 feet: 88.582677165 meters
                            28.0 feet: 91.86351706 meters
                            29.0 feet: 95.144356955 meters
                            30.0 feet: 98.42519685 meters
                            • 11. Re: feet to meters using arrays and methods
                              abillconsl
                              Meters to Feet = Meters * 3.280839895 and 20.0 Meters = 65.574 Feet


                              The above is incorrect, the program is giving you the correct answer based on the fraction you are using.
                              • 12. Re: feet to meters using arrays and methods
                                962826
                                Okay - almost there! The table diaplay has to be as follows, using printf.

                                Feet Meters | Meters Feet
                                ______________________________
                                1.0 .035 | 20 65.617

                                so on and so on - so four columns. RIght now the way it prints is to list all of the feet to meters and then meters to feet underneath. How do I bring up the meters to feet to saddle the feet to meters. Make sense?
                                • 13. Re: feet to meters using arrays and methods
                                  abillconsl
                                  There are several ways. Probably the simplest way - which does not mean the best way, note - in this case would be to add the results to two result arrays and iterate each at the same time after all other processing is done, printing them at that time.
                                  • 14. Re: feet to meters using arrays and methods
                                    962826
                                    abillconsl wrote:
                                    There are several ways. Probably the simplest way - which does not mean the best way, note - in this case would be to add the results to two result arrays and iterate each at the same time after all other processing is done, printing them at that time.
                                    so put the results from footToMeter into an array and then put the results from meterToFoot into an array? Would that be done in their respected methods or in the main method?
                                    1 2 Previous Next