# Question

• 10-13-2009, 09:37 PM
Java_Fanatic
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.
• 10-13-2009, 09:54 PM
mrmatt1111
What have you done so far?
• 10-13-2009, 09:57 PM
DiamondDog
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.

• 10-13-2009, 09:58 PM
Java_Fanatic
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...
• 10-13-2009, 10:04 PM
DiamondDog
OK, that looks good.

So this:
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?
• 10-13-2009, 10:24 PM
Java_Fanatic
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);
• 10-13-2009, 10:34 PM
DiamondDog
Quote:

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:

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'?
• 10-13-2009, 10:47 PM
Java_Fanatic
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 :]
• 10-13-2009, 11:07 PM
DiamondDog
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
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.
Code:

`public void integerSum(int count)`
• 10-13-2009, 11:10 PM
Java_Fanatic
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?
• 10-13-2009, 11:18 PM
DiamondDog
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