Question: LAB2 Lab Assignments: #0: Write a static method called swap. Use the following program structure to see how swap method works. 1 public class SwapTest

LAB2

Lab Assignments:

#0: Write a static method called swap. Use the following program structure to see how swap method works.

1 public class SwapTest {

2 public static void main(String[] args) {

3 int x;

4 int y;

5 x = 5;

6 y = 10;

7 System.out.println("In main(), x:" + x + ", y:" + y);

8 swap(x, y);

9 } // end of main()

10 public static void swap(int x, int y) {

11 int z;

12 z = x;

13 x = y;

14 y = z;

15 System.out.println("In swap(), x:" + x + ", y:" + y);

16 }

17 }

#1: Trace the evaluation of the following expressions, and give their resulting values: (1pt)

a. 2 + 3 * 4 - 6

b. 14 / 7 * 2 + 30 / 5 + 1

c. (12 + 3) / 4 * 2

d. (238 % 10 + 3) % 7

e. (18 - 7) * (43 % 10)

f. 2 + 19 % 5 - (11 * (5 / 2))

g. 813 % 100 / 3 + 2.4

h. 26 % 10 % 4 * 3

i. 22 + 4 * 2

j. 23 % 8 % 3

k. 12 - 2 -3

l. 6 / 2 + 7 / 3

m. 6 * 7 % 4

n. 3 * 4 + 2 * 3

o. 177 % 100 % 10 / 2

p. 89 % (5 + 5) % 5

q. 392 / 10 % 10 / 2

r. 8 * 2 - 7 / 4

s. 37 % 20 % 3 * 4

t. 17 % 10 / 4

#2. Trace the evaluation of the following expressions, and give their resulting values: (1pt)

a. 2 + 2 + 3 + 4

b. "2 + 2" + 3 + 4

c. 2 + " 2 + 3 " + 4

d. 3 + 4 + " 2 + 2"

e. "2 + 2 " + (3 + 4)

f. "(2 + 2) " + (3 + 4)

g. "hello 34 " + 2 * 4

h. 2 + "(int) 2.0" + 2 * 2 + 2

i. 4 + 1 + 9 + "." + (-3 + 10) + 11 / 3

j. 8 + 6 * -2 + 4 + "0" + (2 + 5)

k. 1 + 1 + "8 - 2" + (8 - 2) + 1 +1

l. 5 + 2 + "(1 + 1)" + 4 + 2 * 3

m. "1" + 2 + 3 + "4" + 5 * 6 + "7" + (8 + 9)

#3. The following program contains several (7) mistakes! What are they? (1pt)

1 public class Oops2 {

2 public static void main(String[] args) {

3 int x;

4 System.out.println("x is" x);

5

6 int x = 15.2; // set x to 15.2

7 System.out.println("x is now + x");

8

9 int y; // set y to 1 more than x

10 y = int x + 1;

11 System.out.println("x and y are " + x + and + y);

12 }

13 }

#4. What are the values of first and second at the end of the following code? How would you describe the net effect of the code statements in this exercise? (1pt)

int first = 8;

int second = 19;

first = first + second;

second = first - second;

first = first - second;

# 5. Rewrite the code from the previous exercise to be shorter, by declaring the variables together and by using the special assignment operators (e.g., +=, -=, *=, and /=) as appropriate. (1pt)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!