1 2 Previous Next 16 Replies Latest reply: Oct 17, 2012 9:16 AM by 962826 Go to original post RSS
      • 15. Re: feet to meters using arrays and methods
        abillconsl
        Each time you return a result from the two methods put that result into an array. There are differing ways to do it, but for brevity, I would just place results from feetToMeters into a meterResults array, etc. When done, print out the results from both:
        for ( ... )
          System.out.print("feet: "+feetResults[i]+ " | meters: "+meterResults); 
        • 16. Re: feet to meters using arrays and methods
          962826
          Thanks for everyone's help. I just wanted to share the final result for this - it works perfectly and all of your input helped me learn and understand a little bit more. Look for my next post when I get my next assignemnt haha. Thanks again!
          public class FeetMeters {
               
               public static void main (String[] args) {
                    //Create feet array
                    double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                     
                    //Create meters array
                    double[] meters = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
                    
                    
                    //Create Table Header
                    System.out.println("Feet\t" + "Meters\t" + "|\t" + "Meters\t" + "Feet");
                    
                    //Use for loop to populate table
                    System.out.println("-----------------------------------------");
                    for (int i = 0; i < 10; i++) {
                           System.out.printf("%3.1f%1s%5.4f%1s%2s%3.1f%1s%6.4f\n" , feet, "\t" , footToMeter(feet[i]) , "\t" , "|\t" , meters[i] , "\t" , meterToFoot(meters[i]));
                    
                    }
                    
          }
               /** 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 (meter*3.280839895);
               }
          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
          1 2 Previous Next