Question 4. Given the following Java program public class Example { static int n = 3; public static void main(String[] args) { int k; k = n; Controller control = new Controller(k); control.compute(); } } class Controller { int n; Controller(int n) {this.n = n;} void compute() { int i, m; int sum = 0; String message; Functor func; for(i = n; i > 0; i--) { func = new Functor(); m = func.calculate(i); sum = sum + m; } message = “The sum of factorials from 1 to “ + n + “ is “ + sum; System.out.println(message); } } class Functor { int calculate(int n) { int temp; temp = n – 1; if (n > 0) { return n * calculate(temp); } else {return 1;} } } *List all static variables, stack-dynamic variables, explicit heap-dynamic variables, and implicit heap-dynamic variables.
Question 4. Given the following Java
public class Example
{
static int n = 3;
public static void main(String[] args) {
int k;
k = n;
Controller control = new Controller(k);
control.compute();
}
}
class Controller
{
int n;
Controller(int n) {this.n = n;}
void compute() {
int i, m;
int sum = 0;
String message;
Functor func;
for(i = n; i > 0; i--) {
func = new Functor();
m = func.calculate(i);
sum = sum + m;
}
message = “The sum of factorials from 1 to “ + n + “ is “ + sum;
System.out.println(message);
}
}
class Functor
{
int calculate(int n) {
int temp;
temp = n – 1;
if (n > 0) { return n * calculate(temp); }
else {return 1;}
}
}
*List all static variables, stack-dynamic variables, explicit heap-dynamic variables, and
implicit heap-dynamic variables.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps