# Thread: Is this ok?

1. Member
Join Date
Jan 2011
Posts
18
Rep Power
0

## Is this ok?

I found the following question on the web
"Write a program that declares three one-dimensional arrays referenced by the variables named price, quantity, and amount. Each array should be declared in main() and should be capable of holding 10 double-precision numbers. The numbers that should be stores in price are 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, and 3.98. The numbers that should be stored in quantity are 4, 8, 6, 7, 9, 15, 3, 5, 2, and 4. Your program should pass references to these arrays to a method named extend(), which should calculate the elements in the amount array as the product of the corresponding elements in the price and quantity arrays (for example, amount[1] = price[1] * quantity[1]). After extend() has put values into the amount array, the values in the array should be displayed from within main()."

The attempted code seems a bit long to me. I tried the one below and it gives me the answers. Could you give me comments about this. Thanks.

Java Code:
```public class Shop {

public static void main(String[] args){

double[] price = {10.62, 14.89, 13.21, 16.55, 18.62                       , 9.47, 6.58, 18.32, 12.15, 3.98};
int[] quantity = {4, 8, 6, 7, 9, 15, 3, 5, 2, 4};

Shop tula = new Shop();
tula.extend(quantity, price);
}

public double[] extend(int[] quantity, double[] price){
double [] amount = new double [10];
for (int i = 0; i < 10; i++) {
amount[i] = price[i]*quantity[i];
System.out.println(amount [i]);
}
return amount;
}
}```
Last edited by JosAH; 09-25-2011 at 09:59 AM. Reason: added [code] ... [/code] tags

2. ## Re: Is this ok?

Your code looks fine to me (as a matter of fact it looks much better than a lot of other code people post here ;-) Strictly speaking you don't need a Shop object if you make the extend( ... ) method static ... one little thing: the assignment wants you to display those values in the main method, not in the extend( ... ) method.

kind regards,

Jos

3. Member
Join Date
Jan 2011
Posts
18
Rep Power
0

## Re: Is this ok?

thaks, jos, that is helpful.

#### Posting Permissions

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