Results 1 to 10 of 10
  1. #1
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Adding numbers in an array?

    Hello. I have this problem. Here is the code first

    class SalesAnalysis

    {
    private String [] months = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"};

    private int [] sales2005 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    private int [] sales2006 = {0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};

    public void getTotal (int[])
    {

    }

    Question: This method [getTotal (int[]) ]will total all the values in an array. A parameter to this method is a reference to an array of integers.
    The method will return the total of the values in the referenced array.

    I think the code may be something like this:

    for (int i = 0; i < sales2005.length; i++)

    System.out.print(sales2005[i])


    Any ideas? I am stumped. I have an exam this week.

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

    Default

    if you must total the ints in the array, you need an int variable declared before your loop, and then add the array item's values to this variable within the loop as you loop through your array. When the for loop is done, there's your total. Make sense?

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Somehting like this?

    the call to getTotal would be something like this:
    int 2005Total=0;
    2005Total = getTotal(sales2005);

    The pseudo code for the getTotal method would look something like:
    Java Code:
    public int getTotal (int[])
    {
    loop through the elements of the array, 
    total = total + int[i]
    end loop
    return total
    }
    Did this help?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    The only thing I'd add to CJSL's excellent post is to be sure to declare total before the loop.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default yeah...

    Yeah.... I thought about that, but it's pseudo code... I was hoping the OP would figure that out... don't want to give everything away :)
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    arrrrghhh. my brain is fried. i cant get it. =(

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default uh.... that's hard to believe...

    If you came up with the code in your first post, then you surely can figure out what Fubarable and myself suggested.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default Arrays and adding: (Some progress)

    public class TestSalesa

    {

    public static void main (String [] args)

    {

    int i = 0;
    int sum = 0;

    String [] months = {"", "Jan", "Feb"};


    int[] sales2005 = {0, 20, 30};
    int[] sales2006 = {0, 40, 50};




    getTotal(sales2005);


    }


    static void getTotal(int [] sales2005)

    {

    int sum = 0;

    for (int i = 0; i < sales2005.length; i++)
    sum = sum + sales2005[i];

    System.out.print(sum);
    }

    }

    I worked this out. but i cant write a driver to call the getTotal. arrgh it wont work.

  9. #9
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    i dont understand your int 2005Total = 0;

    The second line helped me with the code for the single file java programme. but the example on the exam paper is written like as if you have to write a driver to call the getTotal. trying to get the objects to cooperate is frustrating.

    the exam question asks you just to write the method in the SalesAnalysis file. but like i want to practice oop. i want to have a crack at writing the driver for practice. but i cant get it. god i will never make a programmer. lol.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default It works fine for me....

    Your "driver" is a method. You call a method. Now what is the problem? It works perfectly for me. I didn't change a thing. it prints 50 on the command line.
    Do you get an error?
    What did you expect it to do?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  2. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  3. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM
  4. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM
  5. Adding graphics to array
    By romina in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 01:45 AM

Posting Permissions

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