您好,欢迎访问三七文档
1.分解质因数packaged2;importjava.util.Scanner;publicclassZys{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);System.out.print(请输入一个数:);inta=sc.nextInt();intn=2;System.out.println(a+=);while(a=n){if(a%n==0){System.out.print(n+*);a=a/n;}if(a%n!=0){n++;}}}}2.判断是否是回文数ackagec2;importjava.util.Scanner;publicclasshuiwenshu{publicstaticvoidmain(String[]args){//System.out.println(请输入一个数);Scannera=newScanner(System.in);System.out.println(请输入一个数);Stringb=a.nextLine();booleanc=false;for(inti=0;ib.length();i++){if(b.charAt(i)!=b.charAt(b.length()-i-1)){c=false;}elsec=true;if(c==true)System.out.println(是回文数);}else{System.out.println(不是回文数)}}}3.数组逆序输出publicclasssz{publicstaticvoidmain(String[]args){inta[]={1,2,3,4,5,6,7,8,9,0};for(inti=9;i=0;i--){System.out.println(a[i]);}}}4.水仙花数publicclasssxh{publicstaticvoidmain(String[]args){inta,b,c;for(inti=100;i=999;i++){a=i/10%10;b=i%10;c=i/100;if(i==a*a*a+b*b*b+c*c*c)System.out.println(i);}}}5.素数publicclassSushu{publicstaticvoidmain(String[]args){for(inti=100;i=200;i++){booleanb=false;for(intj=2;j=Math.sqrt(i);j++){if(i%j==0){b=true;break;}else{b=false;}}if(b==false)System.out.println(i);}}}6.最大公约数和最小公倍数importjava.util.Scanner;publicclassSz{publicstaticvoidmain(String[]args){inta,b,m;Scanners=newScanner(System.in);System.out.print(键入一个整数:);a=s.nextInt();System.out.print(再键入一个整数:);b=s.nextInt();deffcd=newdeff();m=cd.deff(a,b);intn=a*b/m;System.out.println(最大公约数:+m);System.out.println(最小公倍数:+n);}}classdeff{publicintdeff(intx,inty){intt;if(xy){t=x;x=y;y=t;}while(y!=0){if(x==y)returnx;else{intk=x%y;x=y;y=k;}}returnx;}}1、题目:一个数如果恰好等于它的因子之和,这个数就称为完数。例如6=1+2+3.编程找出1000以内的所有完数。publicclassshishu{publicstaticvoidmain(String[]args){inti;for(i=2;i=1000;i++){intsum=0;for(intj=1;j=i/2;j++){if(i%j==0){//System.out.println(j);sum=sum+j;}}if(sum==i){System.out.println(i);}}}}2.假如我们在开发一个系统时需要对员工进行建模,员工包含3个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另外还有一个奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。publicclassmanagerextendsyg{protectedintaward;publicstaticvoidmain(String[]args){yga=newyg();a.setdata(张晓晓,01234,5000);a.print();managerb=newmanager();b.setdata(黄洋洋,01235,6000);b.award=20000;b.print();System.out.println(奖金+b.award);;}}classyg{protectedStringname;protectedStringnum;protectedintsalary;voidsetdata(Stringxm,Stringgh,intgz){name=xm;num=gh;salary=gz;}publicvoidprint(){System.out.println(姓名:+name);System.out.println(工号+num);System.out.println(工资+salary);}}3.以点类作为基类,从点派生出圆,从圆派生圆柱,设计成员函数输出它们的面积和体积。packagethird;publicclassCircleextendspoint{protectedintr;Circle(intr,intx,inty){super(x,y);this.r=r;}publicdoublearea(){return3.14*r*r;}packagethird;publicclassCylinerextendsCircle{protectedinth;Cyliner(inth,intx,inty,intr){super(x,y,r);this.h=h;}publicdoublevolume(){returnarea()*h;}publicstaticvoidmain(String[]args){Circlea=newCircle(2,2,4);Cylinerb=newCyliner(2,2,3,4);System.out.println(圆的面积是:+a.area());System.out.println(圆的体积是:+b.volume());}}}publicclasspoint{protectedintx;protectedinty;point(){this.x=0;this.y=0;}point(intx,inty){this.x=x;this.y=y;}}4.定义一个抽象基类Shape,它包含三个抽象方法center()、diameter()、getArea(),从Shape类派生出Square和Circle类,这两个类都用center()计算对象的中心坐标,diameter()计算对象的外界圆直径,getArea()方法计算对象的面积。编写编写应用程序使用Rectangle类和Circle类。packageW1;publicabstractclassShape{abstractvoidcenter();abstractvoiddiameter();abstractvoidgetArea();}packageW1;publicclassSquareextendsShape{@Overridevoidcenter(){}@Overridevoiddiameter(){}@OverridevoidgetArea(){}}packageW1;publicclassCircleextendsShape{@Overridevoidcenter(){}@Overridevoiddiameter(){}@OverridevoidgetArea(){}}packageW1;publicclassRectangleextendsSquare{}packageW1;publicclassTest{publicstaticvoidmain(String[]args){Circlec=newCircle();Squarer=newRectangle();c.center();c.diameter();c.getArea();r.center();r.diameter();r.getArea();}}实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个接口Add用于涨工资,普通员工一次能涨10%,经理能涨20%。具体要求如下:(1)Person类中的属性有:姓名name(String类型),地址address(String类型)并写出该类的构造方法;(2)Employee类中的属性有:工号ID(String型),工资wage(double类型),工龄(int型),写出该类的构造方法;(3)Manager类中的属性有:级别level(String类型),写出该类的构造方法;publicclassperson{privateStringname;privateStringaddress;publicperson(Stringname2,Stringaddress2){}publicvoidPerson(Stringn,Stringa){this.setName(n);this.setAddress(a);}publicvoidsetName(Stringn){this.name=n;}publicvoidsetAddress(Stringa){this.address=a;}privateStringgetName(){returnthis.name;}privateStringgetAddress(){returnthis.address;}}lassEmpolyeeextendsperson{privateintemployeeNo;privatedoublepay;privateintage;publicEmpolyee(Stringname,Stringaddress,intempolyeeNO,doublepay,intage){super(name,address);this.setEmployeeNo(empolyeeNO);this.setPay(pay);this.setAge(age);}publicvoidsetEmployeeNo(inte){this.employeeNo=e;}publicvoidsetPay(doublep){this.pay=p;}publicvoidsetAge(inta){this.age=a;}publicintgetEmployeeNo(){returnthis.employeeNo;}publicdoublegetPay(){returnthis.pay;}publicintgetAge(){returnthis.age;}}classManagerextendsEmpolyee{privateintlevel;publicManager(Stringname,Stringaddress,intempolyeeNO,doublepay,intage,intlevel){super(name,address,empolyeeNO,pay,age);this.set
本文标题:期末复习JAVA题
链接地址:https://www.777doc.com/doc-2322722 .html