Silkroad Online Forums

A community forum for the free online game Silkroad Online. Discuss Silkroad Online, read up on guides, and build your character and skills.

Faq Search Members Chat  Register Profile Login

All times are UTC




Post new topic Reply to topic  [ 15 posts ] 
Author Message
 Post subject: Basic Java Help
PostPosted: Mon Nov 21, 2011 2:27 am 
Senior Member
User avatar
Offline

Joined: Mar 2007
Posts: 4490
Location:
Pacific
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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 2:34 am 
Addicted Member
User avatar
Offline

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.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 2:57 am 
Senior Member
User avatar
Offline

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.

_________________
ImageCrumpets for PresImage


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 3:04 am 
Senior Member
User avatar
Offline

Joined: Mar 2007
Posts: 4490
Location:
Pacific
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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 3:19 am 
Ex-Staff
User avatar
Offline

Joined: Aug 2007
Posts: 5718
Location: Maine, USA
post what code you do have, would make it easier to diagnose the problem.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 3:22 am 
Addicted Member
User avatar
Offline

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...

_________________
Image


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 10:32 am 
Forum God
User avatar
Offline

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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 2:19 pm 
Active Member
User avatar
Offline

Joined: Mar 2008
Posts: 561
Location:
Off Topic
i dunno java but for C++ is for(i=a;i<=b;i++) sum+=i;
lawlz

_________________
COOL SIG


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 6:57 pm 
Forum God
User avatar
Offline

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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 7:56 pm 
Active Member
User avatar
Offline

Joined: Mar 2008
Posts: 561
Location:
Off Topic
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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 8:00 pm 
Addicted Member
User avatar
Offline

Joined: Apr 2008
Posts: 2612
Location: Texas
And now the OP has to do no work to figure it out on his own...

_________________
Image


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Mon Nov 21, 2011 11:46 pm 
Senior Member
User avatar
Offline

Joined: Mar 2007
Posts: 4490
Location:
Pacific
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
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Tue Nov 22, 2011 5:49 am 
Addicted Member
User avatar
Offline

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).

_________________
Image


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Tue Nov 22, 2011 7:36 am 
Active Member
User avatar
Offline

Joined: Mar 2008
Posts: 561
Location:
Off Topic
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 :sohappy:

_________________
COOL SIG


Top
 Profile  
 
 Post subject: Re: Basic Java Help
PostPosted: Tue Nov 22, 2011 7:42 am 
Addicted Member
User avatar
Offline

Joined: Apr 2008
Posts: 2612
Location: Texas
only_me wrote:
what do u do if u have a =10 and b = -3 :sohappy:


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.

_________________
Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 43 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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group