469,578 Members | 1,705 Online

# Help with coding using pow(double x,int n)

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

Expand|Select|Wrap|Line Numbers
1.  import javax.swing.JOptionPane;
2.
3. class PowerOf {
4.     public static void main(String[] args){
5.
6.         String strX;
7.         double x;
8.         String strN;
9.         int n;
10.         int counter=1;
11.         double value;
12.
13.         strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
14.         x = Double.parseDouble(strX);
15.
16.         strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
17.         n = Integer.parseInt(strN);
18.
19.         for (counter=1; counter<=10; counter++)//to print the value
20.         {
21.             pow(x,n);
22.             System.out.println(pow(x,n));
23.             counter++;
24.         }
25.
26.         while (counter!=10)//for checking the answer
27.         {
28.             counter++;
29.
30.
31.             value=Math.pow(x,n);
32.             System.out.println(value);
33.             n++;
34.         }
35.     }
36.
37.     public static double pow(double x, int n){
38.
39.         double value=1;
40.
41.         for (int counter=1,counter<n, counter++)
42.         {
43.             value=1/value*2;
44.
45.         }
46.
47.             value=value*2;
48.
49.         n++;
50.         return value;
51.     }
52.
53. }
54.
55.
Hope u can spare the time to look through. Would appreciate it. Thanks
Mar 10 '07 #1
1 4644
r035198x
13,262 8TB
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.

Expand|Select|Wrap|Line Numbers
1.  import javax.swing.JOptionPane;
2.
3. class PowerOf {
4.     public static void main(String[] args){
5.
6.         String strX;
7.         double x;
8.         String strN;
9.         int n;
10.         int counter=1;
11.         double value;
12.
13.         strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
14.         x = Double.parseDouble(strX);
15.
16.         strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
17.         n = Integer.parseInt(strN);
18.
19.         for (counter=1; counter<=10; counter++)//to print the value
20.         {
21.             pow(x,n);
22.             System.out.println(pow(x,n));
23.             counter++;
24.         }
25.
26.         while (counter!=10)//for checking the answer
27.         {
28.             counter++;
29.
30.
31.             value=Math.pow(x,n);
32.             System.out.println(value);
33.             n++;
34.         }
35.     }
36.
37.     public static double pow(double x, int n){
38.
39.         double value=1;
40.
41.         for (int counter=1,counter<n, counter++)
42.         {
43.             value=1/value*2;
44.
45.         }
46.
47.             value=value*2;
48.
49.         n++;
50.         return value;
51.     }
52.
53. }
54.
55.
Hope u can spare the time to look through. Would appreciate it. Thanks
When postind code please use code tags.

You need to handle all the possible scenarios using if-else
I have modified your program a bit. You should be able to finish it up now

Expand|Select|Wrap|Line Numbers
1.  import javax.swing.JOptionPane;
2. class PowerOf {
3.  public static void main(String[] args){
4.   String strX;
5.   double x;
6.   String strN;
7.   int n;
8.   int counter=1;
9.   double value;
10.   strX = JOptionPane.showInputDialog(null, "Enter the x value: ");
11.   x = Double.parseDouble(strX);
12.   strN = JOptionPane.showInputDialog(null, "Enter the power(n) value: ");
13.   n = Integer.parseInt(strN);
14.   double check = Math.pow(x, n);
15.   value = PowerOf.pow(x, n);
16.   System.out.print("Correct value is "+check+" : My value is "+value);
17.  }
18.  public static double pow(double x, int n){
19.   double value = 0.0;
20.   if(n < 0) {
21.    //write logic here
22.   }
23.   else if(n == 0) {
24.    //write logic here
25.   }
26.   else {
27.    for (int i = 1; i < n; i++) {
28.    //write logic here
29.    }
30.   }
31.   return value;
32.  }
33. }
34.
35.
Mar 10 '07 #2