Asked by anonymous
without running on java, what is the final answer, please show your work.
public class sample {
public static void main(String[] args){
Sample x = new Sample();
int i = 10;
int b = x.g(i);
System.out.println(b + i);
}
public int f(int d){
int n = 0;
while(n*n < d){
n++;
}
return n-1;
}
public int g(int a){
int b = 0;
int c;
for(int n = 0; n < a; n++){
c = f(n);
b = b + c;
}
return b;
}
}
public class sample {
public static void main(String[] args){
Sample x = new Sample();
int i = 10;
int b = x.g(i);
System.out.println(b + i);
}
public int f(int d){
int n = 0;
while(n*n < d){
n++;
}
return n-1;
}
public int g(int a){
int b = 0;
int c;
for(int n = 0; n < a; n++){
c = f(n);
b = b + c;
}
return b;
}
}
Answers
Answered by
.
Recording all values of the variables in each method as we go:
-main:
i = 10
b = x.g(10) = 12 (returned from g)
---
after method g
i = 10
b = 12*
-f:
d = 0(passed from g's c variable)
n = 0
-while loop-
1st pass:
false, as 0*0 is not <0
n = n - 1 = 0 - 1 = -1
.
.
.
Keep comparing until n^2 is less than d passed in
-g:
a = 10 (passed from main's b variable)
b = 0;
c = NULL;
-for loop-
...n = 0
...c = f(0) = -1 (returned from f's return statement, 1st pass)
...b = b + c = 0 + -1 = -1
(And now after the first pass our variables are:
a = 10
b = -1
c = -1)
This continues until n=9 for n !>= 10
...b = 12
return b; // b is 12, and now we return to main
----------------------------------------------------------------------------------------
b + i = 12 + 10 = 22**
We are really only needing to find what b is, since i does not change in main. It's a lot of back and forth between method g and method f for b but we got it!
This seems like a lot but it's mostly pattern recognition bc of all the loops. It really helps to write down variables and update their values as you go thru code, just a tip.
Also, if you can, paste this into java and add print statements like: "b before adding" and "b after adding", etc. to see how, where, and why these variables change.
-main:
i = 10
b = x.g(10) = 12 (returned from g)
---
after method g
i = 10
b = 12*
-f:
d = 0(passed from g's c variable)
n = 0
-while loop-
1st pass:
false, as 0*0 is not <0
n = n - 1 = 0 - 1 = -1
.
.
.
Keep comparing until n^2 is less than d passed in
-g:
a = 10 (passed from main's b variable)
b = 0;
c = NULL;
-for loop-
...n = 0
...c = f(0) = -1 (returned from f's return statement, 1st pass)
...b = b + c = 0 + -1 = -1
(And now after the first pass our variables are:
a = 10
b = -1
c = -1)
This continues until n=9 for n !>= 10
...b = 12
return b; // b is 12, and now we return to main
----------------------------------------------------------------------------------------
b + i = 12 + 10 = 22**
We are really only needing to find what b is, since i does not change in main. It's a lot of back and forth between method g and method f for b but we got it!
This seems like a lot but it's mostly pattern recognition bc of all the loops. It really helps to write down variables and update their values as you go thru code, just a tip.
Also, if you can, paste this into java and add print statements like: "b before adding" and "b after adding", etc. to see how, where, and why these variables change.
Answered by
anonymous
thanks @.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.