Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSC 207 Lab on Inner Classes in Java CSC 207 Grinnell College Spring, 2012   Algorithms and Object-Oriented Design   Inner Classes in Java Summary This lab reinforces properties of Java's inner classes. Acknowledgment Much of this lab comes from materials by Jerod Weinman. Preparation 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. Exercises Exercise 1 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; } } Is the declaration of x legal? Why or why not? Compile the code to verify your prediction. What if we made class Inner non-static? Compile the code to verify your prediction. What do we call such classes when they are declared non-static? static? Which type would allow a reference to Exercise1.this.i somewhere in Inner? What is the main difference between the objects of these two types of classes? How is this use of the static similar to other places you have seen it used (e.g. variables and methods)? Confirm your explanation with a neighbor. Restore the static modifier to class Inner. Say we added the following method at line 9. private int getX() { return x; } Is this a legal declaration? Does the method need to be static? Why or why not? Add the method and compile to verify your prediction. If your hypothesis was incorrect, figure out why. Confirm with the instructor or a neighbor. Exercise 2 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; } } Is the access of the private member of the private nested class i = Inner1.x; legal? Compile the program to test your prediction. What do you suppose is the reason for this behavior? Exercise 3 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 Vector3D implements 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 Vector3D vec = 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); } } Upon inspection, do you expect the code to compile? Compile Vector3D.java. What happens? Try to come up with an explanation. Change line 36 to the following: private class VectorIterator implements Iterator { Now recompile the code. What happens? Why? Modify the return type of next() to resolve this problem. Recompile again. What is different from the first time you compiled (something subtle)? Does this make marginally more sense? Now change line 18 to the following: private class VectorIterator implements Iterator { and restore the return type of next() to AnyType. Recompile once again. Now what happens? Why can't we re-parameterize VectorIterator the way we first tried to? Be prepared to reflect on this. Exercise 4 Complete the hasNext and next routines of VectorIterator. Test your implementation using the program in the main method of Vector3D. Exercise5 Do Exercise 15.6 in Weiss. Verify your answer with a brief test implementation. Why do you suppose inner classes cannot have static fields? 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. Copyright © 2011-2012 Henry M. Walker. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .