用java 编写一个复数类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用java 编写一个复数类相关的知识,希望对你有一定的参考价值。
2)完成以下的操作:初始化复数、求其绝对值、复数的加、减、乘、除、乘方、自加、自减等。
提示:
1)建立数据类、复数类
2)数据、复数信息的初始化
3)复数信息的输出
4)求复数的绝对值
5)实现复数的加、减、乘、除、乘方、自加、自减等运算
protected int a;
protected int b;
public Complex(int a, int b)
this.a = a;
this.b = b;
public String toString()
return this.a + " + " + this.b + "i";
public static Complex addition(Complex complex1, Complex complex2)
int a = complex1.a + complex2.a;
int b = complex1.b + complex2.b;
return new Complex(a, b);
public static Complex subtract(Complex complex1, Complex complex2)
int a = complex1.a - complex2.a;
int b = complex1.b - complex2.b;
return new Complex(a, b);
public static Complex multiplication(Complex complex1, Complex complex2)
int a = complex1.a * complex2.a - complex1.b * complex2.b;
int b = complex1.b * complex2.a + complex1.a * complex2.b;
return new Complex(a, b);
public static Complex division(Complex complex1, Complex complex2) throws Exception
if (complex2.a == 0)
throw new Exception("complex2.a is 0");
if (complex2.b == 0)
throw new Exception("complex2.b is 0");
int a = (complex1.a * complex2.a + complex1.b * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
int b = (complex1.b * complex2.a - complex1.a * complex2.b) / (complex2.a * complex2.a + complex2.b * complex2.b);
return new Complex(a, b);
//测试用的类
public class ComplexTest
public static void main(String[] args) throws Exception
// TODO Auto-generated method stub
Complex complex1 = new Complex(1, 2);
Complex complex2 = new Complex(3, 4);
System.out.println(complex1 + " + " + complex2 + " = " + Complex.addition(complex1, complex2));
System.out.println(complex1 + " - " + complex2 + " = " + Complex.subtract(complex1, complex2));
System.out.println(complex1 + " x " + complex2 + " = " + Complex.multiplication(complex1, complex2));
System.out.println(complex1 + " / " + complex2 + " = " + Complex.division(complex1, complex2));
追问
你这程序 包括以上的要求吗? 有没有乘方 复数的绝对值
追答只有加 减 乘 除, 其它的功能和这些类似。
追问能不能给我完整下 哥们 我们交作业 老师还问里 麻烦你了 。。。谢了。。。
追答//绝对值
public static double abs(Complex complex)
return (Math.sqrt(complex.a * complex.a + complex.b * complex.b));
//乘方
public static Complex cPow(Complex complex, int n)
double r, j;
r = Math.sqrt(complex.a * complex.a + complex.b * complex.b);
j = Math.atan(complex.b / complex.a);
double a = Math.pow(r, n) * Math.cos(n * j);
double b = Math.pow(r, n) * Math.sin(n * j);
return new Complex(a, b);
//自加
public static Complex addSelf(Complex complex)
return new Complex(++complex.a, ++complex.b);
//自减
public static Complex minSelf(Complex complex)
return new Complex(--complex.a, --complex.b);
以上方法都放到类Complex里面。还有把类Complex里面的int定义的变量全部改成double。
//测试用的类:
...
System.out.println(complex1 + " 绝对值: " + Complex.abs(complex1));
System.out.println(complex2 + " 绝对值: " + Complex.abs(complex2));
System.out.println(complex1 + " 乘方: " + Complex.cPow(complex1, 3));
System.out.println(complex1 + " 自加: " + Complex.addSelf(complex1));
System.out.println(complex2 + " 自减: " + Complex.minSelf(complex2));
以上是关于用java 编写一个复数类的主要内容,如果未能解决你的问题,请参考以下文章