Advertisement
Dragon105801

Finalized and Commented Assignment

Mar 19th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.03 KB | None | 0 0
  1. /**
  2.  * Creates an object that will allow one to store and manipulate an array of strings,
  3.  *      without having to write many of the low-level array-manipulation routines. A
  4.  *      StringsList object stores 0 or more String objects, with empty locations, if
  5.  *      any at the end of the list.
  6.  *
  7.  * @version 3/19/17
  8.  */
  9. public class StringsList
  10. {
  11.     /**
  12.      * Instance Variables
  13.      */
  14.     //size is the current amount of elements in the list.
  15.     private int size = 0;
  16.     //capacity is the max total size of allowed spaces.
  17.     private int capacity = 0;
  18.     //creates an empty String Array.
  19.     private String[] stringArray;
  20.  
  21.     /**
  22.      * Constructor for objects of class StringsList
  23.      * Instantiates an empty StringsList with maximum capacity of 10 that will
  24.      *      store strings(String values).
  25.      * Preconditions: None.
  26.      */
  27.     public StringsList()
  28.     {
  29.         //Initialise instance variables
  30.         //Initialise this.size to 0.
  31.         this.size = 0;
  32.         //Initialise this.capacity to 10 *Cannot be changed by user*
  33.         this.capacity = 10;
  34.         //Initialise this.stringArray to have a max size of value capacity.
  35.         this.stringArray = new String[capacity];
  36.     }
  37.  
  38.     /**
  39.      * Getter method to returns number of elements currently stored in the list.
  40.      * Preconditions: None.
  41.      */
  42.     public int size()
  43.     {
  44.         //Returns number of elements currently stored in the list.
  45.         return this.size;
  46.     }
  47.  
  48.     /**
  49.      * Appends String 'a' at end of the list.
  50.      * Postcondition: List has 'a' appended, size is updated accordingly.
  51.      */
  52.     public void add(String a)
  53.     {
  54.         //Checks for the size of object to be less than the length of the stringArray.
  55.         if(this.size < this.stringArray.length)
  56.         {
  57.             //If this.size is less than this.stringArray
  58.             //Check if this.size is less than this.capacity.
  59.             if(this.size < this.capacity)
  60.             {
  61.                 //Store 'a' into stringArray at position equal to this.size.
  62.                 this.stringArray[this.size()] = a;
  63.                 //Increment the objects size by 1.
  64.                 this.size++;
  65.             }
  66.             else
  67.             {
  68.                 //If this.size is not less than this.capacity print that the list is full
  69.                 System.out.println("List is full");
  70.             }
  71.         }
  72.         else
  73.         {
  74.             //If this.size is not less than the length of the String Array enlarge the capacity
  75.             //by twice the current size.
  76.             this.enlarge();
  77.             //Once you increase the capacity perform the add method again.
  78.             //*User should not be aware that the capacity is being increased*
  79.             this.add(a);
  80.         }
  81.     }
  82.    
  83.     /**
  84.      * Inserts String 'a' in the specific user-numbered location in the list
  85.      *      (The first element is at location 1 to the user)
  86.      *      If there is already an item at position location, that element and all other
  87.      *      elements following it are pushed one position over(to the next highest index)
  88.      *      in the list. If the list is alredy full, at capacity, then the array size is
  89.      *      doubled using the enlarge method.
  90.      * Precondition: Location is in range of 1 to size+1
  91.      * Postcondition: Contents modified as necessary and size is updated accordingly.
  92.      */
  93.     public void insert(String a, int location)
  94.     {
  95.         //Check for this.size to be less than this.capacity
  96.         if(this.size < this.capacity)
  97.         {
  98.             //Check for location to be less than or equal to this.size
  99.             //and that the String 'a' is not of null type.
  100.             if(location <= this.size && a != null)
  101.             {
  102.                 //Check for this.stringArray at index location-1 to be equal to null type.
  103.                 if(this.stringArray[location-1] == null)
  104.                 {
  105.                     //Stores String 'a' into this.stringArray at index location-1.
  106.                     this.stringArray[location-1] = a;
  107.                     //Increment this.size by 1.
  108.                     this.size++;
  109.                 }
  110.                 else
  111.                 {
  112.                     /*If this.stringArray at index location-1 does not equal null
  113.                     go through a for loop that initializes i to this.capacity-1
  114.                     and iterates through the loop location-1 times and then
  115.                     decrements i by 1 after each iteration.*/
  116.                     for(int i = this.capacity-1; i >= location-1; i--)
  117.                     {
  118.                         //Shifts elements by 1.
  119.                         this.stringArray[i] = this.stringArray[i-1];
  120.                     }
  121.                     //Stores String 'a' into stringArray at index location-1.
  122.                     this.stringArray[location-1] = a;
  123.                     //Increments this.size by 1.
  124.                     this.size++;
  125.                 }
  126.             }
  127.             else
  128.             {
  129.                 //If the check fails enlarge the capacity
  130.                 this.enlarge();
  131.                 //Then rerun the insert method with the same parameters.
  132.                 this.insert(a, location);
  133.             }
  134.         }
  135.     }
  136.    
  137.     /**
  138.      * Returns true if String 'a' is in the list, otherwise it will return false.
  139.      */
  140.     public boolean contains(String a)
  141.     {
  142.         //Check for String 'a' not being of null type.
  143.         if (a != null)
  144.         {
  145.             //For loop that starts at 0 then iterates through the length of the stringArray
  146.             //and then increments i by 1 each time.
  147.             for(int i = 0; i < this.stringArray.length; i++)
  148.             {
  149.                 //Check for stringArray at index i not being null
  150.                 //and that stringArray at index i equals String 'a'.
  151.                 if (this.stringArray[i] != null && this.stringArray[i].equals(a))
  152.                 {
  153.                     return true;
  154.                 }
  155.             }
  156.         }
  157.         return false;
  158.     }
  159.    
  160.     /**
  161.      * Returns the index (a value from 1 to size) of 'a' in the list, or -1 if 'a' is not in the list
  162.      */
  163.     public int find(String a)
  164.     {
  165.         //For loop that starts at 0 and iterates through this.size times and increments i by 1.
  166.         for(int i = 0; i < this.size; i++)
  167.         {
  168.             //Check for stringArray at index i being equal to the String 'a'
  169.             //and that stringArray does not equal null type.
  170.             if(this.stringArray[i].equals(a) && this.stringArray != null)
  171.             {
  172.                 //Return i+1 for the index of the position if check is true.
  173.                 return i+1;
  174.             }
  175.         }
  176.         //Returns -1 if the String 'a' is not found.
  177.         return -1;
  178.     }
  179.    
  180.     /**
  181.      * Prints the contents of the StringsList for every location within the capacity of the list,
  182.      *      showing the location of each element. For those locations where no String is stored,
  183.      *      print null as the value.
  184.      */
  185.     public String displayList()
  186.     {
  187.         //The Format for the display of the String.
  188.         //States the capacity and the size of the current StringList.
  189.         String stringDisplay = "StringList: capacity " + this.capacity + ", size " + this.size + "\n";
  190.         //For loop that starts at 0 and runs through the length of the stringArray
  191.         //and increments i by 1.
  192.         for(int i = 0; i < this.stringArray.length; i++)
  193.         {
  194.             //Displays the position of the element and the element itself.
  195.             stringDisplay += "[" + (i+1) + "]" + this.stringArray[i] + "\n";
  196.         }
  197.         //Returns the String to print the result.
  198.         return stringDisplay;
  199.     }
  200.    
  201.     /**
  202.      * Removes the String 'a', if it is in the list. Any elements that follow 'a' in the list are
  203.      *      are moved over one position(to the next lowest index) so there are no holes in the list.
  204.      *      If 'a' is not in the list, the list is not changed.
  205.      * Postcondition: Contents modified as necessary and size is updated accordingly.
  206.      */
  207.     public void delete(String a)
  208.     {
  209.         //Creates an integer to store the location of String 'a' after running the find method on it.
  210.         int pos = this.find(a);
  211.         //If String 'a' is found goes into the if statement.
  212.         if(pos != -1)
  213.         {
  214.             //Creates a String labeled temporary.
  215.             String temporary;
  216.             for(int i = pos-1; i < this.size; i++)
  217.             {
  218.                 //Stores the String in stringArray at position i+1 into temporary.
  219.                 temporary = this.stringArray[i+1];
  220.                 //Stores the String in temporary into stringArray at position i.
  221.                 this.stringArray[i] = temporary;
  222.             }
  223.             //Sets last index position in array to null type.
  224.             this.stringArray[this.size] = null;
  225.             //Decrements this.size by 1.
  226.             this.size--;
  227.            
  228.         }
  229.     }
  230.    
  231.     /**
  232.      * Doubles the capacity of the list, whenever needed. Called by the insert method when
  233.      *      a user inserts an element into a full list. This is done by creating a new list
  234.      *      double the size and then copying the elements from the original into the new list.
  235.      * Precondition: None.
  236.      * Postcondition: Instance variables modified as necessary.
  237.      */
  238.     private void enlarge()
  239.     {
  240.         //Creates a temporary String that has the size of the current String object.
  241.         String[] temporary = new String[this.size];
  242.         //For loop that starts at 0 and iterates through the size of the current String object
  243.         //and increments i by 1 each iteration.
  244.         for(int i = 0; i < this.size; i++)
  245.         {
  246.             //Stores the element at position i in stringArray into the temporary String at the same
  247.             //index.
  248.             //Beginning of swap.
  249.             temporary[i] = this.stringArray[i];
  250.         }
  251.         //Once each element has been stored into the temporary String double the size of capacity.
  252.         this.capacity = this.capacity * 2;
  253.         //Create a new String with the new capacity size.
  254.         this.stringArray = new String[this.capacity];
  255.         //Loop through the temporary String and store everything back into the newly made StringArray
  256.         //with the larger capacity size.
  257.         for(int i = 0; i < this.size; i++)
  258.         {
  259.             //End of swap.
  260.             this.stringArray[i] = temporary[i];
  261.         }
  262.     }
  263.  
  264.     /*public static void main(String[] args)
  265.     {
  266.         StringsList myString = new StringsList();
  267.         System.out.println(myString.displayList());
  268.         myString.add("hello");
  269.         myString.add("tomorrow");
  270.         myString.add("cactus");
  271.         myString.add("world");
  272.         myString.add("a");
  273.         myString.add("goodbye");
  274.         System.out.println(myString.displayList());
  275.         System.out.println(myString.find("hello"));
  276.         System.out.println(myString.find("tomorrow"));
  277.         System.out.println(myString.find("cactus"));
  278.         System.out.println(myString.find("world"));
  279.         System.out.println(myString.find("a"));
  280.         System.out.println(myString.find("goodbye"));
  281.         System.out.println(myString.find("spaghetti"));
  282.         System.out.println(myString.contains("hello"));
  283.         System.out.println(myString.contains("tomorrow"));
  284.         System.out.println(myString.contains("cactus"));
  285.         System.out.println(myString.contains("world"));
  286.         System.out.println(myString.contains("a"));
  287.         System.out.println(myString.contains("goodbye"));
  288.         System.out.println(myString.contains("spaghetti"));
  289.         System.out.println(myString.displayList());
  290.         myString.add("stone");
  291.         myString.add("rock");
  292.         myString.add("84");
  293.         myString.add("tongue");
  294.         myString.add("eye");
  295.         myString.add("foot");
  296.         System.out.println(myString.displayList());
  297.         myString.delete("stone");
  298.         myString.delete("cactus");
  299.         System.out.println(myString.displayList());
  300.     }*/
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement