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.
This tutorial will demonstrate how to create and use an array.
Create a new project called Lab8 and a new class inside the project called TestArray with a main method.
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.
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.
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.
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.
Create a new class inside the project called TestArrayList with a main method.
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.
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
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.
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)
For this lab assignment, each group should submit using Connect the Java file Asteroids.java.