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...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
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...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s) For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d" printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd" printSub4 will print "abcd", "bcd",...
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
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...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the...
THIS IS EXAM REVIEW, so please make sure to show the work and make sure the work is correct. In all tests of hypothesis use the 5% level of significance unless told otherwise. In all confidence interval problems use 95% confidence unless told otherwise. SHOW YOUR WORK. 2. Test scores for a mathematics course have a normal distribution with a mean of 76 and a standard deviation of 12. a.What proportion of scores will be between 70 and 90? b....
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT