Results 1 to 9 of 9
  1. #1
    jaicea is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Exclamation Help on Class promblem

    Write a method sumRange which takes two integer parameters, and sums all the integers between them (inclusive) and returns the sum as an integer.

    If the second number is smaller than the first, the method should return 0 and display an error message.

    ....


    to issue an error message, print it to the console.... like this:

    System.Out.Println("Error!!! The second value was less than the first in method sumRange!");

    Here are some example calls to sumRange and what they would return:

    sumRange(1,3) should return 6 - 1+2+3
    sumRange(2,4) should return 9 - 2+3+4
    sumRange(3,3) should return 3 ... just 3
    sumRange(4,2) should return 0, and print the error message to the console.

    Paste your sumRange method into the assignment window here.



    all I have is:
    Java Code:
    public statis int sumRange(int start, int end) //this should be the parameters
    {
        int sum = 0
        if (end < start)
           system.out printIn ("ERROR: Invalid Range");
        else
           for (int num = start; num < = end; num++)
    sum + = num;
    return ;
    }
    I know this is wrong.
    Last edited by jaicea; 11-23-2009 at 07:50 PM. Reason: code tags were not added

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    This is just to OP's code in code tags, without the typos
    @OP: Please place code tags ([code][/code]) around whatever code you post. They preserve indentation and equal spacing, etc.
    Java Code:
    //static, not statis
    public static int sumRange(int start, int end){ //this should be the parameters
         int sum = 0
         if (end < start)
              system.out.println ("ERROR: Invalid Range");//println, not printIn (L not I)
         else
              for (int num = start; num < = end; num++)
                   sum + = num;
         return;
    }
    You're not actually all that far off. The only thing you are missing is a return sum; statement at the end. You should replace the return; with return sum; and it should work.

    Cheers,
    SingingBoyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    jaicea is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default thanks

    thanks,
    I did not randomly found java. I am in programming class and we are using the Netbeans. this class is killing me. thanks you for helping.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Gotta love the signature.

    No problem :D

    BTW, Netbeans should be catching your problems with the format of the return statements... should've told you that return; was invalid, and your typos in println and static should've been caught as well... I'm guessing that you did not use Netbeans to write that code... as a suggestion, use Netbeans for all your code writing.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    jaicea is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    I have to trace it. How would I do that?

  6. #6
    jaicea is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    i am actually handwriting it...i tend to learn it better that way then i put it through NetBeans.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'm assuming that by trace you mean that you need to trace the progress of the computation. In that case, change your for loop to this:

    Java Code:
    public static int sumRange(int start, int end){ //this should be the parameters
         int sum = 0
         if (end < start)
              system.out.println ("ERROR: Invalid Range");//println, not printIn (L not I)
         else
              for (int num = start; num < = end; num++){
              //brackets because you need to print sum, which means the loop will have more      
              //than one statement
                   sum + = num;
                   //code to print the sum here (same idea as printing the error message)
              }
         return sum;
    }
    I'll leave you to figure out the printing code.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    jaicea is offline Member
    Join Date
    Nov 2009
    Posts
    8
    Rep Power
    0

    Default

    thanks. I did not have to run the code. All the professor was looking for was the method. I had it down and I was stressing. Thanks for your help!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    We don't need no steenkin' loops; the old Greeks even knew that; here's how: suppose you want to add the numbers 1 ... 6; you can visualize it like this:

    Java Code:
    *
    **
    ***
    ****
    *****
    ******
    ... now add that same tirangle to the original one but upside down:

    Java Code:
    *######
    **#####
    ***####
    ****###
    *****##
    ******#
    So twice the number we want to know (let the number be 'n') equals n*(n+1),
    so the sum of the numbers 1 ... n equals n*(n+1)/2

    That is easy enough to turn into a little method:

    Java Code:
    int sum(int n) {
       return n*(n+1)/2;
    If we want to know the sum of the numbers m ... n we actually want to know the sum of the numbers 1 ... n minus the sum of the numbers 1 ... m-1. In Java that makes:

    Java Code:
    int sum(int m, int n) {
       return sum(n)-sum(m-1);
    }
    Now that wasn't too difficult was it?

    kind regards,

    Jos

Similar Threads

  1. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  2. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  4. Replies: 0
    Last Post: 05-13-2008, 06:16 AM
  5. Replies: 0
    Last Post: 04-02-2008, 07:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •