2 Replies - 3696 Views - Last Post: 26 April 2010 - 07:19 AM Rate Topic: -----

#1 Guest_Jdumont11*


Reputation:

Java Array Help

Posted 26 April 2010 - 07:06 AM

I need help with this program.

Project
Write a program that will prompt the user for a list of 5 prices.   Once the user has entered all values, your program should compute and display the following:
•        The sum of all the prices
•        The average of the prices
•        All prices that are higher than the calculated average
 
To better solve this problem, break your code out into the following methods:
•        sumArray – this method should receive an array and return the sum of all elements in the array.   NOTE: this method produces no output.
•        aveArray – this method should receive an array and return the average of all elements in the array.   NOTE: this method produces no output.
•        highPrices – this method should receive an array and an average.   It should then print out all elements in the array whose values are greater than the average.
 
When you are finished writing these methods, create a main method that will prompt the user for the 5 prices and then call the appropriate methods.
Be sure your program demonstrates good programming style (appropriate comments, identifier names, indenting, etc).


Here is my coding so far. I can not find the coding for finding the prices that are higher than the calculated average.

import javax.swing.*;


public class ArrayProgram
{
	
	public static void main(String[] args)
		{
			String[] items = {"Apples", "Bananas", "Nectarines", "Oranges", "Plums"};
			double s;
			double a;
			
			double[] price = new double[5];
			for (int i = 0; i < items.length; i++) 
		{
				price[i] = Double.parseDouble(JOptionPane.showInputDialog(("Enter Price for  " + items[i])));
			s = sumArray(price);
			a = aveArray(price);
			higherAve(price, a);

		JOptionPane.showMessageDialog(null, "The total cost of these items is: $" + s); 
		JOptionPane.showMessageDialog(null, "The average cost of these items is: $" + a);
		JOptionPane.showMessageDialog(null, "The prices above average are: ");
		}

		}
	public static double sumArray(double[] array)
		{
			double sum = 0.00;
			for(int s = 0; s < array.length; s++)
				sum += array[s];
			return sum;
		}
	//This function takes in an array of values and returns the average of all the values in the array
	public static double aveArray(double[] array)
	{
		//First get the sum of the values in the array
		double sum = sumArray(array);
		//Then divide the sum by the number of elements in the array to arrive at the average
		double a = sum / (double)array.length;
		return a;
	}
        
        //This function is used to find the prices higher than the average price
	public static void higherAve(double[] price, double a)
	{
		
	}
	
	
}


This post has been edited by Dogstopper: 26 April 2010 - 07:15 AM
Reason for edit:: Added code tags for the newbie


Is This A Good Question/Topic? 0

Replies To: Java Array Help

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Java Array Help

Posted 26 April 2010 - 07:10 AM

Please, :code:

Can you loop through the array (I see you've done so in previous methods)? Can you compare two doubles to see if one is greater than the other (hint- use an if statement)?
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Java Array Help

Posted 26 April 2010 - 07:19 AM

Just loop through and do a check to see if the current index's value is greater than the average. If it i greater, then print:
for (int i = 0; i < price.length; i++) {
    if (price[i] > a) {
        This is greater than average...so now print it.
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1