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

it should be user interactive by using a scanner to ask how many copies and to e

ID: 3910658 • Letter: I

Question

it should be user interactive by using a scanner to ask how many copies and to enter the string

11. Write a method called stutter that accepts an ArrayList of strings and an integer k as parameters and that before the method is called and k is 4, it should store the values ["how", "how", "how*, "how, replaces every string with k copies of that string. For example, if the list stores the values ["how", are-, "you?"] "are", "are", "are", "are", "you?", "you?, "you?", "you?"] after the method finishes executing If k is 0 or negative, the list should be empty after the call.

Explanation / Answer

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

public class Stutter {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

ArrayList<String> strings = new ArrayList<>();

System.out.println("Enter numbeer of strings");

int n = Integer.parseInt(sc.nextLine());

for (int i = 1; i <= n; i++) {

System.out.println("Enter string " + i + ": ");

strings.add(sc.nextLine());

}

System.out.println("Enter number of copies:");

int k = Integer.parseInt(sc.nextLine());

System.out.println("List before modification: ");

System.out.println(strings);

strings = stutter(strings, k);

System.out.println("List after modification: ");

System.out.println(strings);

}

private static ArrayList<String> stutter(ArrayList<String> strings, int k) {

// TODO Auto-generated method stub

if (k <= 0)

strings = new ArrayList<>();

else {

ArrayList<String> newList = new ArrayList<>();

for (int i = 0; i < strings.size(); i++) {

for(int j = 0; j<k;j++)

newList.add(strings.get(i));

}

strings = newList;

}

return strings;

}

}