sider the following class: blic class Sequence private ArrayList values; public Sequence() { values new ArrayList(); } public void add(int n) { values.add(n); } public String toString() { return values.toString(); }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
Exercise 2:
Consider the following class:
public class Sequence
{
private ArrayList<Integer> values;
public Sequence() { values = new ArrayList<Integer>(); }
public void add(int n) { values.add(n); }
public String toString() { return values.toString(); }
}
Add a method
public Sequence merge(Sequence other)
that merges two sequences, alternating ele-
ments from both sequences. If one sequence is shorter than the other, then alternate
as long as you can and then append the remaining elements from the longer
sequence. For example, if a is
1 4 9 16
and b is
9 7 4 9 11
then a.merge(b) returns the sequence
1 9 4 7 9 4 16 9 11
without modifying a or b.
Transcribed Image Text:Exercise 2: Consider the following class: public class Sequence { private ArrayList<Integer> values; public Sequence() { values = new ArrayList<Integer>(); } public void add(int n) { values.add(n); } public String toString() { return values.toString(); } } Add a method public Sequence merge(Sequence other) that merges two sequences, alternating ele- ments from both sequences. If one sequence is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer sequence. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then a.merge(b) returns the sequence 1 9 4 7 9 4 16 9 11 without modifying a or b.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT