CPSC 170 Lab 10: Maps

The linked list class you created for the last two labs can efficiently access the two ends of the list. However, there is no way to access the rest of the data without removing elements. Adding index access, like the array list, does not make sense because the linked list does not have index access to its data. Instead the linked list needs to be iterated, similar to the scanner class with methods for next and hasNext.

Iteration

The file LinkedList.java contains the linked list class from the last two labs. Modify this class so that it implements Iterable and the iterate method. The iterate method must return an instantiation of an object that implements Iterator. Create a sub-class of the LinkedList class that implements Iterator. The class will need a Node object to represent the current state of the iterator. It will also need to define the methods:

Write a program to test the linked list iterator before moving on.

Wireframe

Linked lists are useful in programs that do not need index access. For example, a program that performs operations on every element in a list, like a rendering program. A rendering program converts a mathematical model of an object into an image of the object. A simple way to mathematically represent a surface is with triangles. The file Triangles.zip contains four text files with triangle representations of surfaces. The file Wireframe.java contains the outline of a program that can render a list of triangles by drawing all of the triangle edges. The program reads the triangles listed in a text file and stores them in a linked list of vertices. The vertex sub-class contains member data for the x, y, and z coordinates of a corner of a triangle. Each of the series of three vertices in the linked list is a triangle. Complete the program by doing the following:

  1. In the paintComponent method, add a loop that uses the linked list iterator to draw every triangle. To draw a triangle, draw a line between each of the pairs of vertices of the triangle, ignoring the z coordinate. The triangle vertices range from -1 to 1, so the x and y coordinates of each line must be scaled to fit in window. Test your program before proceeding.
  2. In the keyPressed method, add a loop that uses the linked list iterator to rotate all of the triangles by a small amount. To rotate a triangle, compute new x, y, and z coordinates using the equation:

    xRotated = x cos θ + z sin θ
    yRotated = y
    zRotated = -x sin θ + z cos θ

Submission

Submit a zip file of your code on the course Inquire site that uses your last names as the file name.