Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2009, 08:32 PM
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
jaicea is on a distinguished road
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:
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 08:50 PM. Reason: code tags were not added
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-23-2009, 08:42 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
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.
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
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-23-2009, 08:44 PM
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
jaicea is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-23-2009, 08:47 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
Default
Gotta love the signature.

No problem

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.
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-23-2009, 08:52 PM
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
jaicea is on a distinguished road
Default
I have to trace it. How would I do that?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-23-2009, 08:53 PM
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
jaicea is on a distinguished road
Default
i am actually handwriting it...i tend to learn it better that way then i put it through NetBeans.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-23-2009, 08:56 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
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:

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.
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-23-2009, 09:11 PM
Member
 
Join Date: Nov 2009
Posts: 8
Rep Power: 0
jaicea is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-23-2009, 09:43 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 933
Rep Power: 2
JosAH is on a distinguished road
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:

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

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:

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:

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

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
java ide, programming homework

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to pass information from child class to parent class pellebye New To Java 7 05-06-2009 01:42 PM
problem in accessing array values of one class in to jframe class cenafu AWT / Swing 8 03-21-2009 10:34 AM
Calling a method on original class from created class kpedersen Advanced Java 4 08-20-2008 01:25 AM
Able to find class file in WEB-INF/classes but not after add sub folders in class dir vitalstrike82 Web Frameworks 0 05-13-2008 07:16 AM
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 08:04 AM


All times are GMT +2. The time now is 07:04 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org