Link to home
Start Free TrialLog in
Avatar of lovemycomputers
lovemycomputers

asked on

Arrays and methods

I have to you’ll create an array of 20 values in dollars and cents format ($1.59). Then create four separate methods within the program to complete the listed tasks. The first method should create a total of all of the values listed in the array. The second should search the array, and return a listing of all values that are less than $5.00. The third should return the average of the values in the array. (Use the output from the first method.) Finally, display a list of all values in the array that are greater than the average value.

I keep getting these errors :  ----jGRASP exec: javac -g Prices.java

Prices.java:3: missing method body, or declare abstract
      public static void main(String[] args);
                         ^
Prices.java:32: operator + cannot be applied to double,double[]
   total +=prices;
         ^
Prices.java:41: operator < cannot be applied to double[],double
      if (prices < 5.00);
                 ^
Prices.java:46: incompatible types
found   : int
required: boolean
      if (i / 20)
            ^
Prices.java:50: operator > cannot be applied to double[],double
      if (prices > average)
                 ^
5 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
 Prices.java
Avatar of for_yan
for_yan
Flag of United States of America image



public static void main(String[] args);  <--- should not have semicolon in the end of this line

The rest of the errors after such error - you don;t need to look at  - verything is screwed up - recompile again
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lovemycomputers
lovemycomputers

ASKER

Do you mean like this??
Prices.java
No,

Look at my explanation above.
You cannot write like this:

   total +=prices; 

Open in new window


prices - is array - the whole array
total is double

you cannot add array to double

you can only add one number to another number
and not array tio a number

you should chanage to

total += prices[i];

Open in new window


here you are adding one element of the array; el;ement is double
so you are adduiing two numbers together, not array to the number

And there are many places tlike that - correct all of them, or look at the code i pasted abive - I
corrected these cases.

main thing - understand it


OHHH ok i get it now I had to add all the prices together using the brackets. Now I run the program and get this:
----jGRASP exec: java Prices

The total of all prices are 288.49999999999994
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
      at Prices.main(Prices.java:38)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
 Why isn't the values less than $5.00 showing or the average. I created the method for it.

Prices.java
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok I've got that but how would I go about displaying the average of the prices? Would it be
average =total/20; ??
Prices.java
Yes it would be and you do not need to print it in the lloop it is just
one onumber
Just print it

System.out.println("Average: " + average);

without any loop.


Also understand that your for lopp can apply either to one operator - then
you don't need vbraces, like there:

for (i = 0; i < 20;i++)System.out.println("i : " + i);

this one opertor will be repeated 20 times with changing i

if you have more than one operator repeating or say "if"  with operator then you need to put braces 
arounfd the group which needs to be repaeted.

In order not to worry - I recommedn to put braces always - both in this case:

for (i = 0; i < 20;i++){
	if (prices[i] > average)
	System.out.println(prices[i] + " are greater than the average value");}

and even in this case:


for (i = 0; i < 20;i++){
System.out.println("i : " + i);
}

then you'll not have to worry and will not make mistake executing only one operator of te intended group

and it is easier to see the logic

So correct in this way all your loops

Open in new window










Okay so to print the values that are greater than the average value am I supposed to write it like this:?
System.out.println(  "is greater than the average value");
It will find it on its own? Prices.java
No!
Why would it find anything on its own ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much its much easier now. Sorry for all your trouble.
You are always welcome.