资源描述:
《合肥学院JAVA期末考试》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、classComplexprivatedoublex,y;publicComplex(){}publicComplex(doublea,doubleb){x二a;y二b;}publicvoidshowComp(){System.out.print(x);if(y>=0)System,out.print("+"+y+"i");elseSystem・out.print(y+"i〃);}publicComplexaddComp(Complexa,Complexb){Complexcomp二newComp
2、lex();comp・x=a.x+b・x;comp,y二a.y+b.y;returncomp;}publicComplexsubComp(Complexa,Complexb){Complexcomp二newComplex();comp・x=a.x-b.x;comp.y二a.y-b・y;returncomp;}publicComplexmu1Comp(Complexa,Complexb){Complexcomp二newComplex();comp.x=a.x*b.x-比y*b.y;comp・y二a.
3、x*b・y+a・y*b.x;returncomp;}publicbooleanequalComp(Complexa,Complexb){if(a.x=b.x&&a.y=b.y)returntrue;elsereturnfalse;publicclassFushu{publicstaticvoidshow(Stringstr,Complexc){System.out.print(str+/z:“);c.showComp();System・out.println("〃);}publicstaticvo
4、idmain(String[]args){Complexcompl二newComplex(8,5);Complexcomp2=ncwComplex(4,-9);Complexcomp3二newComplex();booleanbool;show("compl",compl);show(z/comp2z/,comp2);comp3=comp3.addComp(compl,comp2);System,out.print("相加后“);show(,zcomp3,z,comp3);comp3=comp3.
5、subComp(compl,comp2);System,out.print(,z相减后");show(z,conip3,z,comp3);comp3=comp3.mu1Comp(compl,comp2);System,out.print(〃相乘后〃);show(〃comp3",comp3);bool二comp3.equalComp(compl,comp2);if(bool)System・out・printin(/zcompl-^comp2+冃等〃);elseSystem,out.println(〃
6、compl与comp2不相等〃);}importjava.awt.*;importjava.awt.event.*;publicclassCacuextendsFrameimplementsprivateButtonaddl=newButton("+〃);privateButtonsub=newButton;privateButtonplus二newButton(〃*〃);privateButtondiv=newButton(/z/z,);ActionListener{privateTextFie
7、ldtl—newTextField(12);privateTextFieldt2—newTextField(12);privateTextFieldt3二newTcxtField(12);privateLabelpl—newLabel(〃操作数〃);privateLabelp2二newLabelC操作数〃);privateLabelp3—newLabelC结果〃);privateclassWindowCloscrextendsWindowAdapter{publicvoidwindowClosin
8、g(WindowEventwe){System,exit(0);}}publicStringsi,s2;publicdoublet;privatevoidsetup(){Panelbl=newPanel();bl.setLayout(newGridLayout(3,2));bl.add(pl);bl.add(tl);bl.add(p2);bl.add(t2);bl.add(p3);bl.add(t3);Panelb2二newPanel();b2.setLayout(nevGrid