Commit 033d08f7 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9780596002831

0 → 120000
+1 −0
Changes for 9780596002831: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9780596002831
 No newline at end of file

JavaNut4Examples/3-1

0 → 100644
+20 −0
Changes for JavaNut4Examples/3-1: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
public class Circle {
  // A class field
  public static final double PI= 3.14159;     // A useful constant

  // A class method: just compute a value based on the arguments
  public static double radiansToDegrees(double rads) { 
    return rads * 180 / PI; 
  }

  // An instance field
  public double r;                  // The radius of the circle

  // Two instance methods: they operate on the instance fields of an object
  public double area() {            // Compute the area of the circle
    return PI * r * r; 
  }
  public double circumference() {   // Compute the circumference of the circle
    return 2 * PI * r; 
  }
}

JavaNut4Examples/3-10

0 → 100644
+19 −0
Changes for JavaNut4Examples/3-10: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
// This method creates and returns an Enumeration object
public java.util.Enumeration enumerate() {

  // Here's the definition of Enumerator as a local class
  class Enumerator implements java.util.Enumeration {
    Linkable current;
    public Enumerator() { current = head; }
    public boolean hasMoreElements() {  return (current != null); }
    public Object nextElement() {
      if (current == null) throw new java.util.NoSuchElementException();
      Object value = current;
      current = current.getNext();
      return value;
    }
  }

  // Now return an instance of the Enumerator class defined directly above
  return new Enumerator();
}

JavaNut4Examples/3-11

0 → 100644
+14 −0
Changes for JavaNut4Examples/3-11: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
public java.util.Enumeration enumerate() {
  // The anonymous class is defined as part of the return statement
  return new java.util.Enumeration() { 
    Linkable current;
    { current = head; }  // Replace constructor with an instance initializer
    public boolean hasMoreElements() {  return (current != null); }
    public Object nextElement() {
      if (current == null) throw new java.util.NoSuchElementException();
      Object value = current;
      current = current.getNext();
      return value;
    }
  };  // Note the required semicolon: it terminates the return statement
}

JavaNut4Examples/3-2

0 → 100644
+11 −0
Changes for JavaNut4Examples/3-2: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
public class Circle {
    public static final double PI = 3.14159;  // A constant
    public double r;   // An instance field that holds the radius of the circle

    // The constructor method: initialize the radius field
    public Circle(double r) { this.r = r; }   

    // The instance methods: compute values based on the radius
    public double circumference() { return 2 * PI * r; }
    public double area() { return PI * r*r; }
}
Loading