Commit 78943b4a authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

1-1

0 → 100755
+21 −0
Changes for 1-1: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
/**
 * This program computes the factorial of a number
 */
public class Factorial {                   // Define a class
  public static void main(String[] args) { // The program starts here
    int input = Integer.parseInt(args[0]); // Get the user's input
    double result = factorial(input);      // Compute the factorial
    System.out.println(result);            // Print out the result
  }                                        // The main() method ends here

  public static double factorial(int x) {  // This method computes x!
    if (x < 0)                             // Check for bad input
      return 0.0;                          //   if bad, return 0
    double fact = 1.0;                     // Begin with an initial value
    while(x > 1) {                         // Loop until x equals 1
      fact = fact * x;                     //   multiply by x each time
      x = x - 1;                           //   and then decrement x
    }                                      // Jump back to the star of loop
    return fact;                           // Return the result
  }                                        // factorial() ends here
}                                          // The class ends here

3-1

0 → 100755
+20 −0
Changes for 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; 
  }
}

3-10

0 → 100755
+19 −0
Changes for 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();
}

3-11

0 → 100755
+14 −0
Changes for 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; = head;
    { 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
}

3-2

0 → 100755
+11 −0
Changes for 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