Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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. 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started