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

📄 fraction.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.exceptions;

/** Class of objects thrown when a mathematical
  * exception is detected
  */
class MathException extends Exception {}

/** Class of objects thrown when division by
  * zero is detected
  */
class DivideByZeroException extends MathException {}

/** Class of errors thrown when a zero
  * denominator is detected in a fraction
  */
class ZeroDenominatorException extends MathException {}

/** A class to represent mathematical fractions
  */
public class Fraction {

   private int num;   // fraction numerator
   private int den;   // fraction denominator

   /** Constructor without arguments
     */
   public Fraction() {
      num = 0;
      den = 1;
   }

   /** Class constructor for whole numbers.
     * The denominator is set to 1.
     * @param initNum numerator value
     */
   public Fraction( int initNum ) {
      num = initNum;
      den = 1;
   }

   /** Class constructor
     * @param initNum numerator value
     * @param initDen denominator value
     * @exception ZeroDenominatorException
     *            if a zero denominator is
     *            specified
     */
   public Fraction( int initNum, int initDen ) 
         throws ZeroDenominatorException {
      if ( 0 == initDen ) {
         throw new ZeroDenominatorException();
      }
      num = initNum;
      den = initDen;
   }

   /** Divide one fraction by another
     * @param divisor The divisor fraction
     * @exception ZeroDenominatorException
     *            if a fraction with a zero
     *            denominator is created
     * @exception DivideByZeroException
     *            if the divisor is zero
     */
   Fraction divideBy( Fraction divisor ) 
               throws DivideByZeroException,
                      ZeroDenominatorException {
      if ( 0 == divisor.num ) { 
         throw new DivideByZeroException();
      }
      return new Fraction( num * divisor.den, 
                           den * divisor.num );
   }

   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      try {
         Fraction a = new Fraction( 1, 0 );
      }
      catch( MathException x ) {
         System.err.println( "A math error occurred" );
      }

      try {
         Fraction a = new Fraction( 37, 0 );
      }
      catch( ZeroDenominatorException x ) {
         System.err.println( "A zero denominator " +
                  "was detected" );
      }
      catch( MathException x ) {
         System.err.println( "A math error occurred" );
      }

      try {
         Fraction a = new Fraction( -7, 0 );
      }
      catch( MathException x ) {
         System.err.println( x );
      }

      try {
         Fraction a = new Fraction( -102, 6 ), 
              b = new Fraction( 0 );
         Fraction c = a.divideBy( b );
      }
      catch( MathException x ) {
         x.printStackTrace( System.err );
      }
   }
}

⌨️ 快捷键说明

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