jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes power () and cube() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "power () and cube()" Watch "power () and cube()" New topic

power () and cube()

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43
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.

Alan Jump

Joined: May 25, 2005
Posts: 26
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

Joined: Jul 10, 2002
Posts: 214

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

Joined: May 26, 2005
Posts: 43
The assignment is to "write an int method cube() that returns a cube of its single int formal parameter n.
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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..

Stephanie Dears
Ranch Hand

Joined: May 26, 2005
Posts: 43

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.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11861

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

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.



Java API Documentation
The Java Tutorial
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
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).
I agree. Here's the link: http://aspose.com/file-tools
subject: power () and cube()
It's not a secret anymore!