public class Fraction {
private int numerator; //
private int denominator; //
Fraction(int nume, int denom)
{
numerator = nume;
denominator = denom;
}
public Fraction divide(Fraction other ) throws FractionDivideByZeroException
{
int nume=0, denom=0;
try {
nume = (this.numerator * other.denominator);
denom = (this.denominator*other.numerator);
if(nume!=0 && denom==0) throw new FractionDivideByZeroException();
return new Fraction(nume, denom);
} catch (FractionDivideByZeroException e) {
e.printError();
}
return new Fraction(nume, denom);
}
}
class FractionDivideByZeroException extends Exception
{
void printError()
{
System.out.println("You can not divide by zero!");
}
}
这是测试类:
public class FractionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Fraction frac1 = new Fraction(1,2);
Fraction frac2 = new Fraction(1,3);
Fraction frac3 = frac1.divide(frac2); // here's the error message saying "Unhandled exception type FractionDivideByZeroException"
System.out.println(frac3);
}
}
答案 0 :(得分:2)
从你的分裂方法中取出投掷声明。你扔它然后在那里处理它。
你的FractionTest使用frac1.divide,编译器要求你处理在divide方法中声明的异常。
答案 1 :(得分:1)
你说Divide抛出FractionDivideByZeroException
public Fraction divide(Fraction other ) throws FractionDivideByZeroException
但编写代码以确保它没有。编译器不知道这一点,所以抱怨你没有处理调用代码中的异常
答案 2 :(得分:1)
查看您的方法签名:
public Fraction divide(Fraction other ) throws FractionDivideByZeroException
它说它抛出异常。 main方法没有捕获它,因此编译器会抱怨。
在方法中捕获它或声明你抛出它,但不是两者。
我认为你的逻辑存在缺陷。你应该永远能够创建一个零分母的分数 - 你的构造函数应该检查它。
你的divide()方法应该检查以确保除数的分子不为零。这是获得零除错误的唯一方法。
当你构造分割返回的新分数时,构造函数应抛出异常。
不要在你的divide()方法中捕获它;离开throws子句并删除try / catch。如果它是一个经过检查的异常,那么你的测试用例必须抓住它。
以下是我写的方式:
package fraction;
public class Fraction implements Comparable
{
private int numerator;
private int denominator;
public Fraction()
{
this(0);
}
public Fraction(int numerator)
{
this(numerator,1);
}
Fraction(int numerator, int denominator)
{
if (denominator == 0)
throw new IllegalArgumentException("denominator cannot be zero");
this.numerator = numerator;
this.denominator = denominator;
if (this.numerator*this.denominator < 0)
{
this.numerator = -Math.abs(this.numerator);
this.denominator = Math.abs(this.denominator);
}
this.normalize();
}
public Fraction add(Fraction other)
{
return new Fraction(this.numerator*other.denominator+other.numerator*this.denominator, this.denominator*other.denominator);
}
public Fraction sub(Fraction other)
{
return new Fraction(this.numerator*other.denominator-other.numerator*this.denominator, this.denominator*other.denominator);
}
public Fraction mul(Fraction other)
{
return new Fraction(this.numerator*other.numerator, this.denominator*other.denominator);
}
public Fraction div(Fraction other)
{
return new Fraction(this.numerator*other.denominator, this.denominator*other.numerator);
}
@Override
public boolean equals(Object o)
{
if (this == o)
{
return true;
}
if (o == null || getClass() != o.getClass())
{
return false;
}
Fraction fraction = (Fraction) o;
if (denominator != fraction.denominator)
{
return false;
}
if (numerator != fraction.numerator)
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = numerator;
result = 31 * result + denominator;
return result;
}
public int compareTo(Object o)
{
Fraction other = (Fraction) o;
int product1 = this.numerator*other.denominator;
int product2 = other.numerator*this.denominator;
return (product1-product2);
}
@Override
public String toString()
{
return numerator + "/" + denominator;
}
private void normalize()
{
int sign = 1;
if (this.numerator < 0)
{
sign = -1;
}
int gcd = greatestCommonDivisor(Math.abs(this.numerator), Math.abs(this.denominator));
this.numerator /= gcd;
this.denominator /= gcd;
this.numerator *= sign;
}
public static int greatestCommonDivisor(int m, int n)
{
int a = Math.max(m, n);
int b = Math.min(m, n);
if (a == 0)
return b;
if (b == 0)
return a;
while (a != b)
{
if (b > a)
b -= a;
else
a -= b;
}
return b;
}
}
部分单元测试:
package fraction;
import org.junit.Assert;
import org.junit.Test;
public class FractionTest
{
@Test
public void testAdd()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(11, 8);
Assert.assertEquals(expected, x.add(y));
}
@Test
public void testSub()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(1, 8);
Assert.assertEquals(expected, x.sub(y));
}
@Test
public void testMul()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(15, 32);
Assert.assertEquals(expected, x.mul(y));
}
@Test
public void testDiv()
{
Fraction x = new Fraction(3, 4);
Fraction y = new Fraction(5, 8);
Fraction expected = new Fraction(6, 5);
Assert.assertEquals(expected, x.div(y));
}
@Test
public void testGreatestCommonDivisor()
{
Assert.assertEquals(Fraction.greatestCommonDivisor(48, 180), 12);
Assert.assertEquals(Fraction.greatestCommonDivisor(40902, 24140), 34);
Assert.assertEquals(Fraction.greatestCommonDivisor(2, 199), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(11, 8), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(1, 8), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(15, 32), 1);
Assert.assertEquals(Fraction.greatestCommonDivisor(6, 5), 1);
}
}
答案 3 :(得分:0)
您声明devide
是一种抛出FractionDivideByZeroException
的方法
你的测试功能必须抓住它
或者...你的函数不会抛出此异常,因此throws FractionDivideByZeroException
是多余的。
答案 4 :(得分:0)
将try / catch块添加到main方法中。 FractionDevidedByZeroException
是已检查的例外。你应该用try / catch包围它们。否则会出现此编译错误。
public static void main(String[] args) {
// TODO Auto-generated method stub
Fraction frac1 = new Fraction(1,2);
Fraction frac2 = new Fraction(1,3);
try {
Fraction frac3 = frac1.divide(frac2);
System.out.println(frac3);
} catch (FractionDevidedByZeroException e) {
e.printStackTrace();
}
}