I attempted to create the following project. This is what i did in order to see
ID: 3744001 • Letter: I
Question
I attempted to create the following project. This is what i did in order to see if i provide enough information for assistance.
open netbeans
created new java application project 1
created new java class PowerMain
this is the code written:
public class PowerMain {
public static double power1(double x, int n) {
double pow = 1;
for (int i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
public static double power2(double x, int n) {
if (n == 0) {
return n;
}
if ((n & 1) == 1) {
return x * power2(x, n / 2) * power2(x, n / 2);
}
return power2(x, n / 2) * power2(x, n / 2);
public static void main(String[] args) { //error at this point not allowing programn to run
PowerMain obj1 = new PowerMain();
double x = 2.0;
for (int i = 0; i <= 10; i++) {
System.out.println(x + " ^ " + i + " = " + power1(x, i) + " computed iteratively");
System.out.println(x + " ^ " + i + " = " + power1(x, i) + " computed recursively");
}
}
}
When I attempted to run the program i received an error on public static void main(String[] args){ }
What have I done wrong? Thank you for you assistance.
Explanation / Answer
Hello,
first when you are using functions in same class , no need to create object of that class.
PowerMain obj1 = new PowerMain();
and your code is working . for recursion you also calles the powre1() function , please correct it.
and in power2() function the 1st if is return n . if it return n then output always be 0.0
correct code for power2() function is:
public static double power2(double x, int n) {
if (n == 0) {
return 1;
}
if ((n & 1) == 1) {
return x * power2(x, n / 2) * power2(x, n / 2);
}
return power2(x, n / 2) * power2(x, n / 2);
}
-----------------------------------------------------------------PowerMain.java----------------------------------------------
public class PowerMain {
public static double power1(double x, int n) {
double pow = 1;
for (int i = 0; i < n; i++) {
pow = pow * x;
}
return pow;
}
public static double power2(double x, int n) {
if (n == 0) {
return 1;
}
if ((n & 1) == 1) {
return x * power2(x, n / 2) * power2(x, n / 2);
}
return power2(x, n / 2) * power2(x, n / 2);
}
public static void main(String[] args) {
//PowerMain obj1 = new PowerMain();
double x = 2.0;
for (int i = 0; i <= 10; i++) {
System.out.println(x + " ^ " + i + " = " + power1(x, i) + " computed iteratively");
System.out.println(x + " ^ " + i + " = " + power2(x, i) + " computed recursively");
}
}
}
-------------------------------------------------------------Output------------------------------------------------------------
2.0 ^ 0 = 1.0 computed iteratively
2.0 ^ 0 = 1.0 computed recursively
2.0 ^ 1 = 2.0 computed iteratively
2.0 ^ 1 = 2.0 computed recursively
2.0 ^ 2 = 4.0 computed iteratively
2.0 ^ 2 = 4.0 computed recursively
2.0 ^ 3 = 8.0 computed iteratively
2.0 ^ 3 = 8.0 computed recursively
2.0 ^ 4 = 16.0 computed iteratively
2.0 ^ 4 = 16.0 computed recursively
2.0 ^ 5 = 32.0 computed iteratively
2.0 ^ 5 = 32.0 computed recursively
2.0 ^ 6 = 64.0 computed iteratively
2.0 ^ 6 = 64.0 computed recursively
2.0 ^ 7 = 128.0 computed iteratively
2.0 ^ 7 = 128.0 computed recursively
2.0 ^ 8 = 256.0 computed iteratively
2.0 ^ 8 = 256.0 computed recursively
2.0 ^ 9 = 512.0 computed iteratively
2.0 ^ 9 = 512.0 computed recursively
2.0 ^ 10 = 1024.0 computed iteratively
2.0 ^ 10 = 1024.0 computed recursively
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.