13 Replies Latest reply: Oct 22, 2012 9:19 PM by 969972

# PLEASE HELP

I have an assignment due really soon and I'm stuck. Here are the directions:

Write a program that implements two methods within one class file (outside of the main method).
Within this assignment, two arrays should be declared, each of 10 ‘double’ numbers.
These arrays should be traversed to print out conversions from 1-10 feet into meters, and then 20-30 Meters to Feet.

Here is my code
``````public class Conversion {
public static void main (String[] args) {

//create arrays
double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

feetToMeters(feet);
metersToFeet(meters); }

//convert from feet to meters
public static double feetToMeters(double[] feet){
for (int i = 0; i < feet.length; i++) {
*double* *metersMsr* = (0.305 * feet);
System.out.printf("%4.4f" , feet, "meters: ", metersMsr);
return metersMsr;
}
}

//convert from meters to feet
public static double metersToFeet(double[] meters){
for (int i = 0; i < meters.length; i++) {
double feetMsr = (3.280839895 * meters);
System.out.printf("%4.4f" , meters[i], "feet: " , feetMsr);
return feetMsr;
}
}

}

The errors I'm getting are in the lines in bold. I'm getting an error message that says "The operator * is undefined for the argument types double, double[].

Edited by: EJP on 19/10/2012 16:31: added {noformat}{code}{noformat} tags                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ``````
• ###### 1. Re: PLEASE HELP
You're using the entire array reference where you should be using an element of the array, as you are in the next line in each case.
• ###### 2. Re: PLEASE HELP
Oh yay thank you :D
but now its saying that the method isn't returning a double
• ###### 3. Re: PLEASE HELP
You have the return statement inside the loop. If there are zero elements in the array you will fall through the loop and then hit the place where a return statement should be. There's something wrong with all this: if you're supposed to be converting an array the output would be another array, and the return value would be double[], not double.
• ###### 4. Re: PLEASE HELP
I'm really sorry I don't understand
• ###### 5. Re: PLEASE HELP
Your methods should convert, not display. You can do the displaying afterwards.

Think about it logically:
1. Create methods to convert between feet and meters
2. Display the values in your arrays and their conversions with the methods you just made.
• ###### 6. Re: PLEASE HELP
Thank you guys for being patient I feel ridiculous. I'm sorry.
So basically I need to separate the display from the method I'm using to convert?
• ###### 7. Re: PLEASE HELP
now my output is wrong :/
`````` public class Conversion {
public static void main (String[] args) {

//create arrays
double[] feet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double[] meters = {20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

feetToMeters(feet);
metersToFeet(meters); }

//convert from feet to meters
public static double feetToMeters(double[] feet){
for (int i = 0; i < feet.length; i++) {
double meterMsr = (.0305 * feet);
return meterMsr;}

}

//convert from meters to feet
public static double metersToFeet(double[] meters){
for (int i = 0; i < meters.length; i++) {
double feetMsr = (3.280839895 * meters[i]);
return feetMsr;
}
}

System.out.printf("%4.4f" + meters[i] + "feet: " + feetMsr) + feet[i] + "meters: " + meterMsr);

}
Edited by: 966436 on Oct 18, 2012 11:59 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ``````
• ###### 8. Re: PLEASE HELP
Are you sure the methods are supposed to take the whole array as parameter?
It doesn't make sense that it would take an array of doubles, but only return a single double. Most likely it's supposed to take 1 double as a parameter and convert it to another double, which it returns.

Also you should do the displaying in the main method, not in the conversion methods as I said before.
• ###### 9. Re: PLEASE HELP
Try to understand the code dude, don't just try to fix it. It doesn't help you to get the code to a point where it compiles and does something, having to run back to this forum every time you do something is a huge waste of your time. If there is something in the code you don't understand how it works, look it up in the java tutorial (I hope you have a book though...) and try to understand it.

http://docs.oracle.com/javase/tutorial/
• ###### 10. Re: PLEASE HELP
ok so my display should be before the conversion methods?
Also, I'm not sure. Its supposed to display a table in which 1 - 10 are feet to meters and 20 - 30 are meters to feet
kind of like this:

Feet meters| Meters Feet
_1... Y... .... | 20... X_
• ###### 11. Re: PLEASE HELP
966436 wrote:
ok so my display should be before the conversion methods?
It doesn't matter in which order you define the methods, only the order in which you call them.
Since by default the main method is called first, you can display the results in a loop, using the conversion methods you have defined.

Also, I'm not sure. Its supposed to display a table in which 1 - 10 are feet to meters and 20 - 30 are meters to feet
kind of like this:

Feet meters| Meters Feet
_1... Y... .... | 20... X_
Well, you can easily do that with printf for example. You just need to first display the table header and then loop X times (where X is the number of elements in the arrays).
Inside the loop you'd then display the element in the array and it's value after conversion.
• ###### 12. Re: PLEASE HELP
Thank you so much for your help I really appreciate it
• ###### 13. Re: PLEASE HELP
public class Conversion {

MODERATOR ACTION: removed. Don't post homework answers here. It is not the purpose of these forums, and it is not the purpose of homework either. And when you do post code please format it properly.

Edited by: EJP on 23/10/2012 11:10