您好,欢迎访问三七文档
1接口类1.编写一个类,要求该类实现一个接口,该接口有3个abstract方法:Publicabstractvoidf(intx);Publicabstractdoubleg(doublex);Publicabstractdoubleh(doublex);要求在应用程序主类中使用该类创建对象,并使用接口回调调用方法f()、g()和h()。(1)源程序:interfaceA{publicabstractvoidf(intx);publicabstractvoidg(intx,inty);publicabstractdoubleh(doublex);}classA1implementsA{publicvoidf(intx){System.out.println(x);}publicvoidg(intx,inty){intz=x+y;System.out.println(z);}publicdoubleh(doublex){returnx*x;}}classA2implementsA{publicvoidf(intx){System.out.println(Hello:+x);}publicvoidg(intx,inty){intz=x-y;System.out.println(z);}publicdoubleh(doublex){returnMath.sqrt(x);}}classA3implementsA{publicvoidf(intx){System.out.println(你好:+x);}publicvoidg(intx,inty){doublez=(double)x/y;System.out.println(z);2}publicdoubleh(doublex){return1/x;}}publicclassZuoYe5_3{publicstaticvoidmain(Stringargs[]){Aa=newA1();a.f(10);a.g(12,20);System.out.println(a.h(100));a=newA2();a.f(10);a.g(12,20);System.out.println(a.h(100));a=newA3();a.f(10);a.g(12,20);System.out.println(a.h(100));}}2.设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出助教类(Assistant)、讲师类(Instructor)、副教授类(AssistantProfessor)和教授类(Professor)。其中:工资支出=基本工资+授课时数*每课时兼课金。但助教基本工资为800,每课时兼课金25,讲师基本工资为1000,每课时兼课金35,副教授基本工资为1200,每课时兼课金40,教授基本工资为1400,每课时兼课金50。将Person定义为接口,设计程序实现多态性。(1)源程序:classPerson{protectedStringname;protecteddoubleclasssum;publicPerson(){this.name=;classsum=0;}publicPerson(Stringname,doubleclasssum){this.name=name;this.classsum=classsum;}}classAssistantextendsPersonimplementsPersonPay{publicAssistant(Stringname,doubleclasssum){super(name,classsum);}3publicdoublepay(){return800+classsum*25;}publicStringtoString(){return姓名:+this.name+\t工资支出:+this.pay()+\n;}}classInstructorextendsPersonimplementsPersonPay{publicInstructor(Stringname,doubleclasssum){super(name,classsum);}publicdoublepay(){return1000+classsum*35;}publicStringtoString(){return姓名:+this.name+\t工资支出:+this.pay()+\n;}}classAssistantProfessonextendsPersonimplementsPersonPay{publicAssistantProfesson(Stringname,doubleclasssum){super(name,classsum);}publicdoublepay(){return1200+classsum*40;}publicStringtoString(){return姓名:+this.name+\t工资支出:+this.pay()+\n;}}classProfessorextendsPersonimplementsPersonPay{publicProfessor(Stringname,doubleclasssum){super(name,classsum);}publicdoublepay(){return1400+classsum*50;}publicStringtoString(){return姓名:+this.name+\t工资支出:+this.pay()+\n;}}publicclassSy9_2{publicstaticvoidmain(Stringargs[]){Personps[]=newPerson[4];ps[0]=newAssistant(张三,120);4ps[1]=newInstructor(赵二,130);ps[2]=newAssistantProfesson(李四,125);ps[3]=newProfessor(王五,134);Stringoutput=;for(inti=0;ips.length;i++)output+=ps[i];System.out.println(output);}}3.定义一个接口Bank,它包含有oneyearRate(一年定期利率)、oneyearNationaldebt(一年国债利率)和interestRate(活期利率)常数和count()方法及show()方法。Count()方法计算存款一年所得利息及缴利息税后的总金额(存款本金+税后利息),而show()方法用显示总金额。由于对不同的存款方式,利率是不同的,程序分别定义多个接口的实现,实现对利率的计算和显示。注:除国债外,定期存款和活期利息都要交纳20%的个人所得税。interfaceBank{floatoneyearRate=0.0178f;floatoneyearNationaldebt=0.0198f;floatinterestRate=0.0078f;abstractvoidcount();abstractvoidshow();}classInterestRateCalcuteimplementsBank{privatefloatsaving;privatedoublesum;InterestRateCalcute(floati){saving=i;count();}publicvoidcount(){sum=saving+(saving*Bank.interestRate*0.8);}publicvoidshow(){System.out.println(Withcurrentinterestrate,sum=+sum);;}}classOneyearNationaldebtCalcuteimplementsBank{privatefloatsaving;privatedoublesum;OneyearNationaldebtCalcute(floati){saving=i;count();}publicvoidcount(){sum=saving+(saving*Bank.oneyearNationaldebt);}publicvoidshow(){5System.out.println(Withnationaldebtrate,sum=+sum);}}classOneyearRateCalcuteimplementsBank{privatefloatsaving;privatedoublesum;OneyearRateCalcute(floati){saving=i;count();}publicvoidcount(){sum=saving+(saving*Bank.oneyearRate*0.8);}publicvoidshow(){System.out.println(Withfixedrate,sum=+sum);}}classCalculate{publicstaticvoidmain(Stringargs[]){intmon=10000;OneyearRateCalcuterate=newOneyearRateCalcute(mon);rate.show();OneyearNationaldebtCalcutedebt=newOneyearNationaldebtCalcute(mon);;debt.show();InterestRateCalcuteinterest=newInterestRateCalcute(mon);interest.show();}}
本文标题:java实验报告9
链接地址:https://www.777doc.com/doc-6291303 .html