jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes sumRange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sumRange" Watch "sumRange" New topic


Franny N. Zooey

Joined: Dec 03, 2003
Posts: 3
Write a method called sumRange that accepts two integer parameters that represent a range. Issue an error message and return zero if the second parameter is less than the first. Otherwise, the method should return the sum of the integers in that range (inclusive).
public sumRange (int a, b)
if (b < a)
System.out.println ("Error. Second integer must be greater than the first.");
int count = a, sum = 0;
for (a; count == b ;count++ )
sum = sum + count;
return sum;

Will this code work?
Perhaps an even more practical question- how can I check this for myself?
I only have written the parts to just this method- what other parts do I have to write to see if this will compile and run? (I am very new newbie )
Thanks for any help.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
create a Test class, declare the method to be static,
run it inside main()
possible structure, would like

try compile & run it, you will see
Franny N. Zooey

Joined: Dec 03, 2003
Posts: 3
Thank you for your help!
I agree. Here's the link: http://aspose.com/file-tools
subject: sumRange
It's not a secret anymore!