COSC 123 - Computer Creativity
Lab 8: Lists and Arrays


In this lab, we will practice lists and arrays in Java. The tutorials in the lab will be presented by the TA step-by-step. However, you are free to work at your own pace on the lab. The lab assignment questions will not be presented by the TA.

Tutorial #1

This tutorial will demonstrate how to create and use an array.

Step #1: Set Up the Tutorial

Create a new project called Lab8 and a new class inside the project called TestArray with a main method.

Step #2: Create an Array

An array reference refers to a region in memory that holds the array elements. The array may store base types like int or double or object references to objects. An array is created using new. We will create an array of five numbers which are put into the array using a loop.

Step #3: Finding an Element in an Array

We can find a number in an array by searching through each element one at a time. The code below searches for the number 30.

Step #4: Adding/Removing Elements in an Array

Adding and removing elements in an array is a little harder because we may need to shift elements up and down in the array depending on where the item was added or removed. When adding an element, we also need to have enough space in the array. The example code below removes element 2 and shifts all elements after it down one. Then it adds 50 at index 1 and shifts all elements up one. Arrays are fast to get/set values at spots, but adding/removing are not very fast. The complete code is TestArray.java.

Tutorial #2

In this tutorial, we will use the ArrayList class which implements a resizable array. It is also has methods that makes finding, adding, and removing elements easier than if you did it yourself using an array.

Step #1: Set Up the Tutorial

Create a new class inside the project called TestArrayList with a main method.

Step #2: Creating an ArrayList

An ArrayList is a class, so we create an instance using new. This calls the ArrayList constructor. If we provide no parameters (default constructor), an ArrayList of size 10 is created by default. Otherwise, we can pass a number indicating the initial size of our ArrayList. The code below creates an ArrayList of size 5 and puts 5 numbers into it.

Step #3: Finding an Element in an ArrayList

To find a value in an ArrayList, we could use a loop to search like an array, but there is also a method called indexOf. It will look in the ArrayList for us and return the index of the object we searched for if it is there, otherwise it will return -1

Step #4: Adding/Removing Elements in an Array

Adding and removing elements is much easier with an ArrayList as it will shift elements automatically. The code below use the add method in two ways. The way with no parameters adds 60 to the end of the list. The second way provides the index to insert at (2) and the value (100). Removing is also easy by using the remove method. The get method retrieves a value at an index (used to print the contents), set method changes the value at an index, and the size method returns how many elements are in the list. The complete code is TestArrayList.java.

Lab Question #1

Starter code: Asteroids.java and Bullet.java. For the other classes (GameObject.java, Ship.java, Asteroid.java) use your answer from lab #7.Tasks:

The output should look like this:

Ship: Location: (200.0,200.0) Speed: (0.0,0.0) Size: (30.0,30.0) Acceleration: (0.0,0.0) Num.Lives: 5

Asteroids:
Location: (100.0,100.0) Speed: (10.0,10.0) Size: (20.0,20.0) Type: 1
Location: (200.0,200.0) Speed: (20.0,20.0) Size: (40.0,40.0) Type: 2
Location: (300.0,300.0) Speed: (30.0,30.0) Size: (60.0,60.0) Type: 3

Bullets:
Location: (400.0,400.0) Speed: (40.0,40.0) Size: (5.0,5.0)
Location: (500.0,500.0) Speed: (50.0,50.0) Size: (5.0,5.0)

Submission Instructions and Marking

For this lab assignment, each group should submit using Connect the Java file Asteroids.java.

Grading (10 Marks Total)

Question #1 (10 marks)

  1. +2 - add asteroids and bullet ArrayLists and ship instance variables to Asteroids class
  2. +1 - add start method to Asteroids class
  3. +1 - start method should create a ship instance (200,200,0,0,30,30,0,0,5) and print it out
  4. +2 - start method should create three asteroids, put each in the ArrayList, then print the asteroids in the ArrayList
  5. +2 - start method should create two bullets, put each in the ArrayList, then print the bullets in the ArrayList
  6. +1 - add default constructor to Asteroids that creates asteroids and bullets ArrayLists
  7. +1 - create an Asteroids object in the main method and call the method start

*Home