楼主: Lisrelchen
3244 15

[读者文库]Java Examples in a Nutshell, 3rd Edition [推广有奖]

11
Lisrelchen 发表于 2015-9-27 00:28:32
  1. Testing the Rect Class

  2. Example 2-2 is a standalone program named RectTest that puts the Rect class of Example 2-1 through its paces. Note the use of the new keyword and the Rect( ) constructor to create new Rect objects. The program uses the . operator to invoke methods of the Rect objects and to access their fields. The test program also relies implicitly on the toString( ) method of Rect when it uses the string concatenation operator (+) to create strings to be displayed to the user.

  3. Example 2-2. RectTest.java
  4. package je3.classes;

  5. /** This class demonstrates how you might use the Rect class */
  6. public class RectTest {
  7.     public static void main(String[  ] args) {
  8.         Rect r1 = new Rect(1, 1, 4, 4);    // Create Rect objects
  9.         Rect r2 = new Rect(2, 3, 5, 6);
  10.         Rect u = r1.union(r2);             // Invoke Rect methods
  11.         Rect i = r2.intersection(r1);
  12.         
  13.         if (u.isInside(r2.x1, r2.y1))   // Use Rect fields and invoke a method
  14.             System.out.println("(" + r2.x1 + "," + r2.y1 +
  15.                                ") is inside the union");
  16.         
  17.         // These lines implicitly call the Rect.toString( ) method
  18.         System.out.println(r1 + " union " + r2 + " = " + u);
  19.         System.out.println(r1 + " intersect " + r2 + " = " + i);
  20.     }
  21. }
复制代码

12
Lisrelchen 发表于 2015-9-27 00:29:27
  1. A Rect Subclass

  2. Example 2-3 is a simple subclass of the Rect class of Example 2-1. This DrawableRect class inherits the fields and methods of Rect and adds its own method, draw( ), that draws a rectangle using a specified java.awt.Graphics object. (We’ll see more of the Graphics object in Chapter 12.) DrawableRect also defines a constructor that simply uses the super keyword to pass its arguments up to the corresponding Rect constructor. Note the use of the extends keyword to indicate that Rect is the superclass of DrawableRect.

  3. Example 2-3. DrawableRect.java
  4. package je3.classes;
  5. /**
  6. * This is a subclass of Rect that allows itself to be drawn on a screen.
  7. * It inherits all the fields and methods of Rect
  8. * It relies on the java.awt.Graphics object to perform the drawing.
  9. **/
  10. public class DrawableRect extends Rect {
  11.     /** The DrawableRect constructor just invokes the Rect( ) constructor */
  12.     public DrawableRect(int x1, int y1, int x2, int y2) { super(x1,y1,x2,y2); }
  13.    
  14.     /** This is the new method defined by DrawableRect */
  15.     public void draw(java.awt.Graphics g) {
  16.         g.drawRect(x1, y1, (x2 - x1), (y2-y1));
  17.     }
  18. }
复制代码

13
Lisrelchen 发表于 2015-9-27 00:30:29
  1. Example 2-4. ColoredRect.java
  2. package je3.classes;
  3. import java.awt.*;

  4. /**
  5. * This class subclasses DrawableRect and adds colors to the rectangle it draws
  6. **/
  7. public class ColoredRect extends DrawableRect {
  8.     // These are new fields defined by this class.
  9.     // x1, y1, x2, and y2 are inherited from our super-superclass, Rect.
  10.     protected Color border, fill;
  11.    
  12.     /**
  13.      * This constructor uses super( ) to invoke the superclass constructor, and
  14.      * also does some initialization of its own.
  15.      **/
  16.     public ColoredRect(int x1, int y1, int x2, int y2,
  17.                        Color border, Color fill)
  18.     {
  19.         super(x1, y1, x2, y2);
  20.         this.border = border;
  21.         this.fill = fill;
  22.     }
  23.    
  24.     /**
  25.      * This method overrides the draw( ) method of our superclass so that it
  26.      * can make use of the colors that have been specified.
  27.      **/
  28.     public void draw(Graphics g) {
  29.         g.setColor(fill);
  30.         g.fillRect(x1, y1, (x2-x1), (y2-y1));
  31.         g.setColor(border);
  32.         g.drawRect(x1, y1, (x2-x1), (y2-y1));
  33.     }
  34. }
复制代码

14
Lisrelchen 发表于 2015-9-27 00:31:41
  1. Example 2-5. ComplexNumber.java
  2. package je3.classes;

  3. /**
  4. * This class represents complex numbers, and defines methods for performing
  5. * arithmetic on complex numbers.
  6. **/
  7. public class ComplexNumber {
  8.     // These are the instance variables.  Each ComplexNumber object holds
  9.     // two double values, known as x and y.  They are private, so they are
  10.     // not accessible from outside this class.  Instead, they are available
  11.     // through the real( ) and imaginary( ) methods below.
  12.     private double x, y;

  13.     /** This is the constructor.  It initializes the x and y variables */
  14.     public ComplexNumber(double real, double imaginary) {
  15.         this.x = real;
  16.         this.y = imaginary;
  17.     }

  18.     /**
  19.      * An accessor method.  Returns the real part of the complex number.
  20.      * Note that there is no setReal( ) method to set the real part.  This means
  21.      * that the ComplexNumber class is "immutable".
  22.      **/
  23.     public double real( ) { return x; }
  24.    
  25.     /** An accessor method. Returns the imaginary part of the complex number */
  26.     public double imaginary( ) { return y; }
  27.    
  28.     /** Compute the magnitude of a complex number */
  29.     public double magnitude( ) { return Math.sqrt(x*x + y*y); }

  30.     /**
  31.      * This method converts a ComplexNumber to a string.  This is a method of
  32.      * Object that we override so that complex numbers can be meaningfully
  33.      * converted to strings, and so they can conveniently be printed out with
  34.      * System.out.println( ) and related methods
  35.      **/
  36.     public String toString( ) { return "{" + x + "," + y + "}"; }
  37.    
  38.     /**
  39.      * This is a static class method.  It takes two complex numbers, adds
  40.      * them, and returns the result as a third number.  Because it is static,
  41.      * there is no "current instance" or "this" object.  Use it like this:
  42.      * ComplexNumber c = ComplexNumber.add(a, b);
  43.      **/
  44.     public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
  45.         return new ComplexNumber(a.x + b.x, a.y + b.y);
  46.     }

  47.     /**
  48.      * This is a non-static instance method by the same name.  It adds the
  49.      * specified complex number to the current complex number.  Use it like
  50.      * this:
  51.      * ComplexNumber c = a.add(b);
  52.      **/
  53.     public ComplexNumber add(ComplexNumber a) {
  54.         return new ComplexNumber(this.x + a.x, this.y+a.y);
  55.     }

  56.     /** A static class method to multiply complex numbers */
  57.     public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {
  58.         return new ComplexNumber(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x);
  59.     }

  60.     /** An instance method to multiply complex numbers */
  61.     public ComplexNumber multiply(ComplexNumber a) {
  62.         return new ComplexNumber(x*a.x - y*a.y, x*a.y + y*a.x);
  63.     }
  64. }
复制代码

15
Lisrelchen 发表于 2015-9-27 00:33:07
  1. Example 2-6. Averager.java
  2. package je3.classes;
  3. /**
  4. * A class to compute the running average of numbers passed to it
  5. **/
  6. public class Averager {
  7.     // Private fields to hold the current state.
  8.     private int n = 0;
  9.     private double sum = 0.0, sumOfSquares = 0.0;

  10.     /**
  11.      * This method adds a new datum into the average.
  12.      **/
  13.     public void addDatum(double x) {
  14.         n++;
  15.         sum += x;
  16.         sumOfSquares += x * x;
  17.     }
  18.    
  19.     /** This method returns the average of all numbers passed to addDatum( ) */
  20.     public double getAverage( ) { return sum / n; }

  21.     /** This method returns the standard deviation of the data */
  22.     public double getStandardDeviation( ) {
  23.           return Math.sqrt(((sumOfSquares - sum*sum/n)/n));
  24.     }

  25.     /** This method returns the number of numbers passed to addDatum( ) */
  26.     public double getNum( ) { return n; }

  27.     /** This method returns the sum of all numbers passed to addDatum( ) */
  28.     public double getSum( ) { return sum; }

  29.     /** This method returns the sum of the squares of all numbers. */
  30.     public double getSumOfSquares( ) { return sumOfSquares; }

  31.     /** This method resets the Averager object to begin from scratch */
  32.     public void reset( ) { n = 0; sum = 0.0; sumOfSquares = 0.0; }

  33.     /**
  34.      * This nested class is a simple test program we can use to check that
  35.      * our code works okay.
  36.      **/
  37.     public static class Test {
  38.         public static void main(String args[  ]) {
  39.             Averager a = new Averager( );
  40.             for(int i = 1; i <= 100; i++) a.addDatum(i);
  41.             System.out.println("Average: " + a.getAverage( ));
  42.             System.out.println("Standard Deviation: " +
  43.                                a.getStandardDeviation( ));
  44.             System.out.println("N: " + a.getNum( ));
  45.             System.out.println("Sum: " + a.getSum( ));
  46.             System.out.println("Sum of squares: " + a.getSumOfSquares( ));
  47.         }
  48.     }
  49. }
复制代码

16
Lisrelchen 发表于 2016-5-8 04:55:54

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-30 19:21