Results 1 to 13 of 13
Thread: return question for method
- 09-02-2010, 03:49 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
return question for method
Ok, I am a little confused here as to what I can do to return an int called num.
I have to return it to make the program work to the professors specifications, but it is confusing. here is the header:
I cant return this "int num" because all equations are in a double.Java Code:public static int solve(double [] eqn, double [] roots)
I keep getting the error "Cant change result from int to double/int[]"
How do you guys switch from a double to an int? I assume you use variables, right?
- 09-02-2010, 05:14 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Hi,
You can convert from double value to int value using the type cast. If you look at the Double class you can also find a method that return an int value from the Double instance. But you should be aware that converting from double to int value you will lost the fractional value of your original double value.Last edited by wsaryada; 09-02-2010 at 05:17 AM.
Website: Learn Java by Examples
- 09-02-2010, 05:45 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
I dont mean to be facetious, but is there another way?
My code uses two double arrays, but I need to return an int.
If i use cast, most likely the professor will not approve of it, since he wants me to "program it all out". I am essentially not able to use Java utilities like comparing arrays and such - I have to do the manual work only. Does that make any sense? thanks for your response though. I wish I could use it.
- 09-02-2010, 07:07 AM #4
I don't think your professor can disprove of it, honestly. If your numbers are all doubles, somewhere you're going to lose fractional data if you're required to return an int. Whether that's through .intValue() or through (int) casting, you need some way to return it as an int. I mean, in theory, you could put the double into a string and capture everything to the left of the decimal... then convert that to an int using parseInt, but that's just stupid to be honest.
PS: You can also use Math.floor and Math.ceil, but those still return doubles.
- 09-02-2010, 08:07 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
My guess is that the method is supposed to return the number of roots and the roots need to be placed in the array passed to the method. The number of roots is an integer of course.
kind regards,
Jos
- 09-02-2010, 09:36 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Yes, asking us how to turn the inputs into an output without telling us what the method is supposed to actually do is unlikely to result in a correct answer.
So, can you confirm Jos' suspicions about what this method is supposed to actually do?
- 09-02-2010, 01:18 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
yeah, Jos said it.
I apologize again for my lack for explanation. I was trying to ask for some direction without placing homework on the forum. I do not want anyone to think I am cheating vs. asking for help online. next time though, I will just post some of what it is asking for.
Should I use a variable to do what Jos said?
- 09-02-2010, 03:22 PM #8
A variable is much easier to use than hardcoded literals.Should I use a variable to do what Jos said
- 09-02-2010, 04:33 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 70
- Rep Power
- 0
I had some extra help and got it solved. Thanks for telling me about the cast - I did not know that. Java has some pretty cool packages.
- 09-02-2010, 04:43 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 09-02-2010, 04:50 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Post #2.
Which is slightly worrying since we now know casting has nothing to do with this.
- 09-02-2010, 04:53 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 09-02-2010, 04:55 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Not able to return the method value
By dmakshay2002 in forum Advanced JavaReplies: 11Last Post: 05-28-2010, 02:07 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
can't return a value from a method / jdbc
By tascoa in forum JDBCReplies: 3Last Post: 10-15-2009, 01:02 PM -
Return question in a method.
By MetalGear in forum New To JavaReplies: 1Last Post: 01-13-2008, 04:45 AM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks