资源描述:
《实验三 类和对象》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、//实验三第一题源程序classStudent{privateStringname;privateintage;privateStringspecialty;publicStudent(Stringname,intage,Stringspecialty){this.name=name;this.age=age;this.specialty=specialty;}publicStringgetname(){returnname;}publicvoidsetname(Stringname){this.name=name;}pub
2、licintgetage(){returnage;}publicStringspecialty(){returnspecialty;}publicvoidprintValues(){System.out.println("名字为"name);System.out.println("年龄为"age);}}publicclassSS{publicstaticvoidmain(String[]args){Students1=newStudent("jack",20,"");s1.printValues();}}//实验三第二题源程
3、序classPoint{publicdoublex,y;publicPoint(doubleu,doublev){setX(u);setY(v);}voidsetX(doublex1){x=x1;}voidsetY(doubley1){y=y1;}doublegetX(){returnx;}doublegetY(){returny;}Pointgetmid(Points){doublemidX=(x+s.getX())/2;doublemidY=(y+s.getY())/2;returnnewPoint(midX,midY)
4、;}voidshow(){System.out.println("点的坐标:"+x+","+y);}}classDian{publicstaticvoidmain(Stringargs[]){Points1=newPoint(4.0,6.0);Points2=newPoint(8.0,3.0);s1.show();s2.show();Points=s1.getmid(s2);s.show();}}//实验三第三题源程序(复数及运算)classComplex{intx;inty;voidComplex(inti,intj){x
5、=i;y=j;}voidShowComp(){if(y>0)System.out.println(x+"+"+y+"i");elseif(y==0)System.out.println(+x);elseSystem.out.println(""+x+y+"i");}}publicclassFs{publicstaticComplexaddComp(ComplexC1,ComplexC2){ComplexC=newComplex();C.x=C1.x+C2.x;C.y=C1.y+C2.y;returnC;}publicstat
6、icComplexsubComp(ComplexC1,ComplexC2){ComplexC=newComplex();C.x=C1.x-C2.x;C.y=C1.y-C2.y;returnC;}publicstaticComplexmultiComp(ComplexC1,ComplexC2){ComplexC=newComplex();C.x=C1.x*C2.x-C1.y*C2.y;C.y=C1.x*C2.y+C1.y*C2.x;returnC;}publicstaticbooleanequalComp(ComplexC1,
7、ComplexC2){booleana;if(C1.x==C2.x&&C1.y==C2.y)a=true;elsea=false;returna;}publicstaticvoidmain(String[]args){booleana;ComplexC1=newComplex();ComplexC2=newComplex();ComplexC3=newComplex();ComplexC4=newComplex();ComplexC5=newComplex();C1.Complex(1,5);System.out.print
8、("复数1是:");C1.ShowComp();C2.Complex(3,-8);System.out.print("复数2是:");C2.ShowComp();System.out.print("两复数的和是:");C3=addComp(C1,C2);C3.ShowComp();Syst