This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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!
 
 
subject: sumRange
 
Similar Threads
Printing Arrays
How to calculate number of days between two Timestamp
Need Help Stuck :(
Array Problem
Please Help