posted 14 years ago
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 ]
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
posted 14 years ago
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 ]
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 ]
not so smart guy still curious to learn new stuff every now and then
posted 14 years ago
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 forloop.
If you've further questions, just ask.
If you've further questions, just ask.
macca Mason
Greenhorn
Posts: 13
posted 14 years ago
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
*/
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
*/
posted 14 years ago
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.
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
posted 14 years ago
Macca,
The main method must have the following signature:
It is required to return void (not double)
The main method must have the following signature:
It is required to return void (not double)
[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
macca Mason
Greenhorn
Posts: 13
posted 14 years ago
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 ]
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 ]
posted 14 years ago
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
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
posted 14 years ago
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 forloop 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 forloop 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?
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 forloop 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 forloop 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?
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
