This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Raising a number to the power. Help!

 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Math.pow(double, double) under java.lang package is what you need.
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chi
How do I open the java.lang package?
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
*/
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Macca,
The main method must have the following signature:

It is required to return void (not double)
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Langdon Algar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic