• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

power () and cube()

 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the concept of power (). Is cube() a method from java.lang or java.math? And how is it used? I can't find reference to it anywhere.

Stephanie
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't come across cube() anywhere in the API docs...the java.lang.Math package provides the pow() method, which is specified:

(Just to clarify...this came from the JDK { 1.5 | 5 } API.)

I could see where someone might have written a cube() method based on pow(), but I'm coming up empty trying to find a cube() method in the API. Anyone else have better luck (or more practice at searching the docs)?

(Edited to cite proper API)
[ May 26, 2005: Message edited by: Alan Jump ]
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Math does have a new to 1.5 cube root method.



You'll have to write your own cube() method if you don't like pow()
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment is to "write an int method cube() that returns a cube of its single int formal parameter n.
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think you would need to write something like the following.


hope that helps... int cube (int n) \\int n is the parameter being passed to the method...
return (n*n*n); \\ is cubing the int n number sent to the method and returning the value..or cube of the number..

Pete
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete,

Thanks. That's what I thought, but it doesn't reference it in the text. I found it someone on the Internet, but can't find it again.

Steph
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could also do something like call the power() method inside your cube method... rather than write a bunch of new logic/code, you can RE-USE something that already exists...

Now, granted in this case, it's pretty trivial to write your own method to do the cubing. But i would still argue it's better to use tested, tried and true methods than to write your own whenever possible...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephanie Dears:
The assignment is to "write an int method cube() that returns a cube of its single int formal parameter n.


The key here is that you need to write it YOURSELF. AFAIK, there is no cube() method in any of the standard API classes. When you are in a situation like this where you need to write your own method, you should first understand what the method should do. So in your own words what does this cube() method do? (I think others have hinted at it, but I'm trying to get you to think for yourself so it's a little easier for the next assignment.)

Of course, you may have already finished this assignment and handed it in. If so, I apologize for babbling on about it.

Regards,

Layne
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on how advanced you are in your class, you might be expected to realise that cube is a special case of power and therefore write two methods: a power method and a cube method that just calls power. But as Layne said, the key is to write the code yourself (both methods).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic