In: Computer Science
(java) Show what the following program would print. Be sure to show work space. (Hint: Watch out for integer division) SHOW WORK
public class Q2{
public static void main(String[] args){
int a = 1, b = 4, c = 5;
double x = 3.0;
a = method (b,x);
System.out.println( "first: " + a + " " + b + " " + c);
a = 12;
b = 5;
c = method(a - b, 10.0) * 2;
System.out.println( "then: " + c);
}
public static int method (int t, double v){
int w;
if (v > 5.0)
w = t / 3 + 4;
else
w = 25;
System.out.println( "in method: " + w);
return 2 * w;
}
}
Output:-
in method: 25
first: 50 4 5
in method: 6
then: 24
Output Screenshot:-
Explanation:-
First it will go to main class and there the variables with values are
a = 1
b = 4
c = 5
x = 3.0 (it is double)
The next statement is a = method(b,x)
The it will calls the method function with values b and x those are 4 and 3.0
In method the variables are t and v, then t = 4 and v = 3.0 and a variable w with integer type.
In if condition v > 5.0 ---> Here v value we have 3.0 so the condition is false.
Then it will go to else condition
In else condition w is declared as 25 and next prints in method: w value
which is in method: 25
Then it will return 2*w
That is 50.
We called the function a = method(b,x)
Then the 50 value is returned to a. So a = 50
Now in main mehotd the a = method(b,x) is over.
Next statement it prints first: a b c
Which means we get a is 50 and b is 4 and c is 5 as declared. So the output is first: 50 4 5
Then next we declared a = 12 and b = 5
and for c we called the method function as c = method(a-b,10.0)*2
Here a is 12 and b is 5. After calling the method function the method function has variables t and v. In the method the variables are a-b and 10.0
So t is a-b and v is 10.0
Then the values are t is 12-5 which is 7 and v is 10.0
w is in declared variable in method function as before.
In if condition v>5.0 ---> which means 10.0>5.0
The condition is true the w is t/4+4
Which is 7/3+4 and by simpifying we will get 6
Then it prints in method: w value
Which is in method: 6
and returns 2*w for the method. Which means it returns 2*6 that is 12. It will return the value 12 for c. Because we called the method for the value c which is c=method(a-b,10.0)
In main function we get method(a-b,10.0) is 12 and in the statement it has *2 so the c value is 24
Next it will prints then: c value
Which is then: 24
Please UPVOTE thank you...!!!