Results 1 to 6 of 6

Thread: Help Please

  1. #1
    diamondeyes is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Help Please

    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by diamondeyes View Post
    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-

  4. #4
    diamondeyes is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by diamondeyes View Post
    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-

  6. #6
    diamondeyes is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Here is the finished code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •