Exception in thread "main"
JAVA新手,写了一个计算复数加减乘除的程序,没有语法错误,却出现了运行错误,找不出相应的错误,请指教!
以下是代码:
class Complex
{
// a为实部,b为虚部
private double a;
private double b;
public Complex()
{
}
public Complex(double a, double b)
{
this.a = a;
this.b = b;
}
// 设置相关属性
public void setA(double a)
{
this.a = a;
}
public void setB(double b)
{
this.b = b;
}
// 得到相关属性
public double getA()
{
return a;
}
public double getB()
{
return b;
}
// 重写Object类的toString()方法
public String toString()
{
return "(" + a + " + " + b + "i)";
}
}
public class ComplexTest
{
private static Complex c;
public static Complex add(Complex c1, Complex c2)
{
c = null;
c.setA(c1.getA() + c2.getA());
c.setB(c1.getB() + c2.getB());
return c;
}
public static Complex subtract(Complex c1, Complex c2)
{
c = null;
c.setA(c1.getA() - c2.getA());
c.setB(c1.getB() - c2.getB());
return c;
}
public static Complex multiply(Complex c1, Complex c2)
{
c = null;
c.setA(c1.getA() * c2.getA() - c1.getB() * c2.getB());
c.setB(c1.getB() * c2.getA() + c1.getA() * c2.getB());
return c;
}
public static Complex divide(Complex c1, Complex c2)
{
c = null;
c.setA((c1.getA() * c2.getA() + c1.getB() * c2.getB()) /
(c2.getA() * c2.getA() + c2.getB() * c2.getB()));
c.setB((c1.getB() * c2.getA() - c1.getA() * c2.getB()) /
(c2.getA() * c2.getA() + c2.getB() * c2.getB()));
return c;
}
public static void main(String args[])
{
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(3, 4);
System.out.println(add(c1, c2));
System.out.println(subtract(c1, c2));
System.out.println(multiply(c1, c2));
System.out.println(divide(c1, c2));
}
}