Basic Java Help

Anything else. Post a funny site or tell us about yourself. Discuss current events or whatever else you want. Post off topic threads here.
Post Reply
User avatar
Rainigul
Senior Member
Posts: 4490
Joined: Thu Mar 29, 2007 5:43 pm
Quick Reply: Yes
Location: Pacific

Basic Java Help

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

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Basic Java Help

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

User avatar
Azilius
Senior Member
Posts: 4236
Joined: Tue Oct 31, 2006 9:39 pm
Location: CS:GO

Re: Basic Java Help

Post 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.
ImageCrumpets for PresImage

User avatar
Rainigul
Senior Member
Posts: 4490
Joined: Thu Mar 29, 2007 5:43 pm
Quick Reply: Yes
Location: Pacific

Re: Basic Java Help

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

User avatar
cpinney
Ex-Staff
Posts: 5718
Joined: Sun Aug 26, 2007 8:34 am
Quick Reply: Yes
Location: Maine, USA
Contact:

Re: Basic Java Help

Post by cpinney »

post what code you do have, would make it easier to diagnose the problem.
Image

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Basic Java Help

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

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: Basic Java Help

Post by NuclearSilo »

Too easy (for me). But post your code first, you'll learn from your mistake
Playing Age of Wushu, dota IMBA

User avatar
only_me
Active Member
Posts: 561
Joined: Sun Mar 02, 2008 6:38 pm
Quick Reply: Yes
Location: Off Topic

Re: Basic Java Help

Post by only_me »

i dunno java but for C++ is for(i=a;i<=b;i++) sum+=i;
lawlz
COOL SIG

User avatar
NuclearSilo
Forum God
Posts: 8834
Joined: Mon Aug 21, 2006 12:00 pm
Quick Reply: Yes
Location: Age of Wushu

Re: Basic Java Help

Post 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
Playing Age of Wushu, dota IMBA

User avatar
only_me
Active Member
Posts: 561
Joined: Sun Mar 02, 2008 6:38 pm
Quick Reply: Yes
Location: Off Topic

Re: Basic Java Help

Post 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;}
COOL SIG

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Basic Java Help

Post by EvGa »

And now the OP has to do no work to figure it out on his own...
Image

User avatar
Rainigul
Senior Member
Posts: 4490
Joined: Thu Mar 29, 2007 5:43 pm
Quick Reply: Yes
Location: Pacific

Re: Basic Java Help

Post 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);
}

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Basic Java Help

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

User avatar
only_me
Active Member
Posts: 561
Joined: Sun Mar 02, 2008 6:38 pm
Quick Reply: Yes
Location: Off Topic

Re: Basic Java Help

Post 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:
COOL SIG

User avatar
EvGa
Addicted Member
Posts: 2612
Joined: Wed Apr 23, 2008 4:33 am
Quick Reply: Yes
Location: Texas

Re: Basic Java Help

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

Post Reply

Return to “Off Topic Lounge”