Write and test the following method that implements the power function:
static double pow(double x, int n)
This method returns the value of x raised to the power n. For example pow(2.0, -3) would return
2-3 = 0.125
For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.
Here is a sample run:
0.125
0.25
0.5
1.0
2.0
4.0
8.0
16.0
32.0
64.0
Press any key to continue.....
So far,I have done the coding up to this far..I can't seem to get it right.
Hope u can spare the time to look through. Would appreciate it. Thanks
static double pow(double x, int n)
This method returns the value of x raised to the power n. For example pow(2.0, -3) would return
2-3 = 0.125
For each value of pow(x,n) that you print, also print the value of Math.pow(x,n) to check your results.
Here is a sample run:
0.125
0.25
0.5
1.0
2.0
4.0
8.0
16.0
32.0
64.0
Press any key to continue.....
So far,I have done the coding up to this far..I can't seem to get it right.
Code:
import javax.swing.JOptionPane; class PowerOf { public static void main(String[] args){ String strX; double x; String strN; int n; int counter=1; double value; strX = JOptionPane.showInputDialog(null, "Enter the x value: "); x = Double.parseDouble(strX); strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: "); n = Integer.parseInt(strN); for (counter=1; counter<=10; counter++)//to print the value { pow(x,n); System.out.println(pow(x,n)); counter++; } while (counter!=10)//for checking the answer { counter++; value=Math.pow(x,n); System.out.println(value); n++; } } public static double pow(double x, int n){ double value=1; for (int counter=1,counter<n, counter++) { value=1/value*2; } value=value*2; n++; return value; } }
Comment