期末复习JAVA题_第1页
期末复习JAVA题_第2页
期末复习JAVA题_第3页
期末复习JAVA题_第4页
期末复习JAVA题_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、本文档如对你有帮助,请帮忙下载支持! 1. 分解质因数 package d2; import ; public class Zys public static void main(Stringargs) Scanner sc=new Scanner(System.in); 请输入一个数: ); int a=sc.nextInt(); int n=2; =); while(a=n) if(a%n=0) *); a=a/n; if(a%n!=0) n+; 2. 判断是否是回文数 ackage c2; import ; public class huiwenshu public static voi

2、d main(Stringargs) / 请输入一个数 ); Scanner a=new Scanner(System.in); String b=a.nextLine(); boolean c=false; for(int i=0;i=0;i-) J 4. 水仙花数 public class sxh public static void main(Stringargs) 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) 5. 素数 public cl

3、ass 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) 6最大公约数和最小公倍数 import ; public static void main(String args) int a, b, m; Scanner s = new Scanner(System.in); 键入一个整数: ); a = s.next

4、Int(); 再键入一个整数: ); b = s.nextInt(); deff cd = new deff(); m = cd.deff(a, b); int n = a * b / m; 最大公约数 : + m); 最小公倍数 : + 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; public class Sz return x; 1、

5、题目:一个数如果恰好等于它的因子之和,这个数 就称为 完数 。例如 6=123. 编程找出 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 = w.Iength) 军队装备足够了 !); eIse wsize = weapon; size+; pubIic void attacAII() for (Weapon wea : w) if (wea != nuII) wea.at

6、tack(); pubIic void moveAII() for (Weapon wea : w) if (wea != nuII) wea.move(); package A1; cIass FIighter extends Weapon pubIic void attack() FIighter attacks); pubIic void move() FIighter moves); return; 本文档如对你有帮助,请帮忙下载支持! public class NotSimple public static void main(String args) Army a = new Ar

7、my(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 vo

8、id attack() Tank attacks); public void move() Tank moves); package A1; public void attack() WarShip attacks); public void move() WarShip moves); class Army private Weapon w = null; private int size = 0; private Army() public Army(int i) w = new Weaponi; public void addWeapon(Weapon weapon) if (size

9、= w.length) 军队装备足够了 !); return; else wsize = weapon; size+; public void attacAll() for (Weapon wea : w) if (wea != null) wea.attack(); class WarShip extends Weapon 本文档如对你有帮助,请帮忙下载支持! Assaultable, public void moveAll() for (Weapon wea : w) if (wea != null) wea.move(); package A1; class Flighter exten

10、ds Weapon public void attack() Flighter attacks); public void move() 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(); interface Assaultable abstr

11、act public void attack(); interface Mobile abstract public void move(); abstract class Weapon implements Mobile package A1; class Tank extends Weapon public void attack() Tank attacks); public void move() Tank moves); package A1; class WarShip extends Weapon public void attack() WarShip attacks); pu

12、blic void move() WarShip moves); a.moveAll(); 一个公司有 2辆小汽车 (car) ,一辆公共汽车 (bus) , 其中:第一小辆汽车每天跑 300公里; 第二辆小汽车 每天跑 400公里;公共汽车每天跑 500公里,已知小 本文档如对你有帮助,请帮忙下载支持! 汽车每百公里耗油量为 7升,公共汽车每百公里耗油 量为 10升,用接口编程计算每天所有交通工具的耗 油量。 public interface Vehicle int getfuel(); import c1.Vehicle; public class bus implements Vehic

13、le 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 pany1; import c1.Vehicle; public class company1 Vehicle a; company1() a=new

14、 Vehicle3; a0=new car(300); a1=new bus(500); a2=new car(400); int rf() int i=0; for(i=0;ia.length;i+) i=i+ai.getfuel(); return (i); public static void main(Stringargs) company1 c=new company1(); sum=+c.rf(); 编写创建一个 box 类,在其中定义三个变量表示一个立 方体的长、宽和高,定义一个构造方法对这三个变量进 行初始化,然后定义一个方法求立方体的体积。创建一 个对象,求给定尺寸的立方体的

15、体积。 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; box 的体积是: +v); public class Test public static void main(Stringargs) Box a=new Box(2,2,3); a.vo

16、lume(); 定义一个学生类 student ,属性包括学号、班号、 姓名、 性别、年龄、班级总人数;方法包括获得学号、获得班 号、 获得姓名、 获得性别、 获得年龄、 获得班级总人数、 修改学号、 修改班号、 修改姓名、 修改性别、 修改年龄、 以及一个 tostring () 方法将 student 类中的所有属性 组合成一个字符串。定义一个学生数组对象。设计程序 进行测试。 public class Student private String num; private String grade; private String name; private String sex; pri

17、vate int age; private int count ; Student(String nu,String gr,String na,String se,int ag,int co) this.num=nu; this.grade=gr; =na; 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

18、setGrade(String grade) this.grade = grade; public String getName() return name; public void setName(String name) = name; public String getSex() return sex; public void setSex(String sex) this.sex = sex; this.sex=se; return age; public int getAge() 本文档如对你有帮助,请帮忙下载支持! public void setAge(int

19、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(Stringargs) Student s=new Student2; s0=new Student(5,信管 131, 张晓晓

20、 , 女 ,20,34); s1=new Student(6,信管 131, 小高密 , 女 ,20,34); for(int i=0;i2;i+); 设计一个人员类 person,其中包含一个方法 pay,代表 人员的工资支出。 再以 person 类派生出教师类 teacher 和大学生类 collegestudent 其中: 教师:工资支出 =基本工资 +授课时数 *30 大学生:奖学金支出 将人员类定义为抽象类, pay 为抽象方法,设计程序实 现多态性。 public abstract class Person public abstract void pay(int a,int b

21、); public abstract void pay(); public class CollegeStudent int award; public void pay(int w) this.award=w; 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; Teacher 的工资支出是: +p); Overri

22、de public void pay() / TODO Auto-generated method stub public class Test2 本文档如对你有帮助,请帮忙下载支持! public static void main(Stringargs) Teacher a=new Teacher(); CollegeStudent b=new CollegeStudent(); a.pay(5000,50); b.pay(3000); 1. 将1100之间的所有正整数在放在一个List 集合中,并将集合中索引位置是 10的对象从 集合中移除。 import java.util.*; pub

23、lic class Test public static void main(String args) List list1 = new ArrayList(); for (int i = 1; i = 100; i+) list1.add(i); list1.remove(10); Iterator it = list1.iterator(); while (it.hasNext() J 2. 分别向 Set 集合以及 List 集合中添加“ A”“、 a”、 “ c”、” C、” a”5个元素,观察重复值能否在List 集合以及 Set 集中成功添加。 import ; import ;

24、import ; public class Test2 public static void main(String args) Set set=new HashSet(); set.add(A); set.add(a); set.add(c); set.add(C); set.add(a); J List list1=new ArrayList(); list1.add(A); list1.add(a); list1.add(c); list1.add(C); list1.add(a); 3.(List )已知有一个 Worker 类如下: public class Worker priva

25、te int age; private String name; private double salary; public Worker () public Worker (String name, int age, double salary) = name; this.age = age; this.salary = salary; public int getAge() return age; import ; public void setAge(int age) 本文档如对你有帮助,请帮忙下载支持! this.age = age; public String g

26、etName() return name; public void setName(String name) = name; public double getSalary() return salary; public void setSalary(double salary) this.salary = salary; public void work() + “ work ” ); 完成下面的要求 1) 创建一个 List ,在 List 中增加三个工人, 基本信息如下: 姓名 年龄 工资 zhang3 18 3000 li4 25 3500 wang5 22 320

27、0 2) 在 li4 之前插入一个工人,信息为:姓名: zhao6 ,年龄: 24,工资 3300 3) 删除 wang5 的信息 4) 利用 for 循环遍历,打印 List 中所有工 人的信息 5) 利用迭代遍历,对 List 中所有的工人调用 work 方法。 package z3; public class Worker private int age; private String name; private double salary; public Worker () public Worker (String name, int age, double salary) this

28、.age = age; this.salary = salary; public int getAge() return age; public void setAge(int age) this.age = age; public String getName() return name; public void setName(String name) = name; public double getSalary() return salary; public void setSalary(double salary) this.salary = salary; pu

29、blic void work() work); package z3; import ; import ; = name;import ; 本文档如对你有帮助,请帮忙下载支持! public class Test3 public static void main(String args) Worker w1=new Worker(zhang3,18,3000); Worker w2=new Worker(li4,25,3500); Worker w3=new Worker(wang5,18,3000); Worker w4=new Worker(zhao6,24,3300)

30、; List list1=new ArrayList(); list1.add(w1); list1.add(w2); list1.add(w3); list1.add(1,w4); list1.remove(w3); for(int i=0;ilist1.size();i+) Worker wer=(Worker)list1.get(i); t+wer.getAge()+t+wer.getSalary(); Iterator it=list1.iterator(); while(it.hasNext() Worker w=(Worker) it.next(); w.work(); (Map)

31、利用Map完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出 该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 package z1; public class Test1 public static void main(Stringargs) HashMap hm=new HashMap(); hm.put(2006, 意大利 ); hm.put(2002, 巴西 ); hm.put(1998, 法国 ); hm.put(1994, 巴西 ); hm.put(1990, 德国 ); hm.put(1986, 阿根廷 ); hm.put(1982, 意大

32、利 ); hm.put(1978, 阿根廷 ); hm.put(1970, 德国 ); hm.put(1962, 巴西 ); hm.put(1966, 英格兰 ); hm.put(1962, 巴西 ); hm.put(1958, 巴西 ); hm.put(1954, 德国 ); hm.put(1950, 乌拉圭 ); hm.put(1938, 意大利 ); hm.put(1934, 意大利 ); hm.put(1930, 乌拉圭 ); 请输入一个年份: ); Scanner sc=new Scanner(System.in); String y=sc.nextLine(); J if(hm.c

33、ontainsKey(y) J import ; import ;else 本文档如对你有帮助,请帮忙下载支持! 没有举办世界杯 ); while (it.hasNext() ; public class Worker 完成下列要求: 1) 使用一个Map以老师的名字作为键,以老师 教授的课程名作为值,表示上述 课程安排。 2) 增加了一位新老师 Allen 教 JDBC 3) Lucy 改为教 CoreJava 4) 遍历Map,输出所有的老师及老师教授的课程 5) *利用Map输出所有教JSP的老师。 package z2; import java.util.*; public class

34、 Test2 public static void main(String args) HashMap hm=new HashMap(); hm.put(Tom, CoreJava); hm.put(John, Oracle); hm.put(Susan, Oracle); hm.put(Jerry, JDBC); hm.put(Jim, Unix); hm.put(Kevin, JSP); hm.put(Lucy, JSP); hm.put(Allen,JDBC); 添加后的结果是: ); J hm.put(Lucy, CoreJava); 替换后的结果是: ); J Iterator it

35、 =hm.entrySet().iterator(); Set set=hm.keySet(); for(Object str:set) if(hm.get(str).equals(JSP) 教JSP的老师有:+str); 泛型)使用泛型,改写第 5 题 import ; import ; import ; import ; public class Test3 public static void main(String args) Set setWorker=new HashSet(); Scanner sc=new Scanner(System.in); String str=sc.ne

36、xtLine(); Iterator it =setWorker.iterator(); while (it.hasNext() if (it.equals(str) 放入 HashSet 中时不会出现重复元 素); Set )有如下两个类(只写了类的属性,请自行添加相 应的构造方法和 get/set 方法) 要求, 完善 Worker 和 Address 类,使得 Worker 对象 能够正确放入 HashSet 中:即将 Worker 放入 HashSet 中时不会出现重复元素。 并 编写相应测试代码。 package z3; public class Address private St

37、ring addressName; private String zipCode; public void address(String aN,String zC) this.addressName=aN; this.zipCode=zC; public String getAddressName() return addressName; public void setAddressName(String addressName) this.addressName = addressName; public String getZipCode() return zipCode; public

38、 void setZipCode(String zipCode) this.zipCode = zipCode; private String name; private int age; private double salary; private Address address; public void worker(String n,int a,double s,Address ad) =n; this.age=a; this.salary=s; this.address=ad; public String getName() return name; public v

39、oid setName(String name) = name; public int getAge() return age; public void setAge(int age) this.age = age; public double getSalary() return salary; public void setSalary(double salary) this.salary = salary; public Address getAddress() return address; public void setAddress(Address addres

40、s) this.address = address; 编写一个异常类 MyException ,再编写一个类 Student , 该类有一个产生异常的方法 speak(int m)。 要求参数 m的值大于1000时,方法抛出一个 MyException对象。 最后编写主类,在方法中创新 Student 对象,让该对象 调用 speak() 方法。 public class MyException extends Exception public MyException() this( 发生异常了 ); public MyException(String message) super(messa

41、ge); public class Student public void speak(int m)throws MyException if(m1000) throw new MyException( 发生了异常 ); public class Test1 public static void main(String args) Student s=new Student(); / MyException me=new MyException(); int m; try s.speak(1200); catch(MyException e) 出现了异常 ); q创建类Computer,该类中

42、有一个计算两个数的最大公 约数的方法。如果向该方法传递负整数,该方法就会抛 出自定义异常。 package a2; public class MyException extends Exception private String reason; public MyException(int x,int y) reason= 出现异常 ; public String getReason() return reason; package a2; import ; public class Computer int m,n; public void zdgys(int x,int y)throws

43、 MyException if(x=0|y=0) 本文档如对你有帮助,请帮忙下载支持! double p=sum/2; throw new MyException(x,y); else if(xy) int k=0; k=x; x=y; y=k; int r=x%y; while(r!=0) x=y; y=r; r=x%y; 最大公约数是: +y); public static void main(String args) int result =0; Computer c=new Computer(); try 请输入两个数: ); Scanner s=new Scanner(System.

44、in); int x=s.nextInt(); int y=s.nextInt(); c.zdgys(x, y); catch(MyException e) q 自定义类 Sanj ,其中有成员 x,y,z, 作为三边长,构 造方法 Sanj(a,b,c) 分别给 x,y,z 赋值 , 方法求面积 getArea 和显示三角形信息 ( 三个边长 )showInfo ,这 2 个方法中当三条边不能构成一个三角形时要抛出自定 义异常 NotSanjiaoException ,否则显示正确信息。在 另外一个类中的主方法中构造一个Sanj 对象 ( 三边为命 令行输入的三个整数 ) ,显示三角形信息和

45、面积,要求 捕获异常。 package a3; public class NotSanjiaoException extends Exception String reason; public NotSanjiaoException(int a,int b,int c) reason= 三条边不能构成三角形 ; public String getReason() return reason; package a3; public class Sanj int x,y,z; public Sanj(int a,int b,int c) this.x=a; this.y=b; this.z=c;

46、public void getArea() int sum=x+y+z; 本文档如对你有帮助,请帮忙下载支持! double s=Math.sqrt(p*(p-x)*(p-y)*(p-z); 三角形的面积是: +s); public void showInfo() 三角形的边长是: +x+ +y+ +z+ ); package a3; import ; public class Test3 public static void main(String args) int x,y,z ; 请输入三角形的边长 ); Scanner sc=new Scanner(System.in); x=sc.n

47、extInt(); y=sc.nextInt(); z=sc.nextInt(); try if(x+y=z|x+z=y|y+z=x) throw new NotSanjiaoException (x,y,z); else Sanj sj1=new Sanj(x,y,z); sj1.getArea(); sj1.showInfo(); q 写一个方法 void sanjiao(int a,int b,int c) ,判 断三个参数是否能构成一个三角形,如果不能则抛出异 常 IllegalArgumentException ,显示异常信息 a,b,c+ ” 不能构成三角形”,如果可以构成则显示三

48、角形三个边 长,在主方法中得到命令行输入的三个整数,调用此方 法,并捕获异常。 public class IllegalArgumentException extends Exception String reason; public IllegalArgumentException(int a,int b,int c) reason= 异常信息 a,b,c+ ”不能构成三角形 public String getReason() return reason; package a4; import ; public class Triangle /int a,b,c; public static

49、 void sanjiao(int a,int b,int c)throws IllegalArgumentException /a=this.a; /b=this.b; /c=this.c; if(a+b=c|a+c=b|b+c=a) catch(NotSanjiaoException e) b,c); throw new IllegalArgumentException(a, else +b+ +c+ +能构成三角形 ); public static void main(String args) 请输入三条边: ); Scanner sc=new Scanner(System.in); i

50、nt a=sc.nextInt(); int b=sc.nextInt(); int c=sc.nextInt(); try sanjiao(a,b,c); catch (IllegalArgumentException e) J 1. 程序功能: 将键盘上输入的字符在屏幕上显示出来。 package z1; import ; import ; import ; public class Test1 public static void main(String args) BufferedReader in=new BufferedReader(new InputStreamReader(Sy

51、stem.in); 请输入一行数据: ); 您输入的是: +in.readLine(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); 2. 程序功能:将保存在本地机当前文件夹中的 LX5_1.HTML 文本文件的内容在屏幕上显示出来, 然后将其另存为 LX5_1.txt 文件。 package z2; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class

52、 Test2 public static void main(String args) throws IOException FileReader in = new FileReader(); BufferedReader br = new BufferedReader(in); FileWriter out = new FileWriter(); String str; try while (str = br.readLine() != null) 本文档如对你有帮助,请帮忙下载支持! out.write(str + u005cn); in.close(); out.close(); 3.

53、编写一个 java 课的成绩统计程序, 从键盘输入学生 姓名和成绩,程序按成绩排出名次,并统计出最高 分、最低分、平均分。学生的个数在程序运行前是 未知数。 package z3; import ; import ; import ; import ; import ; import ; import ; import ; public class Test3 public static void main(String args) Scanner sc=new Scanner(System.in); 请输入学生数 ); int s=sc.nextInt(); int a=new ints;

54、请输入学生成绩以逗号隔开 ); BufferedReader br=new BufferedReader(new InputStreamReader(System.in); try String str=br.readLine(); String s1=str.split(,); for(int i=0;is1.length;i+) ai=Integer.parseInt(s1i); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); int max=0; int min=100; int s

55、um=0; for (int j = 0; j a.length; j+) for (int i = j+1; i a.length; i+) if(ajai) int k= ai; ai=aj; aj=k; 本文档如对你有帮助,请帮忙下载支持! public int indexOf(String str) / 返回 指定子字符串在此字符串中第一次出现处 的索引。 成绩排名为: ); public int indexOf(String str,int for (int j = 0; j a.length; j+) J fromIndex) / 从指定的索引开始 , 返回指 定子字符串在此字符串

56、中第一次出现处的 索引。 public String toUpperCase() / String 中的所 有字符都转换为大写 for(int j=0;jmax) 为小写 package z4; max=aj; import ; import ; if(aj=0) count+; index=sentense.indexOf(hello,index+ hello.length(); catch (Exception e) 文件目录错误 ); return count; public static void main(String args) throws IOException String

57、article=./src/aritle.txt; String hello=hello; HelloCount h=new HelloCount(); 单词 +hello+ 在文章中出现的次数: +h.getHelloCount(article, hello); 1 4. 编写一个程序 WriteLog.java 实现如下功能:从键 1 盘输入若行文字(可能包含中文),当最后一行输 入 quit# 时,退出程序且将输入内容除 quit# 外全部 存入文件 c:log.txt 中 package z5; import ; import ; import ; import ; import ;

58、public class WriteLog private static PrintWriter out; public static void main(String args) try out = new PrintWriter(new OutputStreamWriter(new FileOutputStream( ); catch (FileNotFoundException e) e.printStackTrace(); Scanner in = new Scanner(System.in); String line =null; while (!quit#.equals(line

59、= in.nextLine() out.println(line); out.flush(); out.close(); 编写程序 : 使用字符输入、输出流读取文件,将一段文字 加密后存入文件,然后再读取,并将加密前与加密后的文 件输出。 package IOTest; import ; import ; import ; import ; import ; import ; 本文档如对你有帮助,请帮忙下载支持! import ; import ; public class Jiami public staticvoid main(String args) throws IOException

60、 BufferedReader br=new BufferedReader(new FileReader(new File(./abc.txt) ); BufferedWriter bw=new BufferedWriter(new FileWriter(new File(./abc.txt),true); Scanner scan=new Scanner(System.in); String str=scan.nextLine(); char c=str.toCharArray(); J char w=new charc.length; for (int i = 0; i c.length;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论