Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following class: public class MergeSequence {private ArrayList valu

ID: 3938911 • Letter: C

Question

Consider the following class: public class MergeSequence {private ArrayList values; public MergeSequence () {values = new ArrayList();} public void add(int n) {values.add(n);} public String toString() {return values. toString ();}} Add a method public MergeSequence append (MergeSequence other) that creates a new sequence, appending this and the other sequence, without modifying either sequence. For example, if sequence is: 14 9 16 and b is the sequence 9 7 4 9 11 then the call a append(b) returns the sequence 14 9 16 9 7 4 9 11 without modifying a or b. Use JUnit testing framework to verify the functionality of your MergeSequence Class.

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.ArrayList;

public class MergeSequence {

  

   private ArrayList<Integer> values;

  

   public MergeSequence(){

       values = new ArrayList<>();

   }

  

   public void add(int n){

       values.add(n);

   }

  

   public String toString(){

       return values.toString();

   }

  

   //1

   public MergeSequence append(MergeSequence other){

      

       MergeSequence result = new MergeSequence();

      

       // adding current object values

       for(Integer x : this.values){

           result.add(x);

       }

      

       // adding other values

       for(Integer x : other.values){

           result.add(x);

       }

      

       // returning result

       return result;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote