Write a method named repeatElements that accepts an ArrayList of strings and an integer "repeat factor" k as parameters. The method will change the ArrayList in which each element of the ArrayList will be copied k times. For example, if a variable called list contains the elements ["hi", "how are", "you?"], then the call of stretchElements(list, 3); will change the ArrayList to contain ["hi", "hi", "hi", "how are", "how are", "how are", "you?", "you?", "you?"]. Write Java statements that first get number of strings and then the strings from the user and add themto a list, also get the value of k from the user. Then print the ArrayList, call the method, then print the ArrayList again.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a method named repeatElements that accepts an ArrayList of strings and an integer "repeat factor" k as parameters. The method will change the ArrayList in which each element of the ArrayList will be copied k times. For example, if a variable called list contains the elements ["hi", "how are", "you?"], then the call of stretchElements(list, 3); will change the ArrayList to contain ["hi", "hi", "hi", "how are", "how are", "how are", "you?", "you?", "you?"]. Write Java statements that first get number of strings and then the strings from the user and add themto a list, also get the value of k from the user. Then print the ArrayList, call the method, then print the ArrayList again.

Note 1- If a stretch factor of 0 or less is passed, then the list is made empty. 2 - If an empty list is passed in, regardless of the stretch factor, the list should still be empty at the end of the call. You may assume that the list passed is not null. You may not use any other arrays, arraylists, or other data structures to help you solve this problem, though you can create as many simple variables as you like.

Part-3:
Enter number of strings: 3
Enter 3 strings:
Hi
How are
You?
Enter the value of k (stretch value): 2
The original arraylist is: ["Hi" "how are" "you"]
The repeated arraylist is: ["Hi" "Hi" "how are" "how are" "you" "you"]
Transcribed Image Text:Part-3: Enter number of strings: 3 Enter 3 strings: Hi How are You? Enter the value of k (stretch value): 2 The original arraylist is: ["Hi" "how are" "you"] The repeated arraylist is: ["Hi" "Hi" "how are" "how are" "you" "you"]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY