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

Tracing the loops below in the table at the right. You may add more lines to the

ID: 3821963 • Letter: T

Question

Tracing the loops below in the table at the right. You may add more lines to the table if you need them. Only the variables in the table need to be traced. If the loop is an infinite loop, trace three iterations and write “Infinite.” Add more lines if you need them.

a)          StringBuilder input = new StringBuilder(“abcdefg”);

              StringBuilder result = new StringBuilder();

              for(int index = 0; index< input.length(); ++index)

              {

                             char next = input.charAt(index);

                             input.deleteCharAt(index);

                             next = Character.toUpperCase(next);

                             result.insert(0, next);

              }

index

next

input

result

b)

String source = "abcdac";

StringBuilder result = new StringBuilder("");

for (int index = 0; index < source.length(); ++index)

{

      for (int other = 0; other < source.length(); ++other)

      {

            if (source.charAt(index) == source.charAt(other)

&& index != other)

            {

                  char rep = source.charAt(index);

                  result.append(rep);

            }

      }

}

index

source.charAt(index)

other

source.charAt(other)

result

c)

String source = "pqrsqrpr";

StringBuilder result = new StringBuilder("");

for (int index = 0; index < source.length(); ++index)

{

      for (int other = index+1; other < source.length(); ++other)

      {

            if (source.charAt(index) == source.charAt(other))

            {

                  char rep = source.charAt(index);

                  result.insert(0,rep);

            }

      }

}

index

source.charAt(index)

other

source.charAt(other)

result

index

next

input

result

Explanation / Answer

a)

index

next

input

result

0

a

bcdefg

A

1

b

cdefg

BA

2

c

defg

CBA

3

d

efg

DCBA

4

e

fg

EDCBA

5

f

g

FEDCBA

6

g

GFEDCBA

In the first iteration of the loop index value is 0, So and the condition in the for loop satisfies, then it enters to the loop. This time next variable is initialised and stores the 0th position character in the next variable which is a. Then input.delete which deletes the character from the input string , so input string contains bcdefg only , then next value will be capitalised and then the character in the next variable will get inserted in the result variable . Now loop get ending then index value will be inceremented by 1.

And again the same procedure happened and the resulting values is shown in the above table.

index

next

input

result

0

a

bcdefg

A

1

b

cdefg

BA

2

c

defg

CBA

3

d

efg

DCBA

4

e

fg

EDCBA

5

f

g

FEDCBA

6

g

GFEDCBA

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote