public class Conversion {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double meter [] = new double [10];
double feet [] = new double [10];
//Header for feet to meters
System.out.println("Feet" + "\t\t\t" + "Meters");
//for loop to call foot to meter conversion and print table
for(x=1; x<=10; x++) {
footToMeter(x);
System.out.printf("%7.4f%20.3f" , x, meter);
System.out.println();
}
//Header for meters to feet
System.out.println("Meters" + "\t\t\t" + "Feet");
//for loop to call foot method and print out table
for(y = 20; y<=30; y = y+5)
{
feet = meterToFeet(y);
System.out.printf("%7.4f%20.3f" , y, feet);
System.out.println();
}
}
//method to calculate conversion to meters
public static double footToMeter(double [] feet) {
return .3048*feet;
}
//method to calculate conversion to feet
public static double meterToFeet(double [] meter) {
return 3.2808398995*meter;
}
}
Feet to Meters/Meters to Feet- Passing Arrays
Page 1 of 12 Replies - 320 Views - Last Post: 14 October 2012 - 05:18 PM
#1
Feet to Meters/Meters to Feet- Passing Arrays
Posted 14 October 2012 - 09:04 AM
I am very lost in with my assignment. The task is to declare 2 arrays of 10 double numbers, use 1 method to convert 1 - 10 feet into meters and use another method to convert 20 - 30 meters into feet. I need to utilize loops to print the table. I have the following code that I constructed using some examples in my book but it doesn't use and pass the arrays. Help?
Replies To: Feet to Meters/Meters to Feet- Passing Arrays
#2
Re: Feet to Meters/Meters to Feet- Passing Arrays
Posted 14 October 2012 - 09:13 AM
You're getting errors, right? Something like:
You should post errors when you get them, just as I've done above.
Do you not understand those errors, or did you not see them? In case it's the former, you're using x and y as loop control variables, but you don't declare them. Quite often, that's done:
for ( int x = 1; x <= 10; x++ )
Adding the 'int' before the first x declares and initializes the loop control variable, x. Can you do the same for 'y'?
After you've made those changes, come back with any new problems you need help with.
Exception in thread "main" java.lang.Error: Unresolved compilation problems: x cannot be resolved to a variable x cannot be resolved to a variable x cannot be resolved to a variable x cannot be resolved to a variable x cannot be resolved to a variable y cannot be resolved to a variable y cannot be resolved to a variable y cannot be resolved to a variable y cannot be resolved to a variable y cannot be resolved to a variable y cannot be resolved to a variable at Conversion.main(Conversion.java:17)
You should post errors when you get them, just as I've done above.
Do you not understand those errors, or did you not see them? In case it's the former, you're using x and y as loop control variables, but you don't declare them. Quite often, that's done:
for ( int x = 1; x <= 10; x++ )
Adding the 'int' before the first x declares and initializes the loop control variable, x. Can you do the same for 'y'?
After you've made those changes, come back with any new problems you need help with.
#3
Re: Feet to Meters/Meters to Feet- Passing Arrays
Posted 14 October 2012 - 05:18 PM
I added the int to intialize the x and y variables in the loops and now I get an error on the feetToMeters and metersToFeet variables that says "The method meterToFeet(double[]) in the type Conversion is not applicable for the arguments int".
Also, I am getting an error in both methods for caluclating the conversions that says "the * operator is undefined for the argument type double, double[]".
Also, I am getting an error in both methods for caluclating the conversions that says "the * operator is undefined for the argument type double, double[]".
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|