CSSE 220: Object Oriented Software Development
Arrays and ArrayLists

You will do this exercise by yourself, but be quick to ask questions of your instructor, student assistants and classmates as desired.

Goals and Examples

Practice and hence solidify your understanding of using arrays and ArrayLists:

  1. Declaring an array or ArrayList. Examples:
        double[] data;
        Employee[] employees;
    
        ArrayList scores;
        ArrayList students;
    
  2. Allocating space for an array or ArrayList. Examples (assuming declarations above):
        data = new double[50];
        employees = new Employee[this.numberOfEmployees];
    
        scores = new ArrayList();
        students = new ArrayList();
    
        // You also can declare and allocate space in a single statement:
        double[] moreData = new double[100];
        ArrayList moreStudents = new ArrayList();
    
  3. Getting (accessing) elements in an array or ArrayList. Examples (assuming declarations above, and also see examples to the right):
        ... data[4] ...
        ... employees[k] ...
    
        ... scores.get(4) ...
        ... students.get(k) ...
    
  4. Setting (mutating) elements in an array or ArrayList. Examples (assuming declarations above, and also see examples to the right):
        data[4] = valueFromFile;
        employees[k] = new Employee(...);
    
        scores.set(4, scoreFromFile);
        students.set(k, new Student(...)) ...
    
        // Also can add elements to the end of an ArrayList, and more:
        students.add(new Student(...));
        students.remove(j); // Remove element at index j,
                            // shifting the elements behind it
    

You can see more examples of arrays and ArrayLists in the slides on arrays and ArrayLists.

More examples

  1. Initializing all elements of an array or ArrayList, using old-style loops:
        Dog[] dogs = new Dog[numberOfDogs];
    
        for (int k = 0; k < dogs.length; ++k) {
            dogs[k] = new Dog(...);
        }
    
        ArrayList cats = new ArrayList();
    
        for (int k = 0; k < numberOfCats; ++k) {
            cats.add(new Cat(...));
        }
    
  2. Counting array or ArrayList elements that satisfy a given property, using old-style loops:
        int count = 0;
        for (int k = 0; k < dogs.length; ++k) {
            if (... dogs[k] ...) {
                ++ count;
            }
        }
    
        int otherCount = 0;
        for (int k = 0; k < cats.length; ++k) {
            if (... cats.get(k) ...) {
                ++ otherCount;
            }
        }
    
  3. Summing array or ArrayList elements, using NEW-style loops:
        double totalWeight = 0;
        for (Dog dog : dogs) {
            totalWeight += dog.weight();
        }
    
        int totalAge = 0;
        for (Cat cat : cats) {
            totalAge += cat.age();
        }
    
    Note: you can NOT use the enhanced for loop to modify   an element of an array or ArrayList.
  4. The “histogram loop pattern”:

    1. Declare an array or ArrayList that will hold histogram values 0 .. n, where events are numbered and n is the biggest event that can occur.
    2. Initialize all elements of the histogram to 0.
    3. Repeatedly:
      • Get one event (call it m) from which the histogram is to be built.
      • Increment the histogram count at index m.
    The array or ArrayList now holds the desired histogram.

Get the project

Get the ArraysAndArrayLists project, as follows:

  1. Unzip this ArraysAndArrayLists.zip file, putting it into your JavaProjects folder.
  2. In Eclipse, choose File ~ Import and import the unzipped ArraysAndArrayLists project.
  3. Add your ArraysAndArrayLists project to your individual repository for this course.

The Exercise - Overview

You will implement parts of a program that simulates an election, by filling in the parts of it marked TODO, in the order detailed below. Before starting, your instructor will:

    Instructions

  1. Recall the public methods of the State class. With that in mind, implement the getElectoralVotes   method.
  2. Recall the simulate   method of the State class and what it returns. With that in mind, implement the runSimulation   (singular — NOT runSimulations) method. Use the “new” for loop style, just for practice.
  3. Implement the runSimulations method, using the two helper methods that you just wrote and recalling the “histogram loop pattern” (in examples above).
  4. Implement the TODO's in ElectionSimulatorMain.
  5. Make sure that your project has correct style.
  6. Your project should already have proper documentation (per the documentation with which it arrived), except:
  7. Commit your ArraysAndArrayLists project to your individual repository.