What is the output of the following program? Show your reasoning by including ta
ID: 3629809 • Letter: W
Question
What is the output of the following program? Show your reasoning by including tables that illustrate how the values of the variables change over time, following the approach shown in lecture.
public class TracingMethodCalls {
public static int compute(int x, int y) {
x += 3;
y = 2*y - x;
System.out.println(x + " " + y);
return x;
}
public static void main(String[] args) {
int x = 1;
int y = 3;
x = compute(x, y);
System.out.println(x + " " + y);
compute(y, y);
System.out.println(x + " " + y);
y = 3 + 4 * compute(y, x);
System.out.println(x + " " + y);
}
}
Explanation / Answer
public class TracingMethodCalls {public static int compute(int x, int y) {x += 3;y = 2*y - x;System.out.println(x + " " + y);return x;}public static void main(String[] args) {int x = 1;int y = 3;x = compute(x, y);System.out.println(x + " " + y);compute(y, y);System.out.println(x + " " + y);y = 3 + 4 * compute(y, x);System.out.println(x + " " + y);}} x = 1 + 3 will be updated. and will be returned y = 2*3-4 = 6-4 = 2 4 2 4,2 will be printed 4 3 4,3 will be printed .6 0 again calling compute (x,y) 4 3 again 4,3will be printed. 6 2 call compute(y,x) 4 27 print final values
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.