Question: HELP CODING WITH JAVA Situation #2. You are given a String str and you need to modify it as described in this problem The one

HELP CODING WITH JAVA

Situation #2.

You are given a String str and you need to modify it as described in this problem

HELP CODING WITH JAVA Situation #2. You are given a String str

The one below is with recursion

and you need to modify it as described in this problem The

Here is my code that is given

public static String removeMystery1(String str) {

int[] array = new int[str.length()];

String newStr = "";

int sum = 0;

int increment = -1;

for (int i=0; i

sum = sum + ((increment++) + 1);

array[i] = sum;

}

boolean addIndexOrNot = true;

for (int i = 0; i

for (int j = 0; j

if (i == array[j]) addIndexOrNot = false;

}

if (addIndexOrNot) {

char currentChar = str.charAt(i);

newStr = newStr + currentChar;

}

addIndexOrNot = true;

}

return newStr;

}

and:

public static String removeMystery2(String str) {

String result = "";

int toBeRemoved=0;

int count = 1;

// this for loop looks at every char of str and decides whether to copy it in result or not.

for (int i = 0; i

// decide whether to copy char in result

if (i != toBeRemoved) {

result += str.charAt(i);

} else {

toBeRemoved = toBeRemoved + count;

count++;

}

}

return result;

}

Your job is the following:

  • For each of the above methods, you have to add a variable numSteps and insert it into the code so as to count the number of steps each execution of the given method performs. You will create two new methods: removeMystery1WithNumSteps and removeMystery2WithNumSteps.
  • You have to run experiments so as to compute the number of steps for several strings as shown below and fill out the table below:

Table that you have to fill out:

Input string

removeMystery1withNumSteps

removeMystery2withNumSteps

abcdef

abcdefghijklmnopqrstuvwxyz

abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz

public static String removeMystery(String str) { String result = ""; int toBeRemoved=0; int count = 1; // this for loop looks at every char of str and decides whether to copy it in result or not. for (int i = 0; i ignore / do not include it in the return // keep all characters between index 1 and the next value of our counter (excluded) --> // counter++; // str.substring(1,counter); // call method on string starting at counter // RMRaux(str.substring(counter),counter); counter++; if (counter >= str.length()) // it means that we do not need to recursively call RMRaux and all characters //after the one we remove will be included in the solution return str.substring(1); else return str.substring(1,counter) + RMRaux(str.substring(counter),counter); }

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!