《JAVA高级练习题.doc》由会员分享,可在线阅读,更多相关《JAVA高级练习题.doc(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-作者xxxx-日期xxxxJAVA高级练习题【精品文档】1、 编写一个程序,实现从命令行参数输入两个字符串类型的数值,并计算输出两个数值的和。2、编写一个程序,实现从命令行参数输入一字符串,统计该字符串中字符“e”出现的次数。package zuoye;/* * 输出e的出现次数 */import .Scanner;public class Test2 public static void main(String args) Scanner input = new Scanner(System.in);String str=input.next();int count=0;for (int
2、i = 0; i str.length(); i+) if(str.charAt(i)=e)count+;System.out.println(e的出现次数是:+count);3、生成十个0100之间的随机数,放到数组中,然后排序输出。package zuoye;import .Arrays;/* * 生成十个0100之间的随机数,放到数组中,然后排序输出 * author Administrator * */public class Test3 public static void main(String args) int arr=new int10;/定义一个长度为10的int数组for
3、 (int i = 0; i s.lastIndexOf(.)System.out.println(邮箱地址中.出现在的前面);return false;for(int i=0;i=a&s.charAt(i)=A&s.charAt(i)=Z)elseSystem.out.println(用户名里有其他字符);return false;return true;package youxiang;public class Test public static void main(String args) / TODO Auto-generated method stubif(MailTest.tes
4、tMail()System.out.println(邮箱格式合法);elseSystem.out.println(邮箱格式不合法);6、分别在控制台输入字符串和子字符串,并计算字符串中子字符串出现的次数。package zuoye;import .Scanner;/* * 控制台输入一段字符串,再查询一段子字符串出现的次数 * */public class Test6 public static void main(String args) Scanner input = new Scanner(System.in);String a = input.next();String b = inp
5、ut.next();int count = 0;/计数器for(int i = 0;ia.length()-b.length();i=a.indexOf(b, i)+1)if(a.indexOf(b, i)!=-1)count+;System.out.println(count);集合类1、请使用LinkedList来模拟一个队列(先进先出的特性):1) 拥有放入对象的方法void put(Object o)2) 取出对象的方法Object get()3) 判断队列当中是否为空的方法boolean isEmpty();并且,编写测试代码,验证你的队列是否正确。public class Link
6、eds List l;Linkeds()l=new LinkedList();public void put(Object o)l.add(o);public Object get()Object o= l.get(0);l.remove(0);return o;public boolean isEmpty()if(l.isEmpty()return true;return false;public static void main(String args)/定义一个空队列Linkeds l = new Linkeds();/往队列中放入对象(Tom1);(John2);(Mary3);/如果
7、队列不为空,依次输出队列中的元素while(!l.isEmpty()();2、假设顺序列表ArrayList中存储的元素是整型数字15,遍历每个元素,将每个元素顺序输出。package zuoye;import .ArrayList;import .Iterator;import .List;public class Test1 public static void main(String args) List list=new ArrayList();Number n1=new Number(1);Number n2=new Number(2);Number n3=new Number(3)
8、;Number n4=new Number(4);Number n5=new Number(5);(n1);(n2);(n3);(n4);(n5);Iterator it=();while()();3、在一个列表中存储以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)将集合进行排序,并将排序后的结果打印在控制台上package zuoye;public class Fruit private String fruit;public Fruit()public Fruit(String fruit)this.fruit=fruit;public Str
9、ing getFruit() return fruit;public void setFruit(String fruit) this.fruit = fruit;package zuoye;import .Arrays;import .Collections;import .List;public class FruitTest public static void main(String args) List fruit = (apple grade banana pear.split( );(最大值是: + (fruit);(最小值是: + (fruit);4、编写一个程序,创建一个Ha
10、shMap对象,用于存储银行储户的信息(其中储户的主要信息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。package zuoye;public class Bank private String id;private String name;private double money;public Bank()public Bank(String id,String name,double money)this.id=id;this.name=name;this.money=money;public String getId() return id;public void
11、 setId(String id) this.id = id;public String getName() return name;public void setName(String name) this.name = name;public double getMoney() return money;public void setMoney(double money) this.money = money;package zuoye;import .HashMap;import .Iterator;import .Map;import .Set;public class BankTes
12、t public static void main(String args) Map map=new HashMap();(用户1, new Bank(1,张三,()*100000);(用户2, new Bank(2,李四,()*100000);(用户3, new Bank(3,王五,()*100000);(用户4, new Bank(4,赵六,()*100000);Set keySet=();Iterator it = (); double sum = 0; while() Object key = (); Bank a = (key); (账户+()+的余额:+(); /计算所有帐户的余额
13、之和 sum += (); (所有账户余额:+sum);5、从控制台输入若干个单词(输入回车结束)放入集合中,将这些单词排序后(忽略大小写)打印出来。package zuoye;import .ArrayList;import .List;import .Scanner;public class Test5 public static void main(String args) Scanner input = new Scanner(System.in);List list = new ArrayList();String str = ();String arr = ( );String t
14、mp;for (int i = 0; i ; i+) for (int j = i + 1; j 0) str = arrj;arrj = arrj + 1;arrj + 1 = str;for (String a : arr) (a);(a);IO流1、在本机的磁盘系统中,找一个文件夹,利用File类的提供方法,列出该文件夹中的所有文件的文件名和文件的路径,执行效果如下:-路径名:c:tempdef.txt package 作业;import ;public class Test1 public static void main(String args) File file = new Fi
15、le(E:新建文件夹);String files = file.list();for (String o : files) System.out.println(文件名: + o);System.out.println(路径: + file.getAbsolutePath() + + o);System.out.println(-);2、编写一个java程序实现文件复制功能,要求将中的内容复制到中。package 作业;import ;import ;import ;/* * 文件的拷贝 */public class Test2 public static void main(String a
16、rgs) File file = new File(fileabc.txt);File file_back = new File(fileabc_back.txt);try (FileInputStream fi = new FileInputStream(file); FileOutputStream fo = new FileOutputStream(file_back);) int b = -1;while (b = () != -1) (b); catch (Exception e) ();(拷貝成功!);3、创建文件并在其中输入hello world创建一个输入流读取该文件中的文本并
17、且把小写的l变成大写L再利用输出流写入到中实现步骤:1.在本地硬盘C盘下创建一个文件 2.创建一个包含main()方法的类,并在main中编写代码 3运行代码并且测试结果实现过滤器的功能效果显示:package 作业;import ;import ;import ;import ;import ;import ;/* * 替换文件中的小写字母l为大写字母L */public class Test3 public static void main(String args) FileReader f;String s = ;BufferedReader br = null;try f = new
18、FileReader(F:test.txt);br = new BufferedReader(f);s = ();(源文件 + s);s = (l, L); catch (FileNotFoundException e) (); catch (IOException e) (); finally try (); catch (IOException e) ();BufferedWriter bw = null;(s);try FileWriter fw = new FileWriter(F:test.txt);bw = new BufferedWriter(fw);(s); catch (IO
19、Exception e) (); finally try (); catch (IOException e) ();4、在程序中创建一个Student类型的对象,并把对象信息保存到文件中,然后再从文件中把Student对象的信息读出显示在控制台上,Student类的描述如下:package 作业;import .Date;public class Student private int id;private String name;private Date birth;public Student() super();public Student(int id, String name, Da
20、te birth) super();this.id = id;this.name = name;this.birth = birth;public int getId() return id;public void setId(int id) this.id = id;public String getName() return name;public void setName(String name) this.name = name;public Date getBirth() return birth;public void setBirth(Date birth) this.birth
21、 = birth;Overridepublic String toString() return Student id= + id + , name= + name + , birth= + birth + ;public class SerializationExercise public static void main(String args)Student s1 = new Student(1,zhangsan,1999-11-11);try FileOutputStream fos = new FileOutputStream(d:iostudent.txt);ObjectOutpu
22、tStream oos = new ObjectOutputStream(fos);(对象序列化.);(s1);();(); catch (FileNotFoundException e1) (e(); catch (IOException e2) (e();try FileInputStream fis = new FileInputStream(d:iostudent.txt);ObjectInputStream ois = new ObjectInputStream(fis);(反序列化.);Student ss1 = (Student)();(ss1);(); catch (FileN
23、otFoundException e) (); catch (IOException e) (); catch (ClassNotFoundException e) ();多线程1、利用Thread实现,要求多线程求解某范围素数每个线程负责1000范围:线程1找1-1000;线程2找1001-2000;线程3找2001-3000。编程程序将每个线程找到的素数及时打印。package 找素数;public class Number private int num;public Number() super();public Number(int num) super();this.num = n
24、um;public int getNum() return num;public void setNum(int num) this.num = num;public synchronized void checkNum(int num) if (num = 1000) for (int n = 2; n = num; n+) boolean b = true;if (n != 1) for (int i = 2; i n; i+) if (n % i = 0) b = false;break;if (b) System.out.println(n + 是质数);if (num = 2000)
25、 for (int n = 1001; n = num; n+) boolean b = true;if (n != 1) for (int i = 2; i n; i+) if (n % i = 0) b = false;break;if (b) System.out.println(n + 是质数);if (num = 3000) for (int n = 2001; n = num; n+) boolean b = true;if (n != 1) for (int i = 2; i n; i+) if (n % i = 0) b = false;break;if (b) System.
26、out.println(n + 是质数);package 找素数;/public class Numbers1 implements Runnablepublic class Numbers1 extends Thread private Number num;public Numbers1(Number num) this.num=num;public void run() num.checkNum(1000);package 找素数;/public class Numbers1 implements Runnablepublic class Numbers2 extends Thread
27、private Number num;public Numbers2(Number num) this.num=num;public void run() num.checkNum(2000);package 找素数;/public class Numbers1 implements Runnablepublic class Numbers3 extends Thread private Number num;public Numbers3(Number num) this.num=num;public void run() num.checkNum(3000);package 找素数;pub
28、lic class Test public static void main(String args) Number num=new Number();Numbers1 n1=new Numbers1(num);Thread t1=new Thread(n1);t1.setName(11000:);t1.start();Numbers2 n2=new Numbers2(num);Thread t2=new Thread(n2);t2.setName(10012000:);t2.start();Numbers3 n3=new Numbers3(num);Thread t3=new Thread(
29、n3);t3.setName(20013000);t3.start();2、利用Runnable实现,要求多线程求解某范围素数每个线程负责1000范围:线程1找1-1000;线程2找1001-2000;线程3找2001-3000。编程程序将每个线程找到的素数及时打印。与第一题相同,不再贴出代码,第一题注释即为修改处。3、编写一个Java程序(包括一个主程序类,一个线程类。在主程序类中创建2个线程,将其中一个线程的优先级设为10,另一个线程的优先级设为6。让优先级为10的线程打印200次“线程1正在运行”,优先级为6的线程打印200次“线程2正在运行”。package 优先级;public cl
30、ass Main private int num;public int getNum() return num;public void setNum(int num) this.num = num;public Main() super();public Main(int num) super();this.num = num;public void dayin(int num)for(int i=0;inum;i+)System.out.println(Thread.currentThread().getName()+正在运行);package 优先级;public class Xianch
31、eng1 implements Runnable private Main num;public Xiancheng1() super();public Xiancheng1(Main num) this.num = num;Overridepublic void run() num.dayin(200);package 优先级;public class Xiancheng2 implements Runnable private Main num;public Xiancheng2() super();public Xiancheng2(Main num) this.num = num;Ov
32、erridepublic void run() num.dayin(200);package 优先级;public class Test public static void main(String args) Main num=new Main();Xiancheng1 x1=new Xiancheng1(num);Thread t1=new Thread(x1);t1.setPriority(10);t1.setName(线程1);t1.start();Xiancheng2 x2=new Xiancheng2(num);Thread t2=new Thread(x2);t2.setPriority(6);t2.setName(线程2);t2.start();4、编写一个计时器,每隔一秒钟,在控制台打印出最新时间。package 计时器;import java.;import .Date;public class Main private Date date;public Main() super();/