Tina Comston
Comp-111 Week 9
Franklin University
Week 9
Preparation
    • Read Chapter 6 (sections 6.9 - 6.10) and Chapter 7 (sections 7.1 and 7.7) in the COMP 111 Reading Guide.
    Guided Learning Activity
    Assignment
    • Problem 1
    Arrays

    Up to this point we have been working with variables that contain just one value.  If we needed more than one of these variables, we would duplicate the variable and perhaps add a number onto the end.

    For example, if we had 25 students, we might use:
    String stu1;
    String stu2;
    String stu3;
    etc. through String stu25

    This process works OK, but it become tedious.  You have to try and determine how many variables you would need - how many students have the potential to be in the class?  You also have to write code for each and every variable.

    if (stu1 != null)
    {
        System.out.println(stu1);
    }

    You'd have to repeat the above code 25 times, once for each of the variables stu1 through stu25.  This can make a program very long, can introduce errors as code is repeated and is challenging to modify as you'd have to make 25 modifications.

    That's where arrays come in.  Rather than defining 25 different variables, instead you define just one variable and indicate to the computer that it is to be repeated 25 times.

    String stuName[] = new String[25];
    This says that we have a variable by the name of stuName that is an array of size 25 and each element in the array will contain a String object.

    Here's the format for defining an array.

    dataType arrayName[] = new dataType[length];
    • dataType - this can be either a primitive data type or a class
    • arrayName - the name you are giving to the array
    • [] - this directly follows the data type and indicates that it is an array
    • new - new object instance
    • dataType - same as the initial dataType
    • [length]  - the number of elements the array will contain

    To access an element of the array you would use:
        arrayName[elementPosition]

    If you wanted to retrieve a value from the array you would use:
        String tmpName = stuName[7]; - to retrieve the value at element position 7

    If you wanted to change an element value in the array you would use:
        stuName[7] = tmpName; - to change the value at element position 7

    It is important to note that the elements in an array are numbered from 0 to the length - 1.  If you try to access an element position that is less than 0 or greater than the size-1, you will be outside the boundary of the array and a logic error will result.

    The FOR loop is commonly used with arrays.

    for (int i = 0; i < stuName.length; i++)
    {
        if (stuName[i] != null)
            System.out.println(stuName[i]);
    }

    The above code will access the elements in the array, one at a time, printing out each element.  .length is an attribute of the array.  It indicates how many elements are available in the array.

    YouTube video on arrays https://www.youtube.com/watch?v=o4VzblFTwII
      • Copy the code for this problem into a BlueJ class.
      • Compile.
      • Execute.
      • Review the output.
      • Change the FOR statement as indicated.
      • Compile.
      • Execute.
      • Review the output.
    • Problem 2
    ArrayLists

    While arrays are a definite improvement over having individual variables, they do have limitations.
    • The size is static, meaning that once you define the size that is the size of the array.  It cannot be changed.   Defining an array that is too large will take up more memory than is needed.  Defining an array that is too small will require you to manually create a larger array and move everything over to this new array.
    • You cannot add something into the middle of the array easily.  You would need to first move everything over 1 position to the right to make room and then insert into the middle of the array.
    • You cannot remove something from the middle of the array easily.  You would have to shift everything over 1 position to the left and overwrite the element that is to be removed.
    On the plus side - arrays do:
    • allow direct access to elements which is very efficient
    • Can hold primitive or object data

    Because of these limitations, the developers of JAVA came up with the ArrayList object.  The benefits of an ArrayList is:
    • The initial size is 0 and increases as each element is added to the ArrayList - it grows dynamically.
    • Items can be added to the beginning, middle, or end.
    • Items can be removed from the beginning, middle, or end.
    On the down side - ArrayLists:
    • Can only hold objects - no primitive data
    • Methods be used to access element, which is less efficient than an array.
    Before an ArrayList can be used, you must import the Java ArrayList library.  The import statements goes up at the very top of your program, before any of your code.

    import Java.Util.ArrayList;

    The format for declaring an ArrayList is:

    ArrayList <dataType> arrListName = new ArrayList <dataType> ();

    Because the ArrayList is an object, a constructor must be called to create the ArrayList object.  That is what you are seeing above, with a slight twist.  The data type of the elements in the ArrayList is specified within < >.
    • ArrayList - the class of the object that is being created.
    • <dataType> - the data type of the objects that will be stored in the elements
    • arrListName - the name you give the Arraylist
    • new - creating a new object instance
    • ArrayList - the name of the constructor, same as the class name
    • <dataType> - repeating the previous value
    • () - no explicit parameters are being passed to the constructor, empty or default constructor
    For example:
    ArrayList <String> stuName = new ArrayList <String> ();

    The methods that are available for ArrayList objects are:
    • .get(pos) - retrieves the element at the specified position
    • .add(obj) - adds the object to the end of the ArrayList
    • .add(pos, obj) - adds the object at the indicated position, shifting everything from that position over one
    • .remove(pos) - removes the element at the specified position, shifting all elements behind that position up one
    • .set(pos, obj) - replaces the element at the specified position with the object
    • .size() - returns the size or number of elements in the ArrayList
    The FOR loop is commonly used with ArrayLists.

    for (int i = 0; i < stuName.size(); i++)
    {
            System.out.println(stuName.get(i));
    }

    ***We don't need to check to see if the element is null because an ArrayList only contains elements we have added.  It will not contain empty elements.

    YouTube video on ArrayLists https://www.youtube.com/watch?v=e7U-wWdsp78
      • The code sections given in this problem contain syntax errors.  Identify the errors.
        • Hint - copy the code into BlueJ and compile. 
    • Problem 3
      • The code sections given in this problem contain logic errors.  Identify the errors.
        • Hint - copy the code into BlueJ, compile, and execute.

    • Problem 4
      • You will be writing a test class, class, and driver class for a catalog program.
    Reflection Paper
    • Continue journaling each week for your reflection paper.
    Lab 3
    •  Although you have a couple of weeks to complete this lab, it's never a good idea to wait until the last minute.  Read through the instructions. Make sure you have a clear understanding of what is needed.  
    • ****This is typically the most challenging of the labs.