SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* *********************************** *
  2.  * Title: Question 1 - Merge Sequence  *
  3.  * Author: Austin Page - 100725236     *
  4.  * Date: 11/14/19                      *
  5.  * Assignment: #3                      *
  6.  * *********************************** */
  7.  
  8. import java.util.ArrayList; // ArrayList import
  9.  
  10. // MergeSequence Class
  11. public class MergeSequence
  12. {
  13.     // Main method
  14.     public static void main(String[] args)
  15.     {
  16.         // MergeSequence Objects to test program functionality
  17.         MergeSequence a = new MergeSequence(); // MergeSequence 'a'
  18.         MergeSequence b = new MergeSequence(); // MergeSequence 'b'
  19.  
  20.         // Add numbers to a
  21.         a.add(1);
  22.         a.add(4);
  23.         a.add(9);
  24.         a.add(16);
  25.  
  26.         // Add numbers to b
  27.         b.add(9);
  28.         b.add(7);
  29.         b.add(4);
  30.         b.add(9);
  31.         b.add(11);
  32.  
  33.         // Output a and b before the append method
  34.         System.out.println("MergeSequence 'a' before append method: " + a);
  35.         System.out.println("MergeSequence 'b' before append method: " + b);
  36.  
  37.         System.out.println("'b' appended to 'a':                    " + a.append(b)); // append b to a, and print the result
  38.  
  39.         // Output a and b after the append method, to confirm nothing changed
  40.         System.out.println("MergeSequence 'a' after append method:  " + a);
  41.         System.out.println("MergeSequence 'b' after append method:  " + b);
  42.     }
  43.  
  44.     private ArrayList<Integer> values;
  45.  
  46.     public MergeSequence ()
  47.     {
  48.         values = new ArrayList<Integer>();
  49.     }
  50.  
  51.     public void add(int n)
  52.     {
  53.         values.add(n);
  54.     }
  55.  
  56.     public String toString()
  57.     {
  58.         return values.toString();
  59.     }
  60.  
  61.     // Append method
  62.     public MergeSequence append (MergeSequence other)
  63.     {
  64.         MergeSequence result = new MergeSequence(); // Create a new MergeSequence Object that will be returned
  65.  
  66.         // Loop through all the numbers in the MergeSequence Object that the method was called on, and add each number to the result MergeSequence Object
  67.         for (int i = 0; i < this.values.size(); i++)
  68.         {
  69.             result.add(this.values.get(i));
  70.         }
  71.  
  72.         // Loop through all the numbers in the MergeSequence Object that was passed in, and add each number to the result MergeSequence Object
  73.         for (int i = 0; i < other.values.size(); i++)
  74.         {
  75.             result.add(other.values.get(i));
  76.         }
  77.  
  78.         return result; // Return the result
  79.  
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top