Question: CODE IN JAVA PLEASE Situation #2. For this second situation, You are given a String str and you need to modify it. We provide you

CODE IN JAVA PLEASE

CODE IN JAVA PLEASE Situation #2. For this second situation, You are

given a String str and you need to modify it. We provideyou with 2 pieces of code (both iterative approaches), as shown below:public static String removeMystery1(String str) { int[] array = new int[str.length()]; StringnewStr = ""; int sum = 0; int increment = -1; for(int i=0; i sum = sum + ((increment++) + 1); array[i] =

Situation #2. For this second situation, You are given a String str and you need to modify it. We provide you with 2 pieces of code (both iterative approaches), as shown below:

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

What are you asked to do?

You are asked to do the following:

  • For Situation #1:
    • Turn in your java file: Situation1.java.
    • Follow instructions and fill in the given table with results of your experiments
    • Conclude on the type of cost function of n for each method: MaxPairwiseProduct1 and MaxPairwiseProduct2.
  • For Situation #2:
    • Turn in your java file: Situation2.java.
    • Write code as instructed.
    • Follow instructions and fill in the given table with results of your experiments
    • Conclude on the type of cost function of n for each method: removeMystery1WithNumSteps and removeMystery2WithNumSteps

So for instance, if A = {4,2,7,1,9), the maximum value of the product of two of A's distinct values is 63. We share with you two approaches to solving this problem. We provide you with the code of each method below: /* input A: * is non empty * and contains only non-negative values */ public static int MaxPairwise Productl(int[] A) { int maxProduct = 0; for (int i=0; i maxProduct) maxProduct = A[i]*A[j]; } } return maxProduct; } Situation #1. For this first situation, we are doing the work with you. You simply have to follow and copy our steps, so that you learn and are ready to take on Situation #2 more on your own. You are given an array A of non-negative integers and your job is to identify the maximum value of the product of two distinct numbers in A. So for instance, if A = {4,2,7,1,9), the maximum value of the product of two of A's distinct values is 63. We share with you two approaches to solving this problem. We provide you with the code of each method below: /* input A: * is non empty * and contains only non-negative values */ public static int MaxPairwise Product1(int[] A) { int maxProduct = 0; for (int i0; i maxProduct) maxfcedust = A[i]*A[i]: } ) return maxPrepusti } /* input A * is non emptyl * and contains only non-negative values */ public static int MaxPairwiseProduct2(int[] A) { int indexMax = 0;// index of max value in A int indexMax2 = 0; // index of second max value in A for (int i=1;i AlingexMax]) indexMax = 1; > for (int i=1;i A[indexMax2]) indexMax2 = > return Alindex Max)*A[indexMax2); Here is the modified code 1: /* input A: * is non empty * and contains only non-negative values */ public static int MaxPairwise Product withNumSteps(int[] A) { int numSters = 0; // we declare and initialize our number of steps. int maxPredust = 0; DumSters++; // for the declaration of maxPreduct as a step numSters+ = 2; // for the declaration of and checking the condition i maxProduct if (A[i]*Ati] > DaxPcedust) maxPredust - All*AU: dum Steps++; // for the update of max[reduct } numsters + = 2; // for the update of j checking the condition j A[indexMax]) is not exactly 1 but that's the approximation we are using here, for simplicity. Ok, we have these codes, what do we do next? We need to run these codes on a number of inputs of varying sizes. Here is what we ask you to do. For each of the above code, do the following: Run your code on 3 randomly generated arrays of size 3: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 10 randomly generated arrays of size 10: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 50 randomly generated arrays of size 50: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 100 randomly generated arrays of size 100: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 200 randomly generated arrays of size 200: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 500 randomly generated arrays of size 500: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 1000 randomly generated arrays of size 1000: o For each run, collect the number of steps. Report the average in a table like the one shown below. Note: the number of executions is not scientifically informed: it is just for now for you to practice generating input and running inputs. Table that you have to fill out: Size of input array MaxPairwise Product 1 with NumSteps MaxPairwise Product2withNumSteps 3 10 50 100 200 500 1000 Let's look at some code: you can do it otherwise, but the code we provide is one way to do it to TEST public static int[] costMaxPairwise Product10 { // one int per cost, one average cost per size (3, 10, 50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000); int[] averages = new int[7]; for (int =0; i maxProduct) maxProduct = A[i]*A[j]; } } return maxProduct; } Situation #1. For this first situation, we are doing the work with you. You simply have to follow and copy our steps, so that you learn and are ready to take on Situation #2 more on your own. You are given an array A of non-negative integers and your job is to identify the maximum value of the product of two distinct numbers in A. So for instance, if A = {4,2,7,1,9), the maximum value of the product of two of A's distinct values is 63. We share with you two approaches to solving this problem. We provide you with the code of each method below: /* input A: * is non empty * and contains only non-negative values */ public static int MaxPairwise Product1(int[] A) { int maxProduct = 0; for (int i0; i maxProduct) maxfcedust = A[i]*A[i]: } ) return maxPrepusti } /* input A * is non emptyl * and contains only non-negative values */ public static int MaxPairwiseProduct2(int[] A) { int indexMax = 0;// index of max value in A int indexMax2 = 0; // index of second max value in A for (int i=1;i AlingexMax]) indexMax = 1; > for (int i=1;i A[indexMax2]) indexMax2 = > return Alindex Max)*A[indexMax2); Here is the modified code 1: /* input A: * is non empty * and contains only non-negative values */ public static int MaxPairwise Product withNumSteps(int[] A) { int numSters = 0; // we declare and initialize our number of steps. int maxPredust = 0; DumSters++; // for the declaration of maxPreduct as a step numSters+ = 2; // for the declaration of and checking the condition i maxProduct if (A[i]*Ati] > DaxPcedust) maxPredust - All*AU: dum Steps++; // for the update of max[reduct } numsters + = 2; // for the update of j checking the condition j A[indexMax]) is not exactly 1 but that's the approximation we are using here, for simplicity. Ok, we have these codes, what do we do next? We need to run these codes on a number of inputs of varying sizes. Here is what we ask you to do. For each of the above code, do the following: Run your code on 3 randomly generated arrays of size 3: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 10 randomly generated arrays of size 10: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 50 randomly generated arrays of size 50: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 100 randomly generated arrays of size 100: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 200 randomly generated arrays of size 200: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 500 randomly generated arrays of size 500: o For each run, collect the number of steps. o Report the average in a table like the one shown below. Run your code on 1000 randomly generated arrays of size 1000: o For each run, collect the number of steps. Report the average in a table like the one shown below. Note: the number of executions is not scientifically informed: it is just for now for you to practice generating input and running inputs. Table that you have to fill out: Size of input array MaxPairwise Product 1 with NumSteps MaxPairwise Product2withNumSteps 3 10 50 100 200 500 1000 Let's look at some code: you can do it otherwise, but the code we provide is one way to do it to TEST public static int[] costMaxPairwise Product10 { // one int per cost, one average cost per size (3, 10, 50, 100, 200, 500, 1000) int[] sizes = {3, 10, 50, 100, 200, 500, 1000); int[] averages = new int[7]; for (int =0; i

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!