Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Trace the values of the following variables: a) (Hint: Watch out for incremen

ID: 3940225 • Letter: 1

Question

1.      Trace the values of the following variables:

a)         (Hint: Watch out for increment, it is not the normal ++)

String input = "helloThere";

char currentChar;

for (int i = 1; i < input.length(); i += 2) {

      currentChar = input.charAt(i);

      input = input.replace(currentChar, 'x');

      // Read the API

}

Trace Values

i

currentChar

input

b) To show lists, use curly braces to enclose the list, and separate the elements with commas (e.g. {a, b, c}). Show each time a value is assigned to a variable- this is a trace, not a memory diagram.

StringBuilder sb = new StringBuilder("helloThere");

for (int index = 0; index < sb.length(); index = index + 3) {

// watch increment—it is not 1

      sb.append("!");

}

Trace Values

index

sb

Trace Values

i

currentChar

input

Explanation / Answer

[1]

i

Currentchar

input

1

e

hxlloThxrx

3

l

hxxxoThxrx

5

T

hxxxoxhxrx

7

X

hxxxoxhxrx

9

x

hxxxoxhxrx

[2]

i

sb

0

helloThere!

3

helloThere!!

6

helloThere!!!

9

helloThere!!!!

12

helloThere!!!!!

i

Currentchar

input

1

e

hxlloThxrx

3

l

hxxxoThxrx

5

T

hxxxoxhxrx

7

X

hxxxoxhxrx

9

x

hxxxoxhxrx