• 12-14-2010, 10:32 PM
diamondeyes
THIS IS A HOME WORK ASSIGNMENT! I am not trying to get anyone to do this for me. I just simply do not understand how to do it. I can't ask a certain question because I am really overwhelmed and confused. If anyone can offer some insight, even if it is to get me started, it would be wonderful. Here is the assignment:
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).

what does it mean when it says the method should receive an array?
• 12-14-2010, 11:25 PM
Fubarable
You may need to tell us what specifically confuses you.
• 12-15-2010, 12:44 AM
gcalvin
Quote:

Originally Posted by diamondeyes
what does it mean when it says the method should receive an array?

That means that your method should accept an argument of type int[] (or maybe double[] -- lots of instructors tell their students to use doubles for prices, even though it's really a bad choice for the real world). Both sumArray() and aveArray() should return the same type that comprises the array, either int or double.

Show us your understanding of just that much by posting a couple of method declarations.

-Gary-
• 12-15-2010, 02:23 AM
diamondeyes
I actually got it figured out. I am not sure how to use the code tags, so let me know how to do that and I will show the code.
• 12-15-2010, 02:31 AM
gcalvin
Quote:

Originally Posted by diamondeyes
I actually got it figured out. I am not sure how to use the code tags, so let me know how to do that and I will show the code.

Just use the # icon in the editor.

-Gary-
• 12-15-2010, 02:49 AM
diamondeyes
Here is the finished code:
Quote:

import javax.swing.*;

public class fivePrices
{
public static void main(String[] args)
{//variable declaration section
String[] items = {"Apples", "Bananas", "Nectarines", "Oranges", "Plums"};//this line fills the array items with the 5 items

double[] price = new double[5];//declares a new price array that contains 5 doubles
for (int i = 0; i < items.length; i++) //initialize counter to fill array
{
price[i] = Double.parseDouble(JOptionPane.showInputDialog(("E nter Price for " + items[i])));//gets user input for 5 prices
}

double s = sumArray(price);
double a = aveArray(price);

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: ");
higherAve(price, a);
}

//two methods to figure sum and average
public static double sumArray(double[] array)
{//method for sum
double sum = 0.00;
for(int s = 0; s < array.length; s++)
sum += array[s];
return sum;
}
public static double aveArray(double[] array)
{
double average =0.00;
double sum = sumArray(array);

return average = (sum / array.length);
}

public static void higherAve(double[] price, double a)
{//method to determine prices that are above the average price as wel as returns output
String result = "";
for (int i = 0; i < price.length; i++)
{
if(price[i] > a)
{
result += "\$"+price[i]+"\n";
}
}
JOptionPane.showMessageDialog(null, result);
}
}//end class