Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

image text in transcribed

image text in transcribed

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

Find y'. y= |x + X (x) (x) X 1 02x+ 2x 1 O 2x + 1/3 Ex 2x +

Answered: 1 week ago