Question

In: Computer Science

(java) Show what the following program would print. Be sure to show work space. (Hint: Watch...

(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;

}

}

Solutions

Expert Solution

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...!!!


Related Solutions

What java program would you write to solve the following problems and why does it work?...
What java program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Implement MyArrayStack (constructor, push, pop, peek and isEmpty), and MyLinkedQueue with both next and previous pointers (constructor, enqueuer/offer, dequeuer/poll, peek and isEmpty), and write the following two program to test them. You must use MyArrayList or MyLinkedList for the implementation. 2) For stack testing, write a program to check if a string...
What java program would you write to solve the following problems and why does it work?...
What java program would you write to solve the following problems and why does it work? Please also comment on other students’ code at least three times. 1) Implement MyArrayList and MyLinkedList using MyList interface and MyAbstractList as defined in Java Collection Framework. 2) For the following problem use your own created MyArrayList or MyLinkedList if needed. Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully...
Program Specifications: You will be writing a Java program called ASCIIRabbit.java that will print the following...
Program Specifications: You will be writing a Java program called ASCIIRabbit.java that will print the following ASCII objects. There should be two blank lines between each shape and your output must match identically. Rabbit In A Hat -------------------- (^\ /^) \ V / (o o) ( (@) ) --------- | | | | | | _______ Rabbit Under A Hat -------------------- _______ | | | | | | --------- /\"/\ () * () ("")_("") Rabbit Wears A Hat ------------------ _______...
Using Matlab Write a program that print the following matrix. (hint: use nested for-loop and if-statement)...
Using Matlab Write a program that print the following matrix. (hint: use nested for-loop and if-statement) mat = [2 -1 0 0 0 -1 2 -1 0 0 0 0 -1 2 -1 0 0 0 -1 2];
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Show all of your work in the space provided. Show all of your work to get...
Show all of your work in the space provided. Show all of your work to get full credit. All answers have to be in SI units(20 pints for each questions). 2.Following are the two sets of position and time data collected using a motion sensor and cart.        Table 1. table 2 Time (s) Position (m) V(instantaneous) (m/s) 0 10.0 0.25 13.0 0.50 16.5 0.75 21.0 1.00 26.5 1.25 32.3 1.50 38.0 1.75 44.5 2.00 52.0 2.25 60.0 2.50 68.5...
in java Write a program that will print the following output 1 1 2 1 1...
in java Write a program that will print the following output 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 18 8 4 2 1
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Directions: Complete each of the following problems. Be sure to show your work in order to...
Directions: Complete each of the following problems. Be sure to show your work in order to receive full or partial credit. Calculators are allowed along with 1 page (1 side) of notes any any necessary statistical tables. 1. (True / False) Estimating parameters and testing hypotheses are two important aspects of descriptive statistics. 2. (True / False) A statistic is calculated from a population and a parameter is calculated from a sample.   3. (True / False) Descriptive statistics include visual...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT