Question: I'm just trying to figure out the methods for the Sequence class. Most of the stuff is set up though. 1 E7.23 Add a method

 I'm just trying to figure out the methods for the Sequence

I'm just trying to figure out the methods for the Sequence class. Most of the stuff is set up though.

class. Most of the stuff is set up though. 1 E7.23 Add

a method public Sequence merge(Sequence other) to the Sequence class of Exercise

- E7.22 that merges two sequences, alternating elements from both sequences. If

1 E7.23 Add a method public Sequence merge(Sequence other) to the Sequence class of Exercise - E7.22 that 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 149 16 and bis 974 9 11 then a.merge(b) returns the sequence 19 4 7 9 4 16 9 11 without modifying a or b. E7.24 Add a method public Sequence merge Sorted(Sequence other) to the Sequence class of Exercise - E7.22 that merges two sorted sequences, producing a new sorted sequence. Keep an index into each sequence, indicating how much of it has been processed already. Each time, append the smallest unprocessed value from either sequence, then advance the index. For example, if a is 1 4 9 16 and bis 4799 11 then a merge Sorted(b) returns the sequence 1447999 11 16 If a orb is not sorted, merge the longest prefixes of a and b that are sorted. public class SequenceTester { public static void main(String[] args) { Sequence firstSequence = new Sequence(); firstSequence.add(1); first Sequence.add(4); first Sequence.add(9); firstSequence.add(16); System.out.print("First before: "); System.out.println(firstSequence.toString()); Sequence secondSequence = new Sequence(); secondSequence.add(9); secondSequence.add(7); secondSequence.add(4); secondSequence.add(9); secondSequence.add(11); System.out.print("Second before: "); System.out.println(secondSequence.toString()); //For E7.20 Sequence combinedSequence = firstSequence.append( secondSequence); System.out.print("Appended: "); System.out.println(combinedSequence.toString()); System.out.println("Expected: [1, 4, 9, 16, 9, 7, 4, 9, 11]"); = // For E7.21 Sequence combined Sequence2 first Sequence. merge(secondSequence); System.out.print("Merged: "); System.out.println(combinedSequence2.toString()); System.out.println("Expected: [1, 9, 4, 7, 9, 4, 16, 9, 11]"); System.out.print("First after: "); System.out.println(firstSequence.toString()); System.out.print("Second after: "); System.out.println(secondSequence.toString()); } import java.util.ArrayList; public class Sequence { private ArrayList values; //constructor public Sequence() { values = new ArrayList(); } T/add a value to the end of the arraylist public void add(int n) { values.add(n); } //return a string of all the arraylist values 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!