|
Silkroad Online
|
Silkroad Forums
|
Affiliates
|



|
|
View unanswered posts | View active topics
|
Page 1 of 1
|
[ 15 posts ] |
|
Author |
Message |
Rainigul
|
Post subject: Basic Java Help Posted: Mon Nov 21, 2011 2:27 am |
|
Senior Member |
 |
 |
Joined: Mar 2007 Posts: 4490 Location:
|
Free cookies for anyone who can tell me the answer to this. Quote: 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).
|
|
Top |
|
 |
EvGa
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 2:34 am |
|
Addicted Member |
 |
 |
Joined: Apr 2008 Posts: 2612 Location: Texas
|
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.
_________________
|
|
Top |
|
 |
Azilius
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 2:57 am |
|
Senior Member |
 |
 |
Joined: Oct 2006 Posts: 4236 Location: CS:GO
|
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.
_________________
 Crumpets for Pres 
|
|
Top |
|
 |
Rainigul
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 3:04 am |
|
Senior Member |
 |
 |
Joined: Mar 2007 Posts: 4490 Location:
|
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.
|
|
Top |
|
 |
cpinney
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 3:19 am |
|
Ex-Staff |
 |
 |
Joined: Aug 2007 Posts: 5718 Location: Maine, USA
|
post what code you do have, would make it easier to diagnose the problem.
_________________
|
|
Top |
|
 |
EvGa
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 3:22 am |
|
Addicted Member |
 |
 |
Joined: Apr 2008 Posts: 2612 Location: Texas
|
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: Code: 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...
_________________
|
|
Top |
|
 |
NuclearSilo
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 10:32 am |
|
Forum God |
 |
 |
Joined: Aug 2006 Posts: 8834 Location: Age of Wushu
|
Too easy (for me). But post your code first, you'll learn from your mistake
_________________ Playing Age of Wushu, dota IMBA
|
|
Top |
|
 |
only_me
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 2:19 pm |
|
Active Member |
 |
 |
Joined: Mar 2008 Posts: 561 Location:
|
i dunno java but for C++ is for(i=a;i<=b;i++) sum+=i; lawlz
_________________ COOL SIG
|
|
Top |
|
 |
NuclearSilo
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 6:57 pm |
|
Forum God |
 |
 |
Joined: Aug 2006 Posts: 8834 Location: Age of Wushu
|
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
|
|
Top |
|
 |
only_me
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 7:56 pm |
|
Active Member |
 |
 |
Joined: Mar 2008 Posts: 561 Location:
|
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;}
_________________ COOL SIG
|
|
Top |
|
 |
EvGa
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 8:00 pm |
|
Addicted Member |
 |
 |
Joined: Apr 2008 Posts: 2612 Location: Texas
|
And now the OP has to do no work to figure it out on his own...
_________________
|
|
Top |
|
 |
Rainigul
|
Post subject: Re: Basic Java Help Posted: Mon Nov 21, 2011 11:46 pm |
|
Senior Member |
 |
 |
Joined: Mar 2007 Posts: 4490 Location:
|
Sorry, didn't update this. Figured it out a few hours after I posted. Code: 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); }
|
|
Top |
|
 |
EvGa
|
Post subject: Re: Basic Java Help Posted: Tue Nov 22, 2011 5:49 am |
|
Addicted Member |
 |
 |
Joined: Apr 2008 Posts: 2612 Location: Texas
|
That works except it doesn't return the value like the question asks, it simply prints it out. Here is the simplest way: Code: 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: 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).
_________________
|
|
Top |
|
 |
only_me
|
Post subject: Re: Basic Java Help Posted: Tue Nov 22, 2011 7:36 am |
|
Active Member |
 |
 |
Joined: Mar 2008 Posts: 561 Location:
|
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: 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: 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
|
|
Top |
|
 |
EvGa
|
Post subject: Re: Basic Java Help Posted: Tue Nov 22, 2011 7:42 am |
|
Addicted Member |
 |
 |
Joined: Apr 2008 Posts: 2612 Location: Texas
|
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.
_________________
|
|
Top |
|
 |
|
Page 1 of 1
|
[ 15 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 21 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|