
Help on Class promblem
Write a method sumRange which takes two integer parameters, and sums all the integers between them (inclusive) and returns the sum as an integer.
If the second number is smaller than the first, the method should return 0 and display an error message.
....
to issue an error message, print it to the console.... like this:
System.Out.Println("Error!!! The second value was less than the first in method sumRange!");
Here are some example calls to sumRange and what they would return:
sumRange(1,3) should return 6  1+2+3
sumRange(2,4) should return 9  2+3+4
sumRange(3,3) should return 3 ... just 3
sumRange(4,2) should return 0, and print the error message to the console.
Paste your sumRange method into the assignment window here.
all I have is:
Code:
public statis int sumRange(int start, int end) //this should be the parameters
{
int sum = 0
if (end < start)
system.out printIn ("ERROR: Invalid Range");
else
for (int num = start; num < = end; num++)
sum + = num;
return ;
}
I know this is wrong.

This is just to OP's code in code tags, without the typos
@OP: Please place code tags ([code][/code]) around whatever code you post. They preserve indentation and equal spacing, etc.
Code:
//static, not statis
public static int sumRange(int start, int end){ //this should be the parameters
int sum = 0
if (end < start)
system.out.println ("ERROR: Invalid Range");//println, not printIn (L not I)
else
for (int num = start; num < = end; num++)
sum + = num;
return;
}
You're not actually all that far off. The only thing you are missing is a return sum; statement at the end. You should replace the return; with return sum; and it should work.
Cheers,
SingingBoyo

thanks
thanks,
I did not randomly found java. I am in programming class and we are using the Netbeans. this class is killing me. thanks you for helping.

Gotta love the signature.
No problem :D
BTW, Netbeans should be catching your problems with the format of the return statements... should've told you that return; was invalid, and your typos in println and static should've been caught as well... I'm guessing that you did not use Netbeans to write that code... as a suggestion, use Netbeans for all your code writing.

I have to trace it. How would I do that?

i am actually handwriting it...i tend to learn it better that way then i put it through NetBeans.

I'm assuming that by trace you mean that you need to trace the progress of the computation. In that case, change your for loop to this:
Code:
public static int sumRange(int start, int end){ //this should be the parameters
int sum = 0
if (end < start)
system.out.println ("ERROR: Invalid Range");//println, not printIn (L not I)
else
for (int num = start; num < = end; num++){
//brackets because you need to print sum, which means the loop will have more
//than one statement
sum + = num;
//code to print the sum here (same idea as printing the error message)
}
return sum;
}
I'll leave you to figure out the printing code.

thanks. I did not have to run the code. All the professor was looking for was the method. I had it down and I was stressing. Thanks for your help!

We don't need no steenkin' loops; the old Greeks even knew that; here's how: suppose you want to add the numbers 1 ... 6; you can visualize it like this:
Code:
*
**
***
****
*****
******
... now add that same tirangle to the original one but upside down:
Code:
*######
**#####
***####
****###
*****##
******#
So twice the number we want to know (let the number be 'n') equals n*(n+1),
so the sum of the numbers 1 ... n equals n*(n+1)/2
That is easy enough to turn into a little method:
Code:
int sum(int n) {
return n*(n+1)/2;
If we want to know the sum of the numbers m ... n we actually want to know the sum of the numbers 1 ... n minus the sum of the numbers 1 ... m1. In Java that makes:
Code:
int sum(int m, int n) {
return sum(n)sum(m1);
}
Now that wasn't too difficult was it?
kind regards,
Jos