早教吧 育儿知识 作业答案 考试题库 百科 知识分享

Java作业关于复数的懂得帮帮吧,完成一定加分2.\x05实验要求(1)编写一个复数(z=x+iy)类Complex,其中包括成员变量x(复数的实部:int型)、y(复数的虚部:int型)和方法如下:\x05构造函数C

题目详情
Java作业 关于复数的 懂得帮帮吧,完成一定加分
2.\x05实验要求
(1)编写一个复数(z=x+iy)类Complex,其中包括成员变量x (复数的实部:int型)、y (复数的虚部:int型)和方法如下:
\x05构造函数 Complex(),设置x=0,y=0;构造函数:Complex(int i,int j)
\x05显示复数的方法:showComp()将其显示为如:5+8i或5-8i 的形式;
\x05求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Complex C1,Complex C2)
\x05求两个复数的差的方法:public Complex subComp(Complex C1,Complex C2)
\x05求两个复数的乘积的方法:public Complex multiComp(Complex C1,Complex C2)
\x05比较两个复数是否相等的方法:public boolean equalComp(Complex C1,Complex C2)
(2)编写一个主类NumberComplex,在其main方法中,定义并实例化两个复数对象,输出这两个复数对象的值,然后进行复数的加法、减法、乘法、是否相等运算,输出其运算结果(对象)的值
▼优质解答
答案和解析
public class Complex {
\x05private int entry;//实部
\x05private int visual;//虚部
\x05public Complex() {
\x05\x05super();
\x05}
\x05public Complex(int en,int vi) {
\x05\x05super();
\x05\x05this.setEntry(en);
\x05\x05this.setVisual(vi);
\x05}
\x05public int getEntry() {
\x05\x05return entry;
\x05}
\x05public void setEntry(int entry) {
\x05\x05this.entry = entry;
\x05}
\x05public int getVisual() {
\x05\x05return visual;
\x05}
\x05public void setVisual(int visual) {
\x05\x05this.visual = visual;
\x05}
\x05//显示:
\x05public void showComp(){
\x05\x05System.out.println(this.getEntry()+"+"+this.getVisual()+"i");
\x05}
\x05//加法:
\x05public Complex addComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry + c2.entry;
\x05\x05int v = c1.visual + c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//减法:
\x05public Complex subComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry - c2.entry;
\x05\x05int v = c1.visual - c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//乘法:
\x05public Complex multiComp(Complex c1,Complex c2){
\x05\x05int e = c1.entry*c2.entry - c1.visual*c2.visual;
\x05\x05int v = c1.visual*c2.entry + c1.entry*c2.visual;
\x05\x05Complex c = new Complex(e,v);
\x05\x05return c;
\x05}
\x05//相同返回true,不同返回false
\x05public boolean equalComp(Complex c1,Complex c2){
\x05\x05\x05return c1.entry==c2.entry && c1.visual == c2.visual;
\x05}
}
//测试类
public class Test {
\x05public static void main(String [] args){
\x05\x05//验证默认构造初始化:实部是0,虚部也是0
\x05\x05Complex c = new Complex();
\x05\x05System.out.print("默认构造结果是:");
\x05\x05System.out.println(c.getEntry());
\x05\x05System.out.println(c.getVisual());
\x05\x05//加法:
\x05\x05Complex c1 = new Complex(3,7);
\x05\x05Complex c2 = new Complex(5,6);
\x05\x05Complex cj = new Complex();
\x05\x05cj = c.addComp(c1,c2);
\x05\x05System.out.print("加法结果是:");
\x05\x05cj.showComp();
\x05\x05//减法:
\x05\x05Complex c3 = new Complex();
\x05\x05Complex c4 = new Complex();
\x05\x05Complex cm = new Complex();
\x05\x05cm = c.subComp(c1,c2);
\x05\x05System.out.print("减法结果是:");
\x05\x05cm.showComp();
\x05\x05//乘法:
\x05\x05Complex c5 = new Complex();
\x05\x05Complex c6 = new Complex();
\x05\x05Complex cc = new Complex();
\x05\x05cc = c.multiComp(c1,c2);
\x05\x05System.out.print("乘法结果是:");
\x05\x05cc.showComp();
\x05\x05//相等比较:
\x05\x05Complex c7 = new Complex(3,-3);
\x05\x05Complex c8 = new Complex(3,4);\x05\x05
\x05\x05Complex c9 = new Complex(3,4);\x05\x05
\x05\x05boolean resuls1 = c.equalComp(c7,c8);
\x05\x05boolean resuls2 = c.equalComp(c8,c9);
\x05\x05System.out.print("比较结果是:");
\x05\x05System.out.print(resuls1?true:false);
\x05\x05System.out.print(resuls2?true:false);
\x05}
}