收藏 分销(赏)

期末复习JAVA题.doc

上传人:精*** 文档编号:4135765 上传时间:2024-07-31 格式:DOC 页数:30 大小:66.21KB 下载积分:12 金币
下载 相关 举报
期末复习JAVA题.doc_第1页
第1页 / 共30页
期末复习JAVA题.doc_第2页
第2页 / 共30页


点击查看更多>>
资源描述
1. 分解质因数 package d2; import java.util.Scanner; public class Zys { public static void main(String[]args){ Scanner sc=new Scanner(System.in); System.out.print("请输入一个数:"); int a=sc.nextInt(); int n=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. 判断是否是回文数 ackage c2; import java.util.Scanner; public class huiwenshu { public static void main(String[]args){ //System.out.println("请输入一个数"); Scanner a=new Scanner(System.in); System.out.println("请输入一个数"); String b=a.nextLine(); boolean c=false; for(int i=0;i<b.length();i++){ if(b.charAt(i)!=b.charAt(b.length()-i-1)){ c=false; } else c=true; if(c==true) System.out.println("是回文数"); }else{ System.out.println("不是回文数") } } } 3. 数组逆序输出 public class sz { public static void main(String[]args){ int a[]={1,2,3,4,5,6,7,8,9,0}; for(int i=9;i>=0;i--){ System.out.println(a[i]); } } } 4. 水仙花数 public class sxh { public static void main(String[]args){ int a,b,c; for(int i=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. 素数 public class Sushu { public static void main(String[] args){ for(int i=100;i<=200;i++){ boolean b=false; for(int j=2;j<=Math.sqrt(i);j++ ){ if(i%j==0){ b=true;break; } else {b=false;} } if (b==false) System.out.println(i); } } } 6.最大公约数和最小公倍数 import java.util.Scanner; public class Sz { public static void main(String[] args) { int a, b, m; Scanner s = new Scanner(System.in); System.out.print("键入一个整数: "); a = s.nextInt(); System.out.print("再键入一个整数: "); b = s.nextInt(); deff cd = new deff(); m = cd.deff(a, b); int n = a * b / m; System.out.println("最大公约数: " + m); System.out.println("最小公倍数: " + n); } } class deff { public int deff(int x, int y) { int t; if (x < y) { t = x; x = y; y = t; } while (y != 0) { if (x == y) return x; else { int k = x % y; x = y; y = k; } } return x; } } 1、题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程     找出1000以内的所有完数。  public class shishu { public static void main(String[] args) { int i; for (i = 2; i <= 1000; i++) { int sum = 0; for (int j = 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个属性:姓名、工号以及工资。经理也是员工,除了含有员工的属性外,另外还有一个奖金属性。请使用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。 public class manager extends yg{ protected int award; public static void main(String[]args){ yg a=new yg(); a.setdata("张晓晓","01234",5000); a.print(); manager b=new manager(); b.setdata("黄洋洋","01235",6000); b.award=20000; b.print(); System.out.println("奖金"+b.award);; } } class yg { protected String name; protected String num; protected int salary; void setdata(String xm,String gh,int gz){ name =xm; num=gh; salary=gz; } public void print(){ System.out.println("姓名:"+name); System.out.println("工号"+num); System.out.println("工资"+salary); } } 3. 以点类作为基类,从点派生出圆,从圆派生圆柱,设计成员函数输出它们的面积和体积。 package third; public class Circle extends point{ protected int r; Circle(int r,int x,int y){ super(x,y); this.r=r; } public double area(){ return 3.14*r*r; } package third; public class Cyliner extends Circle { protected int h; Cyliner(int h,int x,int y,int r){ super(x,y,r); this.h=h; } public double volume(){ return area()*h; } public static void main(String[]args){ Circle a=new Circle(2,2,4); Cyliner b=new Cyliner(2,2,3,4); System.out.println("圆的面积是:"+a.area()); System.out.println("圆的体积是:"+b.volume()); } } } public class point { protected int x; protected int y; point(){ this.x=0; this.y=0; } point(int x,int y){ this.x=x; this.y=y; } } 4.定义一个抽象基类Shape,它包含三个抽象方法center()、diameter()、getArea(),从Shape类派生出Square和Circle类,这两个类都用center()计算对象的中心坐标,diameter()计算对象的外界圆直径,getArea()方法计算对象的面积。编写编写应用程序使用Rectangle类和Circle类。 package W1; public abstract class Shape { abstract void center(); abstract void diameter(); abstract void getArea(); } package W1; public class Square extends Shape{ @Override void center() { } @Override void diameter() { } @Override void getArea() { } } package W1; public class Circle extends Shape{ @Override void center() { } @Override void diameter() { } @Override void getArea() { } } package W1; public class Rectangle extends Square{ } package W1; public class Test { public static void main(String[] args) { Circle c = new Circle(); Square r = new Rectangle(); 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类型),写出该类的构造方法; public class person { private String name; private String address; public person(String name2, String address2) { } public void Person(String n,String a){ this.setName(n); this.setAddress(a); } public void setName(String n){ this.name = n; } public void setAddress(String a){ this.address = a; } private String getName(){ return this.name; } private String getAddress(){ return this.address; } } lass Empolyee extends person{ private int employeeNo; private double pay; private int age; public Empolyee(String name,String address,int empolyeeNO, double pay,int age){ super(name,address); this.setEmployeeNo(empolyeeNO); this.setPay(pay); this.setAge(age); } public void setEmployeeNo(int e){ this.employeeNo = e; } public void setPay(double p){ this.pay = p; } public void setAge(int a){ this.age = a; } public int getEmployeeNo(){ return this.employeeNo; } public double getPay(){ return this.pay; } public int getAge(){ return this.age; } } class Manager extends Empolyee{ private int level; public Manager(String name,String address,int empolyeeNO,double pay, int age,int level){ super(name,address,empolyeeNO,pay,age); this.setLevel(level); } public void setLevel(int l){ this.level = l; } public int getLevel(){ return this.level; } public void printInfo(){ System.out.println("姓名:"+this.getName()+",工号:"+this.getEmployeeNo() +",工龄:"+this.getAge()+",级别:"+this.getLevel() +",地址:"+this.getAddress()); System.out.println("原工资:"+this.getPay()); System.out.println("涨工资后的工资:"+new Add().addPay(this.getLevel(), this.getPay())); } private String getAddress() { return null; } private String getName() { return null; } } class Add{ public double addPay(int level,double pay){ if(level==0){ pay = (1+0.1)*pay; } else if(level==1){ pay = (1+0.2)*pay; } else{ System.out.println("级别只能为0或1,0表示普通员工,1则表示经理,操作无效!"); } return pay; } } public class ExtendsDemo { public static void main(String[] args) { Manager m1 = new Manager("张三","南京路",0001,2000,10,0); Manager m2 = new Manager("李四","长江路",0002,3000,15,1); m1.printInfo(); System.out.println("************************************************"); m2.printInfo(); } } 编写一个测试类,产生一个员工和一个经理并输出其具有的信息。 1.定义一个接口Assaultable(可攻击的),该接口有一个抽象方法attack()。 2.定义一个接口Mobile(可移动的),该接口有一个抽象方法move()。 3.定义一个抽象类Weapon,实现Assaultable接口和Mobile接口,但并没有给出具体的实现方法。 4.定义3个类:Tank,Flighter,WarShip都继承自Weapon,分别用不同的方式实现Weapon类中的抽象方法。 5.写一个类Army,代表一支军队,这个类有一个属性是Weapon数组w(用来存储该军队所拥有的所有武器);该类还提供一个构造方法,在构造方法里通过传一个int类型的参数来限定该类所能拥有的最大武器数量,并用这一大小来初始化数组w。该类还提供 一个方法addWeapon(Weapon wa),表示把参数wa所代表的武器加入到数组w中。在这个类中还定义两个方法attackAll()和moveAll(),让w数组中的所有武器攻击和移动。 6.写一个主方法去测试以上程序。 class Army { private Weapon[] w = null; private int size = 0; private Army() { } public Army(int i) { w = new Weapon[i]; } public void addWeapon(Weapon weapon) { if (size >= w.length) { System.out.println("军队装备足够了!"); return; } else { w[size] = weapon; size++; } } public void attacAll() { for (Weapon wea : w) { if (wea != null) { wea.attack(); } } } public void moveAll() { for (Weapon wea : w) { if (wea != null) { wea.move(); } } } } package A1; class Flighter extends Weapon { public void attack() { System.out.println("Flighter attacks"); } public void move() { System.out.println("Flighter moves"); } } public class NotSimple { public static void main(String[] args) { Army a = new Army(3); a.addWeapon(new Tank()); a.addWeapon(new Flighter()); a.addWeapon(new WarShip()); a.attacAll(); a.moveAll(); } } interface Assaultable { abstract public void attack(); } interface Mobile { abstract public void move(); } abstract class Weapon implements Assaultable, Mobile { } class Tank extends Weapon { public void attack() { System.out.println("Tank attacks"); } public void move() { System.out.println("Tank moves"); } } package A1; class WarShip extends Weapon { public void attack() { System.out.println("WarShip attacks"); } public void move() { System.out.println("WarShip moves"); } } class Army { private Weapon[] w = null; private int size = 0; private Army() { } public Army(int i) { w = new Weapon[i]; } public void addWeapon(Weapon weapon) { if (size >= w.length) { System.out.println("军队装备足够了!"); return; } else { w[size] = weapon; size++; } } public void attacAll() { for (Weapon wea : w) { if (wea != null) { wea.attack(); } } } public void moveAll() { for (Weapon wea : w) { if (wea != null) { wea.move(); } } } } package A1; class Flighter extends Weapon { public void attack() { System.out.println("Flighter attacks"); } public void move() { System.out.println("Flighter moves"); }} package A1; public class NotSimple { public static void main(String[] args) { Army a = new Army(3); a.addWeapon(new Tank()); a.addWeapon(new Flighter()); a.addWeapon(new WarShip()); a.attacAll(); a.moveAll(); } } interface Assaultable { abstract public void attack(); } interface Mobile { abstract public void move(); } abstract class Weapon implements Assaultable, Mobile { } package A1; class Tank extends Weapon { public void attack() { System.out.println("Tank attacks"); } public void move() { System.out.println("Tank moves"); } } package A1; class WarShip extends Weapon { public void attack() { System.out.println("WarShip attacks"); } public void move() { System.out.println("WarShip moves"); } } 一个公司有2辆小汽车(car),一辆公共汽车(bus),其中:第一小辆汽车每天跑300公里;第二辆小汽车每天跑400公里;公共汽车每天跑500公里,已知小汽车每百公里耗油量为7升,公共汽车每百公里耗油量为10升,用接口编程计算每天所有交通工具的耗油量。 public interface Vehicle { int getfuel(); } import c1.Vehicle; public class bus implements Vehicle { int kil; bus(int k1){kil=k1;} public int getfuel(){ return(10*kil/100); } } import c1.Vehicle; class car implements Vehicle { int kil; car(int k1){kil=k1;} public int getfuel(){ return(7*kil/100); } } import c1.bus; import c1.car; import c1.pany1; import c1.Vehicle; public class pany1 { Vehicle a[]; pany1(){ a=new Vehicle[3]; a[0]=new car(300); a[1]=new bus(500); a[2]=new car(400); } int rf(){ int i=0; for(i=0;i<a.length;i++){ i=i+a[i].getfuel(); } return (i); } public static void main(String[]args){ pany1 c=new pany1(); System.out.println("sum="+c.rf()); } } 编写创建一个box类,在其中定义三个变量表示一个立方体的长、宽和高,定义一个构造方法对这三个变量进行初始化,然后定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。 public class Box { double length; double wide; double high; Box(double l,double w,double h){ this.length=l; this.wide=w; this.high=h; } public void volume(){ double v; v=length*wide*high; System.out.println("box的体积是:"+v); } } public class Test { public static void main(String[]args){ Box a=new Box(2,2,3); a.volume(); } } 定义一个学生类student,属性包括学号、班号、姓名、性别、年龄、班级总人数;方法包括获得学号、获得班号、获得姓名、获得性别、获得年龄、获得班级总人数、修改学号、修改班号、修改姓名、修改性别、修改年龄、以及一个tostring()方法将student类中的所有属性组合成一个字符串。定义一个学生数组对象。设计程序进行测试。 public class Student { private String num; private String grade; private String name; private String sex; private int age; private int count ; Student(String nu,String gr,String na,String se,int ag,int co){ this.num=nu; this.grade=gr; this.name=na; this.sex=se; this.age=ag; this.count=co; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getcount() { return count; } public void setcount(int count) { this.count = count; } public String toString(){ return "学号"+num+"班号"+grade+"姓名"+name+"性别"+sex+"年龄"+age+"班级总人数"+count; } } public class Test3 { public static void main(String[]args){ Student s[]=new Student[2]; s[0]=new Student("130813125","信管131","张晓晓","女",20,34); s[1]=new Student("130813126","信管131","小高密","女",20,34); for(int i=0;i<2;i++){ System.out.println(s[i].toString()); } } } 设计一个人员类person,其中包含一个方法pay,代表人员的工资支出。再以person类派生出教师类teacher和大学生类collegestudent其中: 教师:工资支出=基本工资+授课时数*30 大学生:奖学金支出 将人员类定义为抽象类,pay为抽象方法,设计程序实现多态性。 public abstract class Person { public abstract void pay(int a,int b); public abstract void pay(); } public class CollegeStudent { int award; public void pay(int w){ this.award=w; System.out.println("CollegeStudent的奖学金支出是:"+award); } } public class Teacher extends Person{ int wage; int time; int p; public void pay(int w,int t){ this.wage=w; this.time=t; p=wage+time*30; System.out.println("Teacher的工资支出是:"+p); } @Override public
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服