On paper, write a method called sum with a while loop that adds up all numbers between two numbers a and b, inclusive, and returns the sum as its result. The values for a and b can be passed to the sum method as parameters. For instance: sum(1, 5) would return the value 15 (i.e., 1 + 2 + 3 + 4 + 5).
Basic Java Help
- Rainigul
- Senior Member
- Posts: 4490
- Joined: Thu Mar 29, 2007 5:43 pm
- Quick Reply: Yes
- Location: Pacific
Basic Java Help
Free cookies for anyone who can tell me the answer to this.
Re: Basic Java Help
Have you attempted to write the method on your own? Is this for a class?
What have you got so far?
Hint: Have your while loop compare a to b. You'll need to increment a.
What have you got so far?
Hint: Have your while loop compare a to b. You'll need to increment a.

Re: Basic Java Help
Doing my first Java lab tomorrow..maybe I'll post an answer after that. Google some sample code to help yourself out, this question shouldn't take long.
- Rainigul
- Senior Member
- Posts: 4490
- Joined: Thu Mar 29, 2007 5:43 pm
- Quick Reply: Yes
- Location: Pacific
Re: Basic Java Help
This is for a class, and nothing I've tried so far has worked. I'm either getting "void" type not allowed here, or other stupid errors. I'm about to throw this PC against the wall. Evga do you actually know what is the answer, would appreciate seeing it so I can figure out what I'm doing wrong.
Re: Basic Java Help
Yes, I do know the solution, but for you to learn it is best to post what you have then try from there.
To get rid of any problems, format your class/main as follows. This will compile and run fine, just fill in the method:
EDIT: Well, it will compile and run fine once you give the method a return statement...
To get rid of any problems, format your class/main as follows. This will compile and run fine, just fill in the method:
Code: Select all
public class problem {
public problem() {
}
************************************************************
public static int sum(int a, int b){
//while loop goes here
}
************************************************************
public static void main(String[] args) {
System.out.println("Result from sum(): " + sum(1,5));
}
}
EDIT: Well, it will compile and run fine once you give the method a return statement...

- NuclearSilo
- Forum God
- Posts: 8834
- Joined: Mon Aug 21, 2006 12:00 pm
- Quick Reply: Yes
- Location: Age of Wushu
Re: Basic Java Help
Too easy (for me). But post your code first, you'll learn from your mistake
Playing Age of Wushu, dota IMBA
- NuclearSilo
- Forum God
- Posts: 8834
- Joined: Mon Aug 21, 2006 12:00 pm
- Quick Reply: Yes
- Location: Age of Wushu
Re: Basic Java Help
only_me wrote:i dunno java but for C++ is for(i=a;i<=b;i++) sum+=i;
lawlz
do you ever read? it says using while loop
Playing Age of Wushu, dota IMBA
- only_me
- Active Member
- Posts: 561
- Joined: Sun Mar 02, 2008 6:38 pm
- Quick Reply: Yes
- Location: Off Topic
Re: Basic Java Help
int sum(int a,int b)
{int x,y,s=0;
if(a<b) {x=a;y=b;}
else {x=b;y=a;}
while(x<=y)
{s+=x;x++;}
return s;}
{int x,y,s=0;
if(a<b) {x=a;y=b;}
else {x=b;y=a;}
while(x<=y)
{s+=x;x++;}
return s;}
COOL SIG
- Rainigul
- Senior Member
- Posts: 4490
- Joined: Thu Mar 29, 2007 5:43 pm
- Quick Reply: Yes
- Location: Pacific
Re: Basic Java Help
Sorry, didn't update this. Figured it out a few hours after I posted.
Code: Select all
public void sum(int a, int b)
{
int first = a
int last = b
int total = 0
while (first<=last)
{
total = total + first;
first = first + 1
}
System.out.println(total);
}
Re: Basic Java Help
That works except it doesn't return the value like the question asks, it simply prints it out.
Here is the simplest way:
Then run it in your main and print the result.
In your method, where you have "void", that signifies the return type of the method, void meaning it returns nothing. The question asks to return the answer so you need a return type of int and inside the method you have to "return" the final answer. This allows the method to be called by your program and the method to pass a result (answer, an int in this case) back to the calling program (main method).
Here is the simplest way:
Code: Select all
public int sum(int a, int b){
int result = 0;
while(a <= b){
result += a;
a++;
}
return result;
}
Then run it in your main and print the result.
Code: Select all
public static void main(String[] args) {
System.out.println(sum(1,5));
}In your method, where you have "void", that signifies the return type of the method, void meaning it returns nothing. The question asks to return the answer so you need a return type of int and inside the method you have to "return" the final answer. This allows the method to be called by your program and the method to pass a result (answer, an int in this case) back to the calling program (main method).

- only_me
- Active Member
- Posts: 561
- Joined: Sun Mar 02, 2008 6:38 pm
- Quick Reply: Yes
- Location: Off Topic
Re: Basic Java Help
EvGa wrote:That works except it doesn't return the value like the question asks, it simply prints it out.
Here is the simplest way:Code: Select all
public int sum(int a, int b){
int result = 0;
[b]while(a <= b)[/b]{
result += a;
a++;
}
return result;
}
Then run it in your main and print the result.Code: Select all
public static void main(String[] args) {
System.out.println(sum(1,5));
}
In your method, where you have "void", that signifies the return type of the method, void meaning it returns nothing. The question asks to return the answer so you need a return type of int and inside the method you have to "return" the final answer. This allows the method to be called by your program and the method to pass a result (answer, an int in this case) back to the calling program (main method).
what do u do if u have a =10 and b = -3

COOL SIG
Re: Basic Java Help
only_me wrote:what do u do if u have a =10 and b = -3
You really think his assignment is concerned with checking input constraints? This question was beyond simple, I'm going out on a limb here, but I bet this is an intro class and they are no where near concerned with checking things like that.
If he feels that the class is at that level, then by all means, add the few lines to check a and b. I don't think it is.

