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:

Java Code:
`public static int solve(double [] eqn, double [] roots)`
I cant return this "int num" because all equations are in a double.

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?

2. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14
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 06:17 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.

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.

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

6. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
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?

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?

8. Should I use a variable to do what Jos said
A variable is much easier to use than hardcoded literals.

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.

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.
What cast?

kind regards,

Jos

11. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
Post #2.
Which is slightly worrying since we now know casting has nothing to do with this.

12. Originally Posted by Tolls
Post #2.
Which is slightly worrying since we now know casting has nothing to do with this.

kind regards,

Jos

13. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27
Imagine how complex it would be if it wasn't a SOLVED 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
•