Results 1 to 11 of 11

Thread: Question

  1. #1
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default Question

    Hey, i was sorta stuck on a problem i got in class. Here's what it is:

    Write a method called integerSum that generates the sum of the integers 1 through count. It then prints the value of sum out. the value of count is passes into the method.

    Any idea how i should start this one? Any suggestions will be appreciated.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    What have you done so far?
    My Hobby Project: LegacyClone

  3. #3
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    When you say you got 'stuck', how far did you get?

    What ideas have you got about how you might get the computer to perform this task?

    Can you describe (in plain words if need be, no need to worry about code at this stage) what logic you might use?

    If you can do that, you're a step closer to creating the method you need.

    No doubt someone could just give you a chunk of code that would do what you're after but frankly there's not much point.

    Trying to be helpful. Really.

  4. #4
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    so far, i've written:

    public int integerSum(int count)
    {
    int sum = 0;
    for(int i = 1; i < count; i++)
    }

    I don't know what to do after this. Like, i get the idea, but I'm stuck with the coding...

  5. #5
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    OK, that looks good.

    So this:
    Java Code:
    for(int i = 1; i < count; i++)
    is the start of a 'for' loop.

    Do you know how 'for' loops work?

    If you're not sure, you could check out the Sun Java tutorials.
    Or maybe you're using a book in class, that would show you how they work?
    I'm assuming you've seen 'for' loops somewhere, to know that that was something useful in this particular question.

    What could you do inside this particular 'for' loop that would help you get the result you want?

    If you're not sure, set yourself a simpler task, and then come back to the more complicated one.
    So, for example, can you write a 'for' loop which prints out the numbers 1 to 10, each number on a new line?
    Last edited by DiamondDog; 10-13-2009 at 10:11 PM.

  6. #6
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Yes, I have a book, but it doesn't explain things very well. I know how for loops work and that is the reason i used it in this one. Do i need to have count going up, as in, count++ and then have sum = i + count? As for your question, is the answer this:

    for(int i = 1; i <= 10; i++)
    System.out.println(i);

  7. #7
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    Yes, I have a book, but it doesn't explain things very well.
    Yeah, I've got a few like that, on my shelf. :)

    Yes, that code of yours would print out the numbers exactly as we want them, and you're nearly there as far as the original question goes.

    You started off by setting 'sum' equal to zero.

    So if we use words to describe what's happening:

    Java Code:
    public int integerSum(int count)
    {
    // set 'sum' equal to zero
    int sum = 0;
    
    // use a 'for' loop
    for(int i = 1; i < count; i++)
    {
      // add 'i' to 'sum'
       .... your code to do that goes here ....
    }
    
    // now print the value of 'sum'
     ... your code to do that goes here
    }
    Notice that in your 'for' loop, 'i' will go from 1,2,.... count-1 and then stop.

    In other words, 'i' will never equal 'count'.

    Is that what you want?

    Or do you need to change the 'for' loop slightly so that the final value of 'i' is 'count'?
    Last edited by DiamondDog; 10-13-2009 at 10:37 PM.

  8. #8
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Haha, we all have books like that :]

    So, from what you said, the code should look like this:

    public int integerSum(int count)
    {
    int sum = 0;
    for(int i = 1; i < count; i++)
    {
    sum += i;
    System.out.println(sum);
    }
    }

    I get the idea of the for loop as well. This method will have add numbers from 1 to count-1, never equaling count. I think that is exactly what i needed. Thanks a lot for the crystal clear explaining :]

  9. #9
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    OK, I just thought if count was equal to, say, 6,
    you'd want to work out the sum of 1+2+3+4+5+6

    As it stands, you'll actually work out 1+2+3+4+5 which may, of course,
    be exactly what you want.

    Notice that at the moment your print statement is inside
    the 'for' loop, so you'll print the value of 'sum' each time
    you go through the loop.

    If you only want to print the value of 'sum' once (at the end)
    you'd have to move the print statement so that it appears
    after the 'for' loop has finished.

    The other thing to point out is that you've declared the method
    in the following way
    Java Code:
    public int integerSum(int count)
    That 'int' before the name of the method implies that the method
    is going to return an 'int'.
    At the moment your method doesn't return anything at all.
    It just prints out the required sum.
    That's fine, but it means you should declare the method as type 'void'
    (meaning it doesn't return anything).

    ie.
    Java Code:
    public void integerSum(int count)
    Last edited by DiamondDog; 10-13-2009 at 11:12 PM.

  10. #10
    Java_Fanatic is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

    Default

    Oh yeah, you're right. I'll change that right now. I need the print outside the for loop, since I only needed printed once. Thanks again for the help. Do you mind if i add you as a friend, in case i have any other questions i need answered?

  11. #11
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    You're very welcome and yeah, feel free to add me to your list of friends.
    I'm still a noob when it comes to Java - there are guys round here who know this stuff way better than I do - but anytime I can help, I'm more than happy to.

    Good Luck

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. ur help on my question
    By jameela in forum New To Java
    Replies: 4
    Last Post: 10-13-2008, 12:38 PM
  3. question
    By zizou147 in forum New To Java
    Replies: 13
    Last Post: 07-04-2008, 07:05 PM
  4. Question
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 05-21-2008, 02:23 PM
  5. a question
    By slytheman in forum Java Servlet
    Replies: 0
    Last Post: 03-12-2008, 04:11 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
  •