Results 1 to 9 of 9
Thread: Help on Class promblem
- 11-23-2009, 07:32 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
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:
I know this is wrong.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 ; }Last edited by jaicea; 11-23-2009 at 07:50 PM. Reason: code tags were not added
- 11-23-2009, 07:42 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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.
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.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; }
Cheers,
SingingBoyoIf 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!
- 11-23-2009, 07:44 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
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.
- 11-23-2009, 07:47 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-23-2009, 07:52 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
I have to trace it. How would I do that?
- 11-23-2009, 07:53 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
i am actually handwriting it...i tend to learn it better that way then i put it through NetBeans.
- 11-23-2009, 07:56 PM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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:
I'll leave you to figure out the printing code.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; }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!
- 11-23-2009, 08:11 PM #8
Member
- Join Date
- Nov 2009
- Posts
- 8
- Rep Power
- 0
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!
- 11-23-2009, 08:43 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
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:
... 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),Java Code:*###### **##### ***#### ****### *****## ******#
so the sum of the numbers 1 ... n equals n*(n+1)/2
That is easy enough to turn into a little method:
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 n) { return n*(n+1)/2;
Now that wasn't too difficult was it?Java Code:int sum(int m, int n) { return sum(n)-sum(m-1); }
kind regards,
Jos
Similar Threads
-
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks