JAVA高級(jí)練習(xí)題_第1頁
JAVA高級(jí)練習(xí)題_第2頁
JAVA高級(jí)練習(xí)題_第3頁
JAVA高級(jí)練習(xí)題_第4頁
JAVA高級(jí)練習(xí)題_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余12頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1、編寫一個(gè)程序,實(shí)現(xiàn)從命令行參數(shù)輸入兩個(gè)字符串類型的數(shù)值,并計(jì)算輸出兩個(gè)數(shù)值的和。2、編寫一個(gè)程序,實(shí)現(xiàn)從命令行參數(shù)輸入一字符串,統(tǒng)計(jì)該字符串中字符“e”出現(xiàn)的次數(shù)。package zuoye;/* 輸出e的出現(xiàn)次數(shù)* /import java.util.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 i = 0; i str .len

2、gth(); i+) if (str .charAt( i)= e) count +;System. out .println( e 的出現(xiàn)次數(shù)是:+ count );3、生成十個(gè)0100之間的隨機(jī)數(shù),放到數(shù)組中,然后排序輸出。package zuoye;import java.util.Arrays;/* 生成十個(gè)0100之間的隨機(jī)數(shù),放到數(shù)組中,然后排序輸出* author Administrator*/public class Test3 public static void main(String口args ) int arr= new int 10; / 定義一個(gè)長(zhǎng)度為 10 的jn

3、t數(shù)組for (int i = 0; i s.lastIndexOf( .) System. out .println(郵箱地址中.出現(xiàn)在的前面); return false ; for (int i=0; i= a& s.charAt( i)= A& s.charAt( i)= Z) else System. out .println(用戶名里有其他字符); return false ; return true ; package youxiang; public class Test public static void main(String args) / TODO Auto-gene

4、rated method stub if (MailTest. testMail ()System. out .println(郵箱格式合法);else System. out .println(郵箱格式不合法);6、分別在控制臺(tái)輸入字符串和子字符串,并計(jì)算字符串中子字符串出現(xiàn)的次數(shù)。package zuoye;import java.util.Scanner;/*控制臺(tái)輸入一段字符串,再查詢一段子字符串出現(xiàn)的次數(shù)*/public class Test6 public static void main(String口 args ) Scanner input = new Scanner(Sys

5、tem. in);String a = input .next();String b = input .next();int count = 0; / 計(jì)數(shù)器for (int i = 0; i a.length()- b.length(); i=a.indexOf( b, i)+1) if(a.indexOf( b, i)!=-1)count +;System. out .println( count );集合類1、請(qǐng)使用LinkedList來模擬一個(gè)隊(duì)列(先進(jìn)先出的特性):1)擁有放入對(duì)象的方法void put(Object o)2)取出對(duì)象的方法 Object get()3)判斷隊(duì)列當(dāng)中是

6、否為空的方法boolean isEmpty();并且,編寫測(cè)試代碼,驗(yàn)證你的隊(duì)列是否正確。publicclassLinkeds Listl ;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)

7、/ 定義一個(gè)空隊(duì)列Linkeds l = new Linkeds();/ 往隊(duì)列中放入對(duì)象l.put(Tom1);l.put(John2);l.put(Mary3);/ 如果隊(duì)列不為空,依次輸出隊(duì)列中的元素while(!l.isEmpty()(l.get();2、假設(shè)順序列表ArrayList 中存儲(chǔ)的元素是整型數(shù)字15 ,遍歷每個(gè)元素,將每個(gè)元素順序輸出。package zuoye;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Test1 public stati

8、c void main(String args) List list=new ArrayList();Number n1=new Number(1);Number n2=new Number(2);Number n3=new Number(3);Number n4=new Number(4);Number n5=new Number(5);list.add(n1);list.add(n2);list.add(n3);list.add(n4);list.add(n5);Iterator it=list.iterator();while(it.hasNext() (it.next(); 3、在一個(gè)

9、歹U表中存儲(chǔ)以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)將集合進(jìn)行排序,并將排序后的結(jié)果打印在控制臺(tái)上 package zuoye;public class Fruit private String fruit ; public Fruit() ) public Fruit(String fruit ) this .fruit =fruit; ) public String getFruit() return fruit ; ) public void setFruit(String fruit ) this .fruit = fruit ;

10、)package zuoye;import java.util.Arrays;import java.util.Collections;import java.util.List;public class FruitTest public static void main(String口 args) List fruit = Arrays.asList(apple grade banana pear.split( );(最大值是:+ Collections.max(fruit);(最小值是:+ Collections.min(fruit); 4、編寫一個(gè)程序,創(chuàng)建一個(gè)HashMap對(duì)象,用于存

11、儲(chǔ)銀行儲(chǔ)戶的信息(其中儲(chǔ)戶的主要信息有儲(chǔ)戶的ID,姓名和余額)。另外,計(jì)算并顯示其中某個(gè)儲(chǔ)戶的當(dāng)前余額。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 ;p

12、ublic void 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 java.util.HashMap;import java.util.Iterator;impo

13、rt java.util.Map;import java.util.Set;public class BankTest public static void main(String args) Map map=new HashMap();map.put( 用戶1,newBank(1,張三,Math.random()*100000);map.put( 用戶2,newBank(2,李四,Math.random()*100000);map.put( 用戶3,newBank(3,王五,Math.random()*100000);map.put( 用戶4,newBank(4,趙六,Math.random

14、()*100000);Set keySet=map.keySet();Iterator it = keySet.iterator();double sum = 0;while(it.hasNext()Object key = it.next();Bank a = map.get(key);(賬戶 +a.getId()+ 的余額: +a.getMoney(); / 計(jì)算所有帳戶的余額之和sum += a.getMoney(); (所有賬戶余額: +sum);5、從控制臺(tái)輸入若干個(gè)單詞(輸入回車結(jié)束)放入集合中,將這些單詞排序后(忽略大小寫)打印出來。package zuoye;import ja

15、va.util.ArrayList;import java.util.List;import java.util.Scanner;public class Test5 public static void main(String args) Scanner input = new Scanner(System.in); List list = new ArrayList();String str = input.nextLine();String arr = str.split( );String tmp;for (int i = 0; i arr.length; i+) for (int j

16、 = i + 1; j 0) str = arrj;arrj = arrj + 1;arrj + 1 = str; for (String a : arr) list.add(a); (a);IO 流1 、在本機(jī)的磁盤系統(tǒng)中,找一個(gè)文件夾,利用 File 類的提供方法,列出該文件夾中的所 有文件的文件名和文件的路徑,執(zhí)行效果如下:-路徑名: c:tempdef.txtpackage 作業(yè) ;import ;public class Test1 public static void main(String args ) File file = new File( 新建文件夾);String fi

17、les = file .list();for (String o : files ) System. out .println( 文件名: + o);System. out .println( 路徑 : + file .getAbsolutePath() + + o );System. out .println( );2 、編寫一個(gè)java 程序?qū)崿F(xiàn)文件復(fù)制功能,要求將d:/io/copysrc.doc 中的內(nèi)容復(fù)制到d:/io/copydes.doc 中。package 作業(yè) ;import ;import ;import ;/* 文件的拷貝*/public class Test2 publ

18、ic static void main(String args) 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 = fi.read() != -1) fo.write(b); catch (Exception e) e.printS

19、tackTrace();(拷貝成功!);3 、創(chuàng)建 c:/test.txt 文件并在其中輸入 hello world創(chuàng)建一個(gè)輸入流讀取該文件中的文本并且把小寫的 l 變成大寫 L 再利用輸出流寫入到 d:test.txt 中實(shí)現(xiàn)步驟: 1. 在本地硬盤 C 盤下創(chuàng)建一個(gè)文件test.txt2 .創(chuàng)建一個(gè)包含main() 方法的類,并在main 中編寫代碼3 運(yùn)行代碼并且測(cè)試結(jié)果實(shí)現(xiàn)過濾器的功能效果顯示:package 作業(yè) ;import ;import ;import ;import ;import ;import ;/* 替換文件中的小寫字母l 為大寫字母L*/ public class

20、Test3 public static void main(String args) FileReader f;String s = ;BufferedReader br = null;try f = new FileReader(F:test.txt);br = new BufferedReader(f);s = br.readLine();(源文件 + s);s = s.replace(l, L); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace();

21、finally try br.close(); catch (IOException e) e.printStackTrace();BufferedWriter bw = null;(s);try FileWriter fw = new FileWriter(F:test.txt); bw = new BufferedWriter(fw);bw.write(s); catch (IOException e) e.printStackTrace(); finally try bw.close(); catch (IOException e) e.printStackTrace();d:/io/s

22、tudent.txt 文件Student 類的描述如4 、在程序中創(chuàng)建一個(gè)Student 類型的對(duì)象,并把對(duì)象信息保存到中,然后再從文件中把Student 對(duì)象的信息讀出顯示在控制臺(tái)上,下:package 作業(yè) ;import java.util.Date;public class Student private int id ;private String name ;private Date birth ;public Student() super ();public Student( int id , String name , Date birth ) super ();this .

23、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 = bir

24、th ;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); ObjectOutputStre

25、am oos = new ObjectOutputStream(fos); (對(duì)象序列化.);oos.writeObject(s1);oos.flush(); oos.close(); catch (FileNotFoundException e1) (e1.getMessage(); catch (IOException e2) (e2.getMessage();try FileInputStream fis = new FileInputStream(d:iostudent.txt); ObjectInputStream ois = new ObjectInputStream(fis);(

26、反序列化.);Student ss1 = (Student)ois.readObject();(ss1);ois.close(); catch (FileNotFoundException e) (e.getMessage(); catch (IOException e) (e.getMessage(); catch (ClassNotFoundException e) (e.getMessage();多線程1 、利用 Thread 實(shí)現(xiàn),要求多線程求解某范圍素?cái)?shù)每個(gè)線程負(fù)責(zé)1000 范圍:線程1 找 11000 ;線程 2 找 1001-2000 ;線程 3 找 2001-3000 。編程程

27、序?qū)⒚總€(gè)線程找到的素?cái)?shù)及時(shí)打印。package 找素?cái)?shù) ;public class Number private int num ;public Number() super ();public Number( int num ) super ();this .num = num ;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

28、= 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 + 是質(zhì)數(shù));if (num = 2000) 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 .prin

29、tln( n + 是質(zhì)數(shù)); if (num = 3000) for (int n = 2001; n = num ; n+) boolean b = trueif (n != 1) for (int i = 2; i n; i+) if (n % i = 0) b = false ;break ;if (b) System. out .println( n + 是質(zhì)數(shù) );package 找素?cái)?shù) ;/public class Numbers1 implements Runnablepublic class Numbers1 extends Thread private Number num

30、;public Numbers1(Number num ) this .num = num ;public void run() num .checkNum(1000);package 找素?cái)?shù) ;/public class Numbers1 implements Runnablepublic class Numbers2 extends Thread private Number num ;public Numbers2(Number num ) this .num = num ;public void run() num .checkNum(2000);package 找素?cái)?shù) ;/publi

31、c 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 找素?cái)?shù) ;public class Test public static void main(String args ) Number num = new Number();Numbers1 n1 = new Numbers1(

32、 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( n3 ); t3.setName( 20013000 ); t3 .start();2、利用 Runnable 實(shí)現(xiàn),要求多線程求解某范圍素?cái)?shù)

33、每個(gè)線程負(fù)責(zé)1000 范圍:線程1 找1-1000 ;線程 2 找 1001-2000 ;線程 3 找 2001-3000 。編程程序?qū)⒚總€(gè)線程找到的素?cái)?shù) 及時(shí)打印。與第一題相同,不再貼出代碼,第一題注釋即為修改處。3 、編寫一個(gè)Java 程序(包括一個(gè)主程序類,一個(gè)線程類。在主程序類中創(chuàng)建2 個(gè)線程,將其中一個(gè)線程的優(yōu)先級(jí)設(shè)為 10 ,另一個(gè)線程的優(yōu)先級(jí)設(shè)為6。讓優(yōu)先級(jí)為10 的線程打印200 次“線程 1 正在運(yùn)行”,優(yōu)先級(jí)為 6 的線程打印200 次“線程 2 正在運(yùn)行”。package 優(yōu)先級(jí) ;public class Main private int num ;public int

34、 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 )正在運(yùn)for (int i =0; i num ;i +)System. out .println(Thread. currentThread ().getName()+ 行);package 優(yōu)先級(jí) ;public class Xiancheng1 impl

35、ements Runnable private Main num ;public Xiancheng1() super ();public Xiancheng1(Main num ) this .num = num ;Overridepublic void run() num .dayin(200); package 優(yōu)先級(jí) ;public class Xiancheng2 implements Runnable private Main num ;public Xiancheng2() super ();public Xiancheng2(Main num ) this .num = num ;Overridepublic void run() num .dayin(200);package 優(yōu)先級(jí) ;public class Test public static void main(String args ) Main num =new Main();Xianch

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論