| CSC 207 | Grinnell College | Fall, 2018 |
| Algorithms and Object-Oriented Design | ||
This lab reinforces properties of Java's inner classes.
Much of this lab comes from materials by Jerod Weinman.
In preparation for this lab, read Weiss' discussion of inner classes, sections 15.1-15.2, pages 574-580.
Programs for this lab may be found in this innerclasses directory.
Consider the following code:
Exercise1.java
public class Exercise1 {
private int i = 1;
private static int j = 42;
static class Inner {
private int x = i + j;
}
}
x legal? Why or why not?
Inner non-static?
static? static?
Exercise1.this.i somewhere in Inner?
static similar to
other places you have seen it used (e.g. variables and methods)?
Confirm your explanation with a neighbor.
static modifier to class
Inner. Say we added the following method at line 9.
private int getX() { return x; }
Consider the following code:
Exercise2.java
package innerclasses;
public class Exercise2 {
private int i;
private static int j = 42;
public int fiddle() {
Inner1 obj = new Inner1(); // Default constructor
i = Inner1.x;
return i + j;
}
private static class Inner1 {
private static int x = 207;
}
}
i = Inner1.x;Consider the Vector3d and its inner class:
package innerclasses; import java.util.Iterator; import java.util.NoSuchElementException; /** Simple three-dimensional vector that is iterable over the dimensions. * * @author Jerod Weinman */ public class Vector3Dimplements Iterable { private AnyType i,j,k; // Three components of the vector /** Construct a vector of the three specified components * * @param i first component of the vector * @param j second component of the vector * @param k third component of the vector */ public Vector3D(AnyType i, AnyType j, AnyType k) { this.i = i; this.j = j; this.k = k; } /** * Return an iterator over the components of this vector. * * @return iterator over the components of this vector */ public Iterator iterator() { return new VectorIterator (); } /** * Iterator over the components of this vector. */ private class VectorIterator implements Iterator { private int currentComponent = 0; /** Determine whether there are more elements in this iterator. * * @return true if there are more elements */ public boolean hasNext() { return false; // YOUR CODE HERE } /** * Return the next element of the vector. * * @return the next element of the vector * @throws NoSuchElementException when {@link hasNext} is false*/ public AnyType next() { return Vector3D.this.k; // Return an arbitrary component } /** * Unsupported operation to remove an item from a vector of a * fixed-dimension. * * @throws UnsupportedOperationException in any case */ public void remove() { throw new UnsupportedOperationException (); } } /* VectorIterator */ /** Driver program for the vector and its iterator */ public static void main(String args[]) { // Create a vector of powers of two Vector3Dvec = new Vector3D (4,8,16); // Get the iterator Iterator iter = vec.iterator(); for (int component : vec) // Iterate and print the components System.out.println(component); } }
Vector3D.java. What happens? Try to come up
with an explanation.
private class VectorIterator<AnyType2> implements
Iterator<AnyType2> {
next() to resolve this
problem.
private class VectorIterator implements Iterator<AnyType> {
and restore the return type of next() to AnyType.
VectorIterator the
way we first tried to? Be prepared to reflect on this.
hasNext and next routines
of VectorIterator.
main method of Vector3D.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/207.sp012/labs/lab-inner-classes.shtml
|
created 12 January 2009 by Jerod Weinman revsied 18 January 2011 by Jerod Weinman revised 9-15 March 2012 by Henry M. Wallker |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |