⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module6.lst

📁 Mcgraw-Hill - Java 2 - A Beginner S Guide, 2Nd Ed - 2003 -prog.
💻 LST
📖 第 1 页 / 共 2 页
字号:
 
  MyClass(double d) { 
    System.out.println("Inside MyClass(double)."); 
    x = (int) d; 
  } 
 
  MyClass(int i, int j) { 
    System.out.println("Inside MyClass(int, int)."); 
    x = i * j; 
  }    
}    
    
class OverloadConsDemo {    
  public static void main(String args[]) {    
    MyClass t1 = new MyClass();  
    MyClass t2 = new MyClass(88);  
    MyClass t3 = new MyClass(17.23);  
    MyClass t4 = new MyClass(2, 4);  
  
    System.out.println("t1.x: " + t1.x); 
    System.out.println("t2.x: " + t2.x); 
    System.out.println("t3.x: " + t3.x); 
    System.out.println("t4.x: " + t4.x); 
  }    
}

listing 13
// Initialize one object with another. 
class Summation { 
  int sum; 
 
  // Construct from an int. 
  Summation(int num) { 
    sum = 0; 
    for(int i=1; i <= num; i++) 
      sum += i; 
  } 
 
  // Construct from another object. 
  Summation(Summation ob) { 
    sum = ob.sum; 
  } 
} 
 
class SumDemo { 
  public static void main(String args[]) { 
    Summation s1 = new Summation(5); 
    Summation s2 = new Summation(s1); 
 
    System.out.println("s1.sum: " + s1.sum); 
    System.out.println("s2.sum: " + s2.sum); 
  } 
}

listing 14
// A queue class for characters.   
class Queue {   
  private char q[]; // this array holds the queue   
  private int putloc, getloc; // the put and get indices   
   
  // Construct an empty Queue given its size.  
  Queue(int size) {   
    q = new char[size+1]; // allocate memory for queue   
    putloc = getloc = 0;   
  }   
  
  // Construct a Queue from a Queue.  
  Queue(Queue ob) {  
    putloc = ob.putloc;  
    getloc = ob.getloc;  
    q = new char[ob.q.length];  
  
    // copy elements  
    for(int i=getloc+1; i <= putloc; i++)  
      q[i] = ob.q[i];  
  }  
  
  // Construct a Queue with initial values.  
  Queue(char a[]) {  
    putloc = 0;  
    getloc = 0;  
    q = new char[a.length+1];  
  
    for(int i = 0; i < a.length; i++) put(a[i]);  
  }  
      
  // Put a characer into the queue.   
  void put(char ch) {   
    if(putloc==q.length-1) {   
      System.out.println(" -- Queue is full.");   
      return;   
    }   
       
    putloc++;   
    q[putloc] = ch;   
  }   
   
  // Get a character from the queue.  
  char get() {   
    if(getloc == putloc) {   
      System.out.println(" -- Queue is empty.");   
      return (char) 0;    
    }   
     
    getloc++;   
    return q[getloc];   
  }   
}   
   
// Demonstrate the Queue class.   
class QDemo2 {   
  public static void main(String args[]) {   
    // construct 10-element empty queue  
    Queue q1 = new Queue(10);   
  
    char name[] = {'T', 'o', 'm'};   
    // construct queue from array  
    Queue q2 = new Queue(name);   
  
    char ch;   
    int i;   
   
    // put some characters into q1   
    for(i=0; i < 10; i++)   
      q1.put((char) ('A' + i));   
  
    // construct queue from another queue  
    Queue q3 = new Queue(q1);  
  
    // Show the queues.  
    System.out.print("Contents of q1: ");   
    for(i=0; i < 10; i++) {    
      ch = q1.get();   
      System.out.print(ch);   
    }   
   
    System.out.println("\n");   
   
    System.out.print("Contents of q2: ");   
    for(i=0; i < 3; i++) {    
      ch = q2.get();   
      System.out.print(ch);   
    }   
   
    System.out.println("\n");   
   
    System.out.print("Contents of q3: ");   
    for(i=0; i < 10; i++) {    
      ch = q3.get();   
      System.out.print(ch);   
    }   
  }   
}

listing 15
// A simple example of recursion.  
class Factorial {  
  // This is a recursive function.  
  int factR(int n) {  
    int result;  
  
    if(n==1) return 1;  
    result = factR(n-1) * n;  
    return result;  
  }  
  
  // This is an iterative equivalent.  
  int factI(int n) {  
    int t, result;  
  
    result = 1;  
    for(t=1; t <= n; t++) result *= t;  
    return result;  
}  
}  
  
class Recursion {  
  public static void main(String args[]) {  
    Factorial f = new Factorial();  
  
    System.out.println("Factorials using recursive method.");  
    System.out.println("Factorial of 3 is " + f.factR(3));  
    System.out.println("Factorial of 4 is " + f.factR(4));  
    System.out.println("Factorial of 5 is " + f.factR(5));  
    System.out.println();  
 
    System.out.println("Factorials using iterative method.");  
    System.out.println("Factorial of 3 is " + f.factI(3));  
    System.out.println("Factorial of 4 is " + f.factI(4));  
    System.out.println("Factorial of 5 is " + f.factI(5));  
  }  
}

listing 16
// Use a static variable. 
class StaticDemo { 
  int x; // a normal instance variable 
  static int y; // a static variable 
} 
 
class SDemo { 
  public static void main(String args[]) { 
    StaticDemo ob1 = new StaticDemo(); 
    StaticDemo ob2 = new StaticDemo(); 
 
    /* Each object has its own copy of 
       an instance variable. */ 
    ob1.x = 10; 
    ob2.x = 20; 
    System.out.println("Of course, ob1.x and ob2.x " + 
                       "are independent."); 
    System.out.println("ob1.x: " + ob1.x + 
                       "\nob2.x: " + ob2.x); 
    System.out.println(); 
 
    /* Each object shares one copy of 
       a static variable. */ 
    System.out.println("The static variable y is shared."); 
    ob1.y = 19; 
    System.out.println("ob1.y: " + ob1.y + 
                       "\nob2.y: " + ob2.y); 
    System.out.println(); 
 
    System.out.println("The static variable y can be" + 
                       " accessed through its class."); 
    StaticDemo.y = 11; // Can refer to y through class name 
    System.out.println("StaticDemo.y: " + StaticDemo.y + 
                       "\nob1.y: " + ob1.y + 
                       "\nob2.y: " + ob2.y); 
  } 
}

listing 17
// Use a static method. 
class StaticMeth { 
  static int val = 1024; // a static variable 
 
  // a static method 
  static int valDiv2() { 
    return val/2; 
  } 
} 
 
class SDemo2 { 
  public static void main(String args[]) { 
 
    System.out.println("val is " + StaticMeth.val); 
    System.out.println("StaticMeth.valDiv2(): " + 
                       StaticMeth.valDiv2()); 
 
    StaticMeth.val = 4; 
    System.out.println("val is " + StaticMeth.val); 
    System.out.println("StaticMeth.valDiv2(): " + 
                       StaticMeth.valDiv2()); 
 
  } 
}

listing 18
class StaticError { 
  int denom = 3; // a normal instance variable 
  static int val = 1024; // a static variable 
 
  /* Error! Can't access a non-static variable 
     from within a static method. */ 
  static int valDivDenom() { 
    return val/denom; // won't compile! 
  } 
}

listing 19
// Use a static block 
class StaticBlock { 
  static double rootOf2; 
  static double rootOf3; 
 
  static { 
    System.out.println("Inside static block."); 
    rootOf2 = Math.sqrt(2.0); 
    rootOf3 = Math.sqrt(3.0); 
  } 
 
  StaticBlock(String msg) { 
    System.out.println(msg); 
  } 
} 
 
class SDemo3 { 
  public static void main(String args[]) { 
    StaticBlock ob = new StaticBlock("Inside Constructor"); 
 
    System.out.println("Square root of 2 is " +  
                       StaticBlock.rootOf2); 
    System.out.println("Square root of 3 is " +  
                       StaticBlock.rootOf3); 
 
  } 
}

listing 20
// A simple version of the Quicksort. 
class Quicksort { 
 
  // Set up a call to the actual Quicksort method. 
  static void qsort(char items[]) { 
    qs(items, 0, items.length-1); 
  } 
 
  // A recursive version of Quicksort for characters. 
  private static void qs(char items[], int left, int right)  
  {  
    int i, j;  
    char x, y;  
  
    i = left; j = right;  
    x = items[(left+right)/2];  
  
    do {  
      while((items[i] < x) && (i < right)) i++;  
      while((x < items[j]) && (j > left)) j--;  
  
      if(i <= j) {  
        y = items[i];  
        items[i] = items[j];  
        items[j] = y;  
        i++; j--;  
      }  
    } while(i <= j);  
  
    if(left < j) qs(items, left, j);  
    if(i < right) qs(items, i, right);  
  } 
} 
 
class QSDemo { 
  public static void main(String args[]) { 
    char a[] = { 'd', 'x', 'a', 'r', 'p', 'j', 'i' }; 
    int i; 
 
    System.out.print("Original array: "); 
    for(i=0; i < a.length; i++)  
      System.out.print(a[i]); 
 
    System.out.println(); 
 
    // now, sort the array 
    Quicksort.qsort(a); 
     
    System.out.print("Sorted array: "); 
    for(i=0; i < a.length; i++)  
      System.out.print(a[i]); 
  } 
}

listing 21
// Use an inner class. 
class Outer {  
  int nums[]; 
 
  Outer(int n[]) { 
    nums = n; 
  } 
  
  void Analyze() {  
    Inner inOb = new Inner();  
 
    System.out.println("Minimum: " + inOb.min());  
    System.out.println("Maximum: " + inOb.max());  
    System.out.println("Average: " + inOb.avg());  
  }  
  
  // This is an innner class.  
  class Inner {  
    int min() { 
      int m = nums[0]; 
      for(int i=1; i < nums.length; i++)  
        if(nums[i] < m) m = nums[i]; 
 
      return m; 
    } 
             
    int max() { 
      int m = nums[0]; 
      for(int i=1; i < nums.length; i++)  
        if(nums[i] > m) m = nums[i]; 
 
      return m; 
    } 
             
    int avg() { 
      int a = 0; 
      for(int i=0; i < nums.length; i++)  
        a += nums[i]; 
 
      return a / nums.length; 
    } 
  }  
}  
  
class NestedClassDemo {  
  public static void main(String args[]) {  
    int x[] = { 3, 2, 1, 5, 6, 9, 7, 8 }; 
    Outer outOb = new Outer(x);  
 
    outOb.Analyze(); 
  }  
}

listing 22
// Use ShowBits as a local class.  
class LocalClassDemo {  
  public static void main(String args[]) {  
 
    // An inner class version of ShowBits. 
    class ShowBits {  
      int numbits;  
    
      ShowBits(int n) {  
        numbits = n;  
      }  
  
      void show(long val) {  
        long mask = 1;  
  
        // left-shit a 1 into the proper position  
        mask <<= numbits-1;  
   
        int spacer = 0;  
        for(; mask != 0; mask >>>= 1) {  
          if((val & mask) != 0) System.out.print("1");  
          else System.out.print("0");  
          spacer++;  
          if((spacer % 8) == 0) {  
            System.out.print(" ");  
            spacer = 0;  
          }  
        }  
        System.out.println();  
      }  
    }  
  
     
    for(byte b = 0; b < 10; b++) { 
      ShowBits byteval = new ShowBits(8);  
 
      System.out.print(b + " in binary: "); 
      byteval.show(b);  
    } 
  }  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -