This week's book giveaway is in the Android forum. We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line! See this thread for details.

Hi I am stuck. I need to make a program that allows me to raise a number to the power. The initial number is entered by the user as so is the power value.

Hi am new to programming so be descriptive in your replies. Please help me. I have been stuck for ages. Thx [ December 02, 2003: Message edited by: macca Mason ]

Math.pow(double, double) under java.lang package is what you need.

macca Mason
Greenhorn

Joined: Nov 26, 2003
Posts: 13

posted

0

Hi Chi How do I open the java.lang package?

chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348

posted

0

java.lang package is imported automatically, you could just use Math.pow() for calculation. If you need to use some package that is not automatically imported do somthing like import java.util.*; to import classes under java.util package. [ December 02, 2003: Message edited by: chi Lin ]

Also, macca, since you're new to programming and since your power value is an int, if loops aren't foreign to your understanding, you might want to implement this program without using the Math.pow() method. You can do it with a pretty simple for-loop. If you've further questions, just ask.

Still got problem with my powers program can you please help! Thanks

/* C:\Documents and Settings\Mahmoud Maguid\My Documents\JavaCode\Coursework\Powers.java:20: '.class' expected Math.pow(double,double) ^ C:\Documents and Settings\Mahmoud Maguid\My Documents\JavaCode\Coursework\Powers.java:22: ')' expected System.out.println("The result of powers is "+ results); ^ C:\Documents and Settings\Mahmoud Maguid\My Documents\JavaCode\Coursework\Powers.java:20: unexpected type required: value found : class Math.pow(double,double) ^ 3 errors Tool completed with exit code 1 */

The error messages are not very helpful at all, are they? The problem is on the line result = Math.pow(double, double); "double" is a type, not a variable name; you want to pass the name of some double variables: result = Math.pow(number, power); But you haven't initialized any appropriate variables yet, so you've got a ways to go. Before you start with loops and things, maybe you could get a simpler program working that just printed out, say 2 to the 2nd power and verified that you got "4". Then move on from there.

I still have a problem with the code. I tryed to make the code simpler but there is one error which I can not resolve. Can you please take look at my program.

I have canceled the math.pow statement but used the for loop instead Can you please show me some directions to go. I belive the main problem is storing the power number so it can loop again [ edited to improve code formatting for greater readability -ds ] [ December 08, 2003: Message edited by: Dirk Schreckmann ]

Macca, Hi there, I am fairly new to Java as well, a way to do your problem while using "int"s provided by the user and the "Math.pow" method which takes "double"s would be to convert the ints to doubles and then send them to the Math.pow method. As in the following: ----------------------------------------------------------------- public class Powers { public static void main(String[] args) { if (args.length <2) { System.out.println("enter two numbers on the command line"); } else { int number1 = Integer.parseInt(args[0]); int power1 = Integer.parseInt(args[1]); int answer2; double answer1; double number2 = (double) number1; double power2 = (double) power1; answer1 = Math.pow(number2, power2); answer2 = (int) answer1;

System.out.println(number1 + " to the " + power1 + " power is: " + answer2); } } } ---------------------------------------------- Hope this helps, Langdon

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted

0

Considering your power method...

Why are you checking to see if initialnum == initialnum ? A mathematician would likely know for sure, but I think that it's the identity theorem that always makes such an expression true - a number always equals itself. So, this will always evaluate to be true. Let me be blunt. Your for-loop is completely wrong. Also, your power method doesn't have enough information to perform such an operation properly. Remember, the power function requires two operands - one which I'll call the base number and the other which is the power to which the base number is to be raised. Think for a moment about the power function, and perhaps the pattern of a proper for-loop shall become clearer. What happens when we raise a number, x, to 2? Simple. Multiply x by itself one time - x * x. What happens when we raise a number, x, to 3? Again, simple. Multiply x by itself two times - x * x * x. What happens when we raise a number, x, to 4? Multiply x by itself three times - x * x * x * x. And What happens when we raise a number, x, to the number, n? Well, multiply x by itself n - 1 times is likely the pattern that has revealed itself. So, is the solution becoming clearer?