对象的运算
设计复数类
- 数值与类型
数集的拓展:
整数类型:int,long,short……
实数类型:float,double……
- 编写Java的复数类的改进
1.必要改进,实部/虚部为0不显示
2.虚部为1,“1”不显示
3.虚部为负,(3+ -5i)→(3- 5i)
package com.one;
public class ComplexNumber {
private double real,imag;
public ComplexNumber(double a,double b)
{
real = a;
imag = b;
}
public void Show()
{
System.out.printf("%.2f+%.2fi\n",real,imag);
}
public static void main(String args[])
{
ComplexNumber d;
d = new ComplexNumber(1.0,5.0);
d.Show();
}
}
public double Module()
{
double t = real * real + imag * imag;
return(Math.sqrt(t));
}
System.out.pritnf("%f",d.Module());