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
.
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:
hasNext
: returns a boolean if there are objects after
the current object in the linked listnext
: returns the current object and updates the
iterator's current object to be the next in the listremove
: throws an UnsupportedOperationExceptioinWrite a program to test the linked list iterator before moving on.
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:
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.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 θ
Submit a zip file of your code on the course Inquire site that uses your last names as the file name.