Page 1 of 1

Basic Java Help

Posted: Mon Nov 21, 2011 2:27 am
by Rainigul
Free cookies for anyone who can tell me the answer to this.

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

Re: Basic Java Help

Posted: Mon Nov 21, 2011 2:34 am
by EvGa
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.

Re: Basic Java Help

Posted: Mon Nov 21, 2011 2:57 am
by Azilius
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.

Re: Basic Java Help

Posted: Mon Nov 21, 2011 3:04 am
by Rainigul
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

Posted: Mon Nov 21, 2011 3:19 am
by cpinney
post what code you do have, would make it easier to diagnose the problem.

Re: Basic Java Help

Posted: Mon Nov 21, 2011 3:22 am
by EvGa
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: 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...

Re: Basic Java Help

Posted: Mon Nov 21, 2011 10:32 am
by NuclearSilo
Too easy (for me). But post your code first, you'll learn from your mistake

Re: Basic Java Help

Posted: Mon Nov 21, 2011 2:19 pm
by only_me
i dunno java but for C++ is for(i=a;i<=b;i++) sum+=i;
lawlz

Re: Basic Java Help

Posted: Mon Nov 21, 2011 6:57 pm
by NuclearSilo
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

Re: Basic Java Help

Posted: Mon Nov 21, 2011 7:56 pm
by only_me
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;}

Re: Basic Java Help

Posted: Mon Nov 21, 2011 8:00 pm
by EvGa
And now the OP has to do no work to figure it out on his own...

Re: Basic Java Help

Posted: Mon Nov 21, 2011 11:46 pm
by Rainigul
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

Posted: Tue Nov 22, 2011 5:49 am
by EvGa
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;
      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).

Re: Basic Java Help

Posted: Tue Nov 22, 2011 7:36 am
by only_me
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 :sohappy:

Re: Basic Java Help

Posted: Tue Nov 22, 2011 7:42 am
by EvGa
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.