Question: JAVA Problem 10: Sequence Consider the class Sequence given here: Sequence.java 2. Modify this class by adding two methods: 1. public Sequence append(Sequence other) This

JAVA Problem 10: Sequence Consider the class Sequence given here: Sequence.java 2.JAVA

Modify this class by adding two methods: 1. public Sequence append(Sequence other)

Problem 10: Sequence Consider the class Sequence given here: Sequence.java 2. Modify this class by adding two methods: 1. public Sequence append(Sequence other) This method creates a new sequence, appending this and the other sequence, without modifying either sequence. For example, if a is the sequence 1 4 9 16 and b is the sequence 9 7 4 9 11 then the call to a .append(b) returns the sequence 1 4 9 16 9 7 4 9 11 without modifying a orb. 2. public Sequence merge (Sequence other) This method merges two sequences, alternating elements 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 bis 9 7 4 9 12 then a merge (b) returns the sequence 1947 9 4 16 9 11 without modifying a orb. You can assume that all sequences will always contain at least one number. Example: Sequence firstSequence = new Sequence(); firstSequence.add (1); first Sequence.add(4); first Sequence.add(9); firstSequence.add(16); Sequence secondSequence = new Sequence(); secondSequence.add(9); secondSequence.add(7); secondSequence.add(4); secondSequence.add(9); secondSequence.add(11); Sequence appended Sequence = firstSequence.append(secondSequence); appendedSequence.toString(); // returns "[1, 4, 9, 16, 9, 7, 4, 9, 11]" firstSequence.toString(); // returns "[1, 4, 9, 16)" secondSequence.toString(); // returns "[9, 7, 4, 9, 11]" Sequence mergedSequence = firstSequence merge (secondSequence); mergedSequence.toString(); // returns "[1, 9, 4, 7, 9, 4, 16, 9, 11]" firstSequence.toString(); // returns "[1, 4, 9, 16)" secondSequence.toString(); // returns "[9, 7, 4, 9, 11]" import java.util.ArrayList; public class Sequence { private ArrayList values; public Sequence() { values = new ArrayList(); } public ArrayList getValues() { return values; } public void add(int n) { values.add(n); } public String tostring() { return values.toString(); } }

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!