aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "sumRange" Watch "sumRange" New topic
Author

sumRange

Franny N. Zooey
Greenhorn

Joined: Dec 03, 2003
Posts: 3
Hello.
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.");
else
{
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.
FNZ
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
Greenhorn

Joined: Dec 03, 2003
Posts: 3
Thank you for your help!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sumRange