Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sumRange

 
Franny N. Zooey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic