7 Replies - 1237 Views - Last Post: 25 May 2009 - 05:46 PM Rate Topic: -----

#1 angelfish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-May 09

Need some help with arrays

Post icon  Posted 24 May 2009 - 07:38 PM

Hi there. I am just learning java and i have an assignment i am stuck on.
I am to write a program that will prompt the user for a list of 5 prices and display the sum of all the prices, the average of the prices and all the prices that are higher than the calculated average.

I need 3 methods SumArray that should receive an array and return the sum. Produces no output. AvgPrice that should receive and array and return the average. Produces no output. highPrices should recieve an array and an average. It should print out the numbers that are greater than the average.

Here is what I have come up with:

 import java.util.*;
public class Price
{
	public static void main(String[] args)
	{
		
		double[] prices = {3.14, 1.49, 0.99, 3.26, 4.11};	

		for(int i=0; i < prices.length; ++i)
			{
			System.out.println("Please enter five prices: ");
			Scanner sc = new Scanner(System.in);			

			sumArray (prices[i]);
			System.out.print("Thank you! Total: " + totalPrice);
			averageArray (prices[i]);
			System.out.print("Average: " + avgPrice);
			highPrices (prices[i]);			
			System.out.print("High Prices: " + high1, high2);
			}
	}

	public static double sumArray(double totalPrice[])
	{
		
		double sum;
		totalPrice = 0.0;
		

			  for(int i=0; i<= 4; i++)
		 		sum = sum + totalPrice[i];
		return sum;
	}
	
	public static double[] averageArray(double totalPrice[])
	{
		double totalPrice;
		double avgPrice;
		totalPrice = 0.0;

			  for(int i=0; i<= 4; i++)
		 		sum = sum + totalPrice[i];
		
		avgPrice = totalPrice/5;
		return avgPrice;
	}
	
	public static double[] highPrices(double high1, double high2, double low)
	{
		prices[0] = 3.14;
		prices[1] = 1.49;
		prices[2] = 0.99;
		prices[3] = 3.26;
 		prices[4] = 4.11;

		int high1 = prices[5];
		for (int i=0; i < prices.length; i++)
			{
			if (prices[i] > high1){
				high1 = prices[i];}
			}
		for (int i=1; i < prices.length; i++)
			{
			if (prices[1] . high2){
				high2 = prices[1];}
			}
		return high1;
		return high2;
	}
}



Can someone help me? I'm getting these errors.

Price.java:14: sumArray(double[]) in Price cannot be applied to (double)
sumArray (prices[i]);
^
Price.java:15: cannot find symbol
symbol : variable totalPrice
location: class Price
System.out.print("Thank you! Total: " + totalPrice);
^
Price.java:16: averageArray(double[]) in Price cannot be applied to (double)
averageArray (prices[i]);
^
Price.java:17: cannot find symbol
symbol : variable avgPrice
location: class Price
System.out.print("Average: " + avgPrice);
^
Price.java:18: highPrices(double,double,double) in Price cannot be applied to (double)
highPrices (prices[i]);
^
Price.java:19: cannot find symbol
symbol : variable high1
location: class Price
System.out.print("High Prices: " + high1, high2);
^
Price.java:19: cannot find symbol
symbol : variable high2
location: class Price
System.out.print("High Prices: " + high1, high2);
^
Price.java:27: incompatible types
found : double
required: double[]
totalPrice = 0.0;
^
Price.java:37: totalPrice is already defined in averageArray(double[])
double totalPrice;
^
Price.java:42: cannot find symbol
symbol : variable sum
location: class Price
sum = sum + totalPrice[i];
^
Price.java:42: cannot find symbol
symbol : variable sum
location: class Price
sum = sum + totalPrice[i];
^
Price.java:42: array required, but double found
sum = sum + totalPrice[i];
^
Price.java:45: incompatible types
found : double
required: double[]
return avgPrice;
^
Price.java:50: cannot find symbol
symbol : variable prices
location: class Price
prices[0] = 3.14;
^
Price.java:51: cannot find symbol
symbol : variable prices
location: class Price
prices[1] = 1.49;
^
Price.java:52: cannot find symbol
symbol : variable prices
location: class Price
prices[2] = 0.99;
^
Price.java:53: cannot find symbol
symbol : variable prices
location: class Price
prices[3] = 3.26;
^
Price.java:54: cannot find symbol
symbol : variable prices
location: class Price
prices[4] = 4.11;
^
Price.java:56: high1 is already defined in highPrices(double,double,double)
int high1 = prices[5];
^
Price.java:56: cannot find symbol
symbol : variable prices
location: class Price
int high1 = prices[5];
^
Price.java:57: cannot find symbol
symbol : variable prices
location: class Price
for (int i=0; i < prices.length; i++)
^
Price.java:59: cannot find symbol
symbol : variable prices
location: class Price
if (prices[i] > high1){
^
Price.java:60: cannot find symbol
symbol : variable prices
location: class Price
high1 = prices[i];}
^
Price.java:62: cannot find symbol
symbol : variable prices
location: class Price
for (int i=1; i < prices.length; i++)
^
Price.java:64: cannot find symbol
symbol : variable prices
location: class Price
if (prices[1] . high2){
^
Price.java:65: cannot find symbol
symbol : variable prices
location: class Price
high2 = prices[1];}
^
Price.java:67: incompatible types
found : int
required: double[]
return high1;
^
Price.java:68: incompatible types
found : double
required: double[]
return high2;
^
28 errors

Is This A Good Question/Topic? 0
  • +

Replies To: Need some help with arrays

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need some help with arrays

Posted 24 May 2009 - 07:45 PM

OK one by one:

	public static double sumArray(double totalPrice[])
	{
		
		double sum;
		totalPrice = 0.0;
		

			  for(int i=0; i<= 4; i++)
				 sum = sum + totalPrice[i];
		return sum;
	}


This method should return the sum of the array TotalPrice... so you shouldn't try to change totallPrice and your 4 is bad hard coding it should look like:

	public static double sumArray(double totalPrice[])
	{		
		double sum = 0.0;
		
		 for(int i=0; i<= totalPrice.length; i++)
				 sum = sum + totalPrice[i];
		return sum;
	}



The average
   public static double[] averageArray(double totalPrice[])
	{
		double totalPrice;
		double avgPrice;
		totalPrice = 0.0;

			  for(int i=0; i<= 4; i++)
				 sum = sum + totalPrice[i];
		
		avgPrice = totalPrice/5;
		return avgPrice;
	}
	



repeats the previous error and claim it returns an array which is not the case it returns only a single value
also your sum method now works... why not using it ?
   public static double averageArray(double totalPrice[])
	{
		return sumArray(totalPriice) / totalPrice.length;
	}


And the last one

void displayOverAverage(double average, double[] total) {
   for(int i = 0; i < total.length; i++) {
	   if(total[i] > average)
		   System.out.println(total[i] + " is over " + average);
   }
}



Hope this helps
Helly coding

This post has been edited by pbl: 24 May 2009 - 07:55 PM

Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Need some help with arrays

Posted 24 May 2009 - 07:47 PM

You have to go through them slowly. Heres a start. Some of your errors say cannot find symbol totalPrice etc. This is because you declar totalPrice in the scope of a method, which means it wont be available in other places. If you need to use these other places, declare them in the class scope (where you declare your array).
You also have a couple of errors relating to whats being returned from a method. For instance, your double[] averageArray method. This method expects a double array to be returned. Your returning avgPrice which is just a double, therefore your returning the wrong type. If you just want a double returned, change method to
double averageArray
Go through them slowly and you can figure them out.
Was This Post Helpful? 0
  • +
  • -

#4 angelfish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-May 09

Re: Need some help with arrays

Posted 24 May 2009 - 08:05 PM

Ok thanks. This helps a bit. I thought I had to do all of the calculations in each one.
I am still getting that first error message though. Can someone help me figure out why it is saying that?

Price.java:16: sumArray(double[]) in Price cannot be applied to (double)
sumArray (prices[i]);
^
Price.java:18: averageArray(double[]) in Price cannot be applied to (double)
averageArray (prices[i]);
^
Price.java:20: highPrices(double,double[]) in Price cannot be applied to (double)
highPrices (prices[i]);
^
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Need some help with arrays

Posted 24 May 2009 - 08:57 PM

View Postangelfish, on 24 May, 2009 - 07:05 PM, said:

Ok thanks. This helps a bit. I thought I had to do all of the calculations in each one.
I am still getting that first error message though. Can someone help me figure out why it is saying that?

Price.java:16: sumArray(double[]) in Price cannot be applied to (double)
sumArray (prices[i]);
^
Price.java:18: averageArray(double[]) in Price cannot be applied to (double)
averageArray (prices[i]);
^
Price.java:20: highPrices(double,double[]) in Price cannot be applied to (double)
highPrices (prices[i]);
^

repost your corrected code
Was This Post Helpful? 0
  • +
  • -

#6 angelfish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-May 09

Re: Need some help with arrays

Posted 25 May 2009 - 05:07 PM

 import java.util.*;
public class Price
{
	public static void main(String[] args)
	{
		
		double[] prices = {3.14, 1.49, 0.99, 3.26, 4.11};	
		double totalPrice;
		double sum;
		double avgPrice;
		for(int i=0; i < prices.length; ++i)
			{
			System.out.println("Please enter five prices: ");
			Scanner sc = new Scanner(System.in);			

			sumArray (prices);
					System.out.print("Thank you! Total: " + totalPrice);
			averageArray (prices);
			System.out.print("Average: " + avgPrice);
			highPrices (avgPrice, prices);			
			
			}
	}

	public static double sumArray(double totalPrice[])
	{	

			  for(int i=0; i<= totalPrice.length; i++)
		 		sum = sum + totalPrice[i];
		return sum;
	}
	
	public static double averageArray(double totalPrice[])
	{
	
		return sumArray(sum,totalPrice) / totalPrice.length;
	}
	
	public static void highPrices(double avgPrice, double [] totalPrice)
	{
		
		for (int i=0; i < totalPrice.length; i++)
			{
			if (totalPrice[i] > avgPrice){
				System.out.println(totalPrice[i] + " is over " + avgPrice);}
			}
		
	}
}



Price.java:29: cannot find symbol
symbol : variable sum
location: class Price
sum = sum + totalPrice[i];
^
Price.java:29: cannot find symbol
symbol : variable sum
location: class Price
sum = sum + totalPrice[i];
^
Price.java:30: cannot find symbol
symbol : variable sum
location: class Price
return sum;
^
Price.java:36: cannot find symbol
symbol : variable sum
location: class Price
return sumArray(sum,totalPrice) / totalPrice.length;
^
4 errors

This post has been edited by angelfish: 25 May 2009 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

Re: Need some help with arrays

Posted 25 May 2009 - 05:33 PM

for the .class errors:

1) you use double[i] instead of prices[i]

2) your method take in an array of type double, yet you pass an element of a double array, thus you're pass a double. so just pass the array like this:
sumArray(prices);


Then for line 17:
you missed a parenthesis.
Was This Post Helpful? 1
  • +
  • -

#8 angelfish  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-May 09

Re: Need some help with arrays

Posted 25 May 2009 - 05:46 PM

View Postphil1000, on 25 May, 2009 - 04:33 PM, said:

for the .class errors:

1) you use double[i] instead of prices[i]

2) your method take in an array of type double, yet you pass an element of a double array, thus you're pass a double. so just pass the array like this:
sumArray(prices);


Then for line 17:
you missed a parenthesis.



Ok thanks. That makes sense. I had prices[i] at first and was getting errors then as well. (prices) seems to be working.
I think I have it almost figured out but it seems to be having a problem finding the sum variable in the class although i have it there. This is fustrating. Been working on this for 5 days. :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1