Advertisement
NYCJacob

arrayList

Sep 16th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. //
  2. //Complete the class ArrayListMethods. It consists of four short methods to manipulate an array list of strings.
  3. //The method header and javadoc are given to you.
  4. //
  5. // Need help starting this question? In the lesson titled
  6. // "Starting points: Problem Set Questions", go to the
  7. // problem titled "Problem Set 6 - Question 1" for some tips on
  8. // how to begin.
  9. //
  10.  
  11. import java.util.ArrayList;
  12. public class ArrayListMethods
  13. {
  14.     ArrayList<String> list; //instance variable
  15.     /**
  16.      * Constructor for objects of class ArrayListMethods
  17.      */
  18.     public ArrayListMethods(ArrayList<String> arrayList)
  19.     {
  20.         // initialise instance variables
  21.         list = arrayList;
  22.     }
  23.  
  24.     /**
  25.      * Determines if the array list is sorted (do not sort)
  26.      * When Strings are sorted, they are in alphabetical order
  27.      * Use the compareTo method to determine which string comes first
  28.      * You can look at the String compareTo method in the Java API
  29.      * @return true if the array list is sorted else false.
  30.      */
  31.     public boolean isSorted()
  32.     {
  33.         boolean sorted = true;
  34.         int result = 0;
  35.         // TODO: Determine if the array is sorted.
  36.         String first = "";
  37.         String second = "";
  38.         for (int i = 0; i < list.size() -1; i++)
  39.         {
  40.             first = list.get(i);
  41.             second = list.get(i + 1);
  42.             result = first.compareTo(second);
  43.             if (result > 0)
  44.             {
  45.                 sorted  = false;
  46.             }
  47.             else
  48.             {
  49.                sorted = true;
  50.             }
  51.         }
  52.        
  53.         return sorted;
  54.     }
  55.  
  56.     /**
  57.      * Replaces all but the first and last elements with the larger of its two neighbors
  58.      * You can use the compareTo() method to determine which string is larger (larger in alphabetical
  59.      * order).
  60.      * Example: if the list is originally
  61.      *    ["cat", "ape", "dog", "horse", "zebra"]
  62.      * after this method it should be:
  63.      *    ["cat", "dog", "horse", "zebra", "zebra"]
  64.      */
  65.     public void replaceWithLargerNeighbor()
  66.     {
  67.         String move1 = "";
  68.         String move2 = "";
  69.         // TODO: Replace all but the first and last elements with the larger of its to neighbors
  70.         for (int i = 1; i < list.size() - 1; i++)
  71.         {
  72.             move1 = list.get(i -1);
  73.             move2 = list.get(i + 1);
  74.             if(move1.compareTo(move2) > 0)
  75.                {
  76.                    list.set(i, move1);
  77.                }
  78.             else if (move2.compareTo(move1) > 0)
  79.                {
  80.                    list.set(i, move2);
  81.                }
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Gets the number of duplicates in the list.
  87.      * Be careful to only count each duplicate once. Start at index 0. (Does it match any of the other elements?)
  88.      * Get the next word. It is at index i. (Does it match any of the words with index > i?)
  89.      * @return the number of duplicate words in the list
  90.      */
  91.     public int countDuplicates()
  92.     {
  93.         int duplicates = 0;
  94.  
  95.         // TODO: Write the code to get the number of duplicates in the list
  96.         for (int x = 0; x < list.size() - 1; x++)
  97.         {
  98.             for (int y = x + 1; y < list.size(); y++)
  99.             {
  100.                 if ( list.get(x).compareTo(list.get(y)) == 0)
  101.                     {
  102.                         duplicates++;
  103.                     }
  104.             }
  105.  
  106.         }
  107.  
  108.         return duplicates;
  109.     }
  110.  
  111.      /**
  112.      * Moves any word that starts with x, y, or z to the front of the ArrayList, but
  113.      * otherwise preserves the order
  114.      * Example: if the list is orginially
  115.      *   ["ape", "dog", "xantus", "zebra", "cat", "yak"]
  116.      * after this method is called it should be
  117.      *   ["xantus", "zebra", "yak", "ape", "dog", "cat"]
  118.      */
  119.     public void xyzToFront()
  120.     {
  121.         int insertAt = 0;
  122.  
  123.         // TODO:  Move any word that starts with x, y, or z to the front of the ArrayList, but otherwise preserves the order
  124.         for (int c = 1; c < list.size(); c++)
  125.         {
  126.             if (list.get(c).startsWith("x") || list.get(c).startsWith("y")  || list.get(c).startsWith("z") )
  127.                 {
  128.                  list.set(0, list.remove(c));  
  129.                 }
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * gets the string representation of this array list
  135.      * @returns the string representation of this array list in
  136.      * standard collection format
  137.      */
  138.     public String toString()
  139.     {
  140.         return list.toString();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement