1

好的,所以对于学校来说,我目前必须制作一个可以处理有理数算术的程序。我已经完成了这部分代码,但任务的第二部分是创建两个自定义异常类。第一个异常类很容易创建,只是一个空对象处理类。但我无法弄清楚的类是自定义除以零类。问题是我们有特定的类参数,它们是:

Class - DivideByZeroException
Constructors - DivideByZeroException(), DivideByZeroException (Rational rational)
Instance Fields - Rational rational 
Public Methods - Rational getRational() - String getMessage() : override

我遇到的问题是,我不知道为什么当类必须扩展 RunTimeException 从而启用 super(message) 调用时有必要覆盖 getMessage。这是可以抛出异常的类和应该处理它们的类的源代码。我可能有一些我目前正在尝试的奇怪代码,所以如果有些东西没有意义,那可能是我感到沮丧。

package com.math.rational;

public class DivideByZeroException extends RuntimeException
{
    private Rational rational;

    DivideByZeroException()
    {
        super();
    }

    DivideByZeroException(Rational rational)
    {
        super();
        this.rational = rational;
    }

    public Rational getRational()
    {
        try
        {
        return this.rational;
        }
        catch(NullPointerException e)
        {
            e.printStackTrace();
            System.out.println("You have no rational object to retrieve");
            System.exit(1);
            return null;
        }
    }

    @Override
    public String getMessage()
    {
        if(this.rational != null)
        {
            return this.rational.toString();
        }
        else
        {
            return new String("You attempted to divide by zero");
        }
    }
}


package com.math.rational;

public class Rational
{
    private int numerator;
    private int denominator;

    public Rational(int numerator, int denominator)
    {

        try
        {
            this.numerator = numerator;
            this.denominator = denominator;

            int[] intHolder = simplify(this.numerator, this.denominator);
            this.numerator = intHolder[0];
            this.denominator = intHolder[1];

            if ( this.denominator == 0 || denominator == 0)
            {
                System.out.println("true");
                throw new DivideByZeroException(new Rational(this.numerator, this.denominator));
            }   
        }

        catch (DivideByZeroException e)
        {
            System.out.println(e.getMessage());
        }   
    }

    public Rational(Rational numerator, Rational denominator)
    {
        try
        {
        numerator.multiply(denominator.reciprocalNumber());

        if ( numerator.denominator == 0 || denominator.denominator == 0)
        {
            System.out.println("true");
            throw new DivideByZeroException(new Rational(this.numerator, this.denominator));
        }
        }
        catch(DivideByZeroException e)
        {
            System.out.println(e.getMessage());
        }
    }

    public int[] simplify(int tempNumerator, int tempDenominator)
    {
        int reduce = greatestCommon(getAbs(tempNumerator), tempDenominator);

        tempNumerator /= reduce;
        tempDenominator /= reduce;
        int[] size = {tempNumerator, tempDenominator};

        for(int i : size)
        {
            System.out.printf("%s ", i);
        }
        return size;
    }

    public int greatestCommon(int first, int second)
    {
        while(first != second)
        {
            if(first > second)
            {
                first -= second;
            }
            else
            {
                second -= first;
            }
        }

        return first;
    }

    public Rational reciprocalNumber()
    {
        return new Rational(this.denominator, this.numerator);
    }

    public int getAbs(int value)
    {
        return  Math.abs(value);
    }

    public Rational add(Rational other)
    {

        if(other.getDenominator() != this.getDenominator())
        {
            int commonDenom = this.getDenominator() * other.getDenominator();
            int firstNumerator = numerator * other.getDenominator();
            int secondNumerator = other.getNumerator() * this.getDenominator();
            int finalNumerator = firstNumerator + secondNumerator;
            return new Rational(finalNumerator, commonDenom);
        }
        else if(other.getDenominator() == this.getDenominator())
        {
            int finalNumerator = this.getNumerator() + other.getNumerator();
            return new Rational(finalNumerator, this.getDenominator());
        }
        else
        {
            System.out.println("LINE 90 EXECUTED WHICH MEANS SOMETHING IS WRONG");
            return new Rational(this.getNumerator(), this.getDenominator());
        }

    }

    public Rational subtract(Rational other)
    {
        return add(new Rational(-other.numerator, other.denominator));
    }

    public Rational divide(Rational other)
    {
        return multiply(other.reciprocalNumber());
    }

    public Rational multiply(Rational other)
    {
        int newNumerator = this.getNumerator() * other.getNumerator();
        int newDenominator = this.getDenominator() * other.getDenominator();
        return new Rational(newNumerator, newDenominator);
    }

    public int getNumerator()
    {
        return this.numerator;
    }

    public void setNumerator(int numerator)
    {
        this.numerator = numerator;
    }

    public int getDenominator()
    {
        return this.denominator;
    }

    public void setDenominator(int denominator)
    {
        this.denominator = denominator;
    }

    @Override
    public String toString()
    {
        String answer;

        if(denominator == 1)
        {
            answer = numerator + "";
        }
        else
        {
            answer = numerator + "/" + denominator;
        }
        return answer;
    }

}

唯一的其他类实际上只是一个用于测试值的驱动程序类。

4

1 回答 1

0

定义自定义运行时异常的示例:

public static int divide(x,y) {
  double result;
  if (y==0) {
     throw new CustomException("Cannot divide by 0");
  }
  else {
     result = x/y;
  }
     return result;
}

public class CustomException extends RuntimeException {
   public CustomException (String errorMsg) {
   super(errorMsg);
}
于 2013-10-18T10:07:36.807 回答