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
Java

Avatar of undefined
Last Comment
for_yan

8/22/2022 - Mon
for_yan



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
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lovemycomputers

ASKER
Do you mean like this??
Prices.java
for_yan

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


I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
lovemycomputers

ASKER
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
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lovemycomputers

ASKER
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
for_yan

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










⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lovemycomputers

ASKER
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
for_yan

No!
Why would it find anything on its own ?
ASKER CERTIFIED SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
lovemycomputers

ASKER
Thank you so much its much easier now. Sorry for all your trouble.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
for_yan

You are always welcome.