We help IT Professionals succeed at work.

Arrays and methods

lovemycomputers
lovemycomputers asked
on
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
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:


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
Awarded 2011
Awarded 2011
Commented:
public class Prices
{
	public static void main(String[] args)
	{
	
	double[] prices = new double[20];
	prices[0] = 2.36;
	prices[1] = 13.43;
	prices[2] = 6.32; 
	prices[3] = 12.36; 
	prices[4] = 9.32;
	prices[5] = 4.78;
	prices[6] = 3.16; 
	prices[7] = 11.64; 
	prices[8] = 4.98;
	prices[9] = 8.36; 
	prices[10] = 1.03;
	prices[11] = 7.15;
	prices[12] = 4.65;
	prices[13] = 6.54;
	prices[14] = 14.28;
   prices[15] = 7.17;
	prices[16] = 5.36;
	prices[17] = 6.34;
	prices[18] = 10.25;
	prices[19] = 4.77;
	double total = 0.0; //you need yo initilize total to zero
	double average;
	int i; 
	for (i = 0; i < 20;i++);
   {
   total +=prices[i]; // you cannot use prices without index in math experssion ; without index - it is the whole array, you need index when adding, etc
	}
	{
   total = 2.36 + 13.43 + 6.32 + 12.36 + 9.32+ 4.78 + 3.16 + 11.64 + 4.98
	+ 8.36 + 1.03 + 7.15 + 4.65 + 6.54 + 14.28 + 7.17 + 5.36 + 6.34 + 10.25 + 4.77;
	for (i = 0; i < 20;i++)
		System.out.println("The total of all prices are" + total);
		
	for (i = 0; i < 20;i++);
	if (prices[i] < 5.00);
	System.out.println("The values less than $5.00 are" + prices);
	
	 average = total/20;
	for (i = 0; i < i;i++)
	if (i / 20)   // still error - sdon't understand what you had in mind:  if requires boolena - nit just i/20 which is int, you need boolean if((i/20) >1) or something like that
	System.out.println("The average of all prices is" + average);
	
	for (i = 0; i < 20;i++);
	if (prices[i] > average)
	System.out.println(prices + "are greater than the average value");
	}
}
}



	

Open in new window

Author

Commented:
Do you mean like this??
Prices.java
Awarded 2011
Awarded 2011

Commented:
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


Author

Commented:
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
Awarded 2011
Awarded 2011
Commented:
check this:

public class Prices
{
	public static void main(String[] args)
	{

	double[] prices = new double[20];
	prices[0] = 2.36;
	prices[1] = 13.43;
	prices[2] = 6.32;
	prices[3] = 12.36;
	prices[4] = 9.32;
	prices[5] = 4.78;
	prices[6] = 3.16;
	prices[7] = 11.64;
	prices[8] = 4.98;
	prices[9] = 8.36;
	prices[10] = 1.03;
	prices[11] = 7.15;
	prices[12] = 4.65;
	prices[13] = 6.54;
	prices[14] = 14.28;
   prices[15] = 7.17;
	prices[16] = 5.36;
	prices[17] = 6.34;
	prices[18] = 10.25;
	prices[19] = 4.77;
	double total = 0.0;
	double average;
	//int i;
	for (int i = 0; i < 20;  i++)
   {
      
   total +=prices[i];
	}

   total = 2.36 + 13.43 + 6.32 + 12.36 + 9.32+ 4.78 + 3.16 + 11.64 + 4.98
	+ 8.36 + 1.03 + 7.15 + 4.65 + 6.54 + 14.28 + 7.17 + 5.36 + 6.34 + 10.25 + 4.77;
	for (int i = 0; i < 20;i++)
		System.out.println("The total of all prices are " + total);

	for (int i = 0; i < 20;i++){
	if (prices[i] < 5.00);
	System.out.println("The values less than $5.00 are " + prices[i]);
    }

	 average = total/20;
	for (int i = 0; i < 20;i++) {
	if((i/20) >1)
	System.out.println("The average of all prices i s" + average); }

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

Open in new window

Awarded 2011
Awarded 2011
Commented:
there were many mistakes

loop variables should be initialized within a loop (not a lwa, but woul be normal prcatice and des not casue confusion);

for(int i=0; i<20; i++)

noy like that:

int i;

for( i=0; i<20; i++)

never put semicolon after loop initilaization like that:

for(int i=0; i<20; i++);

this detaches the loop from the real body of the loop.

It should be

for(int i=0; i<20; i++)  \*(no semicolon here!)*/ {


}

hwhen you print  don't wrtite  "prices"

again only with the index attached -
you cannot print whole array, you ned to print element of the array







Author

Commented:
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
Awarded 2011
Awarded 2011

Commented:
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










Author

Commented:
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
Awarded 2011
Awarded 2011

Commented:
No!
Why would it find anything on its own ?
Awarded 2011
Awarded 2011
Commented:
That is how it should be.

pay attention to waht I wrote above - you cannot
have semicolon after loop condition but before the
body of the loop:

this is wring:

for(int i=0; i,20; i++);  <---- this smeicolon completely sspoils every thing

it should be

for(int i=0; i,20; i++) {
// then waht is here will be executed 20 times
}





public class Prices
{
	public static void main(String[] args)
	{

	double[] prices = new double[20];
	prices[0] = 2.36;
	prices[1] = 13.43;
	prices[2] = 6.32;
	prices[3] = 12.36;
	prices[4] = 9.32;
	prices[5] = 4.78;
	prices[6] = 3.16;
	prices[7] = 11.64;
	prices[8] = 4.98;
	prices[9] = 8.36;
	prices[10] = 1.03;
	prices[11] = 7.15;
	prices[12] = 4.65;
	prices[13] = 6.54;
	prices[14] = 14.28;
   prices[15] = 7.17;
	prices[16] = 5.36;
	prices[17] = 6.34;
	prices[18] = 10.25;
	prices[19] = 4.77;
	double total = 0.0;
	double average;
	//int i;
	for (int i = 0; i < 20;  i++)
   {
      
   total +=prices[i];
	}

   total = 2.36 + 13.43 + 6.32 + 12.36 + 9.32+ 4.78 + 3.16 + 11.64 + 4.98
	+ 8.36 + 1.03 + 7.15 + 4.65 + 6.54 + 14.28 + 7.17 + 5.36 + 6.34 + 10.25 + 4.77;

         System.out.println();
		System.out.printf("The total of all prices are  %6.2f", total);

        System.out.println();
         System.out.println("");    

	for (int i = 0; i < 20;i++){
	if (prices[i] < 5.00)
	System.out.println("The values less than $5.00 are " + prices[i]);
    }

         System.out.println("");
	 average = total/20;

	System.out.printf("The average of all prices is %6.2f", average);

     System.out.println("");
         System.out.println("");

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

Open in new window


Output:
The total of all prices are  144.25

The values less than $5.00 are 2.36
The values less than $5.00 are 4.78
The values less than $5.00 are 3.16
The values less than $5.00 are 4.98
The values less than $5.00 are 1.03
The values less than $5.00 are 4.65
The values less than $5.00 are 4.77

The average of all prices is   7.21

13.43 are greater than the average value 
12.36 are greater than the average value 
9.32 are greater than the average value 
11.64 are greater than the average value 
8.36 are greater than the average value 
14.28 are greater than the average value 
10.25 are greater than the average value 

Open in new window

Author

Commented:
Thank you so much its much easier now. Sorry for all your trouble.
Awarded 2011
Awarded 2011

Commented:
You are always welcome.