Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribedJAVA

image text in transcribed

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

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

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago