JAVA高級練習(xí)題_第1頁
JAVA高級練習(xí)題_第2頁
JAVA高級練習(xí)題_第3頁
JAVA高級練習(xí)題_第4頁
JAVA高級練習(xí)題_第5頁
已閱讀5頁,還剩18頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、1、編寫一個程序,實(shí)現(xiàn)從命令行參數(shù)輸入兩個字符串類型的數(shù)值,并計算輸出兩個數(shù)值的和。2、編寫一個程序,實(shí)現(xiàn)從命令行參數(shù)輸入一字符串,統(tǒng)計該字符串中字符“此現(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

2、.length(); i+) if (str .charAt( i)= 'e') count +;System. out .println( "e 的出現(xiàn)次數(shù)是:"+ count );3、生成十個0100之間的隨機(jī)數(shù),放到數(shù)組中,然后排序輸出。package zuoye;import java.util.Arrays;/* 生成十個0100之間的隨機(jī)數(shù),放到數(shù)組中,然后排序輸出* author Administrator* /public class Test3 public static void main(String args) int arr= ne

3、w int 10; /定義一個長度為10的迎數(shù)組 for (int i = 0; i < arr.length ; i+) arri=( int ) (Math. random ()*100); /取01 的隨機(jī)數(shù) *100 ,即為 0100 的隨機(jī)數(shù)Arrays. sort (arr);排序方法for (int a :arr)/ 增強(qiáng) for 循環(huán) System. out .print( a + "");4、巴黎時間比北京時間晚7個小時,紐約時間比北京時間晚12個小時,試編寫一程序,根據(jù)輸入的北京時間輸出相應(yīng)的巴黎和紐約時間。package zuoye;import

4、 java.util.Calendar;import java.util.Date;public class Test4 public void getTime(int y,int m,int d,int h,int mi,int s)/獲取Calendar實(shí)例Calendar time = Calendar.getInstance();/設(shè)置巴黎時間time.set(y,m,d,h-7,mi,s);/輸出巴黎時間System.out.println("巴黎時間是"+time.get(Calendar.YEAR)+""+ (time.get(Calend

5、ar.MONTH)+"月"+ time.get(Calendar.DAY_OF_MONTH)+"日"+ time.get(Calendar.HOUR_OF_DAY)+'時”+ time.get(Calendar.MINUTE)+"分"+ time.get(Calendar.SECOND盧秒");/設(shè)置紐約時間time.set(y,m,d,h-12,mi);/輸出紐約時間System.out.println("紐約時間是"+time.get(Calendar.YEAR)+""+

6、(time.get(Calendar.MONTH)+"月"+ time.get(Calendar.DAY_OF_MONTH)+"日"+ time.get(Calendar.HOUR_OF_DAY)+'時"+ time.get(Calendar.MINUTE)+"分"+time.get(Calendar.SECOND)+'秒)public static void main(String args) Test4 time = new Test4();/設(shè)置北京時間time.getTime(2018,3,15,16

7、,53,9);) )5、解析一個郵箱地址是否合法,如果合法則打印出用戶名部分和該郵箱所屬的網(wǎng)站域名如果郵箱地址不合法則顯示不合法的原因提示:郵箱地址不合法的因素:1)郵箱地址中不包含 或。2)郵箱地址中含有多了 或。3)郵箱地址中。出現(xiàn)在 的前面4)用戶名里有其他字符實(shí)現(xiàn)步驟:(1)創(chuàng)建一個類,類名: mailtest類圖如下:mailtest+testmail() : Boolean(類名和方法名必須與要求一樣。區(qū)分大小寫)package youxiang;import java.util.Scanner;public class MailTest public static boolean

8、 testMail()Scanner input = new Scanner(System. in);String s = input .next();if (s.indexOf( "")=-1| s.indexOf( ".")=-1) System. out .println("郵箱地址中不包含 或."); return false ;)if (s.indexOf( "" )!= s.lastIndexOf( "" )|s.indexOf( ".")!= s.lastInd

9、exOf( ".") System. out .println("郵箱地址中含有多余的 或.");return falseif (s.indexOf( "" )> s.lastIndexOf( ".") (System. out .println("郵箱地址中.出現(xiàn)在的前面"); return false ;)for (int i=0; i< s.indexOf( "");i+) (if ( (s.charAt( i)>= 'a'&&

10、amp; s.charAt( i)<= 'z')|(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-generated method stubif (Ma

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

12、em. in);String a = input .next();String b = input .next();int count = 0; / 計數(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、請使用LinkedList來模擬一個隊列(先進(jìn)先出的特性):1)擁有放入對象的方法void put(Object o)2)取出對象的方法 Object get()3)判斷隊列

13、當(dāng)中是否為空的方法boolean isEmpty();并且,編寫測試代碼,驗(yàn)證你的隊列是否正確。public class Linkeds List l ;Linkeds()l =new LinkedList();)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)/定義一個空隊列Li

14、nkeds l = new Linkeds(); /往隊列中放入對象 l.put("Tom1");l.put("John2");l.put("Mary3");/如果隊列不為空,依次輸出隊列中的元素while(!l.isEmpty()System.out.println(l.get(); )2、假設(shè)順序列表 ArrayList中存儲的元素是整型數(shù)字15,遍歷每個元素,將每個元素順序輸出。package zuoye;import java.util.ArrayList;import java.util.Iterator;import ja

15、va.util.List;public class Test1 public static void main(String口 args) List<Number> 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(nl);list.add(n2);list.add(n3);list.add(n4);list.add(n

16、5);Iterator it=list.iterator();while(it.hasNext() System.out.print(it.next();3、在一個歹U表中存儲以下元素:apple,grape,banana,pear1)返回集合中的最大的和最小的元素2)將集合進(jìn)行排序,并將排序后的結(jié)果打印在控制臺上package zuoye;public class Fruit private String fruit ;public Fruit() ) public Fruit(String fruit ) this .fruit =fruit; )public String getFrui

17、t() return fruit ;)public void setFruit(String fruit ) this .fruit = fruit)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("

18、");System.out.println("最大值是:"+ Collections.max(fruit);System.out.println("最小值是:"+ Collections.min(fruit);)(其中儲戶的主要信4、編寫一個程序,創(chuàng)建一個HashMap對象,用于存儲銀行儲戶的信息息有儲戶的ID,姓名和余額)。另外,計算并顯示其中某個儲戶的當(dāng)前余額。package zuoye;public class Bank privateString id;privateString name ;private double money ;

19、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 setId(String id ) this .id = id;public String getName() return name ;)public void setName(String name ) this .name = name ;)public

20、double getMoney() return money ;)public void setMoney( double money ) this .money = money ;)package zuoye;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class BankTest public static void main(String args) Map<String,Bank> map=new HashMap<

21、String,Bank>();map.put("用戶map.put("用戶map.put("用戶map.put("用戶1", new Bank("1","張三",Math.random()*100000);2”, new Bank("2","李四",Math.random()*100000);3”, new Bank("3","王五",Math.random()*100000);4", new Bank(&qu

22、ot;4","趙六",Math.random()*100000);Set keySet=map.keySet();Iterator it = keySet.iterator();double sum = 0;while(it.hasNext()Object key = it.next();Bank a = map.get(key);System.out.println("賬戶"+a.getId()+”的余額:"+a.getMoney();/計算所有帳戶的余額之和sum += a.getMoney();)System.out.print

23、ln("所有賬戶余額:"+sum);)5、從控制臺輸入若干個單詞(輸入回車結(jié)束)放入集合中,將這些單詞排序后(忽略大小 寫)打印出來。package zuoye;import java.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<String> list = new ArrayList&

24、lt;String>();String str = input.nextLine();String口 arr = str.split("");String tmp;for (int i = 0; i < arr.length; i+) for (int j = i + 1; j < arr.length - 1; j+) if (pareToIgnoreCase(arrj) > 0) str = arrj;arrj = arrj + 1;arrj + 1 = str;)for (String a : arr) list.add(a);

25、System.out.println(a);)IO流1、在本機(jī)的磁盤系統(tǒng)中,找一個文件夾,利用 File類的提供方法,列出該文件夾中的所有 文件的文件名和文件的路徑,執(zhí)行效果如下:-路徑名:c:tempdef.txtpackage 作業(yè);import java.io.File;public class Test1 public static void main(String口 args ) File file = new File(新建文件夾");String口 files = file .list();for (String o : files ) System. out .pri

26、ntln("文件名:"+ o);System. out .println("路徑:"+ file .getAbsolutePath() +"" + o);System. out .println(""); 2、編寫一個java程序?qū)崿F(xiàn)文件復(fù)制功能,要求將 d:/io/copysrc.doc中的內(nèi)容復(fù)制到d:/io/copydes.doc 中。package 作業(yè);import java.io.File;import java.io.FileInputStream;import java.io.FileOutputS

27、tream;/*文件的拷貝*/public class Test2 public static void main(String args) File file = new File("fileabc.txt");File file_back = new File("fileabc_back.txt");try (FileInputStream fi = new FilelnputStream(file); FileOutputStream fo = new FileOutputStream(file_back);) int b = -1;while (

28、b = fi.read() != -1) fo.write(b); catch (Exception e) e.printStackTrace();System.out.println("拷具成功!");3、創(chuàng)建c:/test.txt文件并在其中輸入"hello world"創(chuàng)建一個輸入流讀取該文件中的文本并且把小寫的l變成大寫L再利用輸出流寫入到d:test.txt中實(shí)現(xiàn)步驟:1.在本地硬盤C盤下創(chuàng)建一個文件test.txt2 .創(chuàng)建一個包含 main()方法的類,并在 main中編寫代碼3 .運(yùn)行代碼并且測試結(jié)果實(shí)現(xiàn)過濾器的功能效果顯示:=、;臥打偽

29、 '31, o J - Q -PuL>lem 匕期力日皿 DdUlEi 1 口 Consrile«tarnnn=tedA tasl: 口自卡總內(nèi)口plication Ei H5gdkl.%N08Msdkli*N_06Ub<Zj*v 從文蚪亡! /text H七乂七串讀瓦乂套口內(nèi)容是/ h總工JLc wor J_elpackage 作業(yè);import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.File

30、Reader;import java.io.FileWriter;import java.io.IOException;/*替換文件中的小寫字母l為大寫字母L*/ public class 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();System.ou

31、t.println("源文件"+ s);s = s.replace("l", "L"); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); finally try br.close(); catch (IOException e) e.printStackTrace();BufferedWriter bw = null;System.out.println(s);try FileWriter f

32、w = 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();)4、在程序中創(chuàng)建一個Student類型的對象,并把對象信息保存到d:/io/student.txt文件中,然后再從文件中把 Student對象的信息讀出顯示在控制臺上,Student類的描述如下:package

33、 作業(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 .id = id ;this .name = name ;this .birth = birth ;)public int getId() return id ;)public void setId( int

34、 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(Datebirth ) this .birth = birth ;)Overridepublic String toString() return "Student id="+ id + ", name=" + na

35、me + ", 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");ObjectOutputStream

36、oos = new ObjectOutputStream(fos);System.out.println("對象序列化."); oos.writeObject(s1);oos.flush();oos.close(); catch (FileNotFoundException e1) System.out.println(e1.getMessage(); catch (IOException e2) System.out.println(e2.getMessage(); try FileInputStream fis = new FileInputStream("d

37、:iostudent.txt"); ObjectInputStream ois = new ObjectInputStream(fis);System.out.println("反序列化.");Student ss1 = (Student)ois.readObject();System.out.println(ss1);ois.close(); catch (FileNotFoundException e) System.out.println(e.getMessage(); catch (IOException e) System.out.println(e.g

38、etMessage(); catch (ClassNotFoundException e) System.out.println(e.getMessage();多線程1、利用Thread實(shí)現(xiàn),要求多線程求解某范圍素數(shù)每個線程負(fù)責(zé)1000范圍:線程1找1-1000 ;線程2找1001-2000;線程3找2001-3000。編程程序?qū)⒚總€線程找到的素數(shù)及時打印。package 找素數(shù);public class Number private int num ;public Number() super ();public Number( int num ) super ();this .num =

39、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 <= 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 .printl

40、n( 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 .println( n + "是質(zhì)數(shù)");)if (num = 3000) for (int n = 2001; n <= num ; n +) boolean b = true ;if

41、 (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找素數(shù);/public class Numbersl implements Runnable public class Numbersl extends Thread private Number num ;public Numbers1(Number num ) this .num = num ;)public void run(

42、) num .checkNum(1000);)package找素數(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找素數(shù);/public class Numbers1 implements Runnablepublic class Numbers3 extend

43、s Thread private Number num ;public Numbers3(Number num ) this .num = num ;)public void run() num .checkNum(3000);)package 找素數(shù);public 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:&q

44、uot; ); 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),要求多線程求解某范圍素數(shù)每個線程負(fù)責(zé)1000范圍:線程1找1-1000;線程2找10

45、01-2000;線程3找2001-3000。編程程序?qū)⒚總€線程找到的素數(shù)及時打印。與第一題相同,不再貼出代碼,第一題注釋即為修改處。2個線程,將3、編寫一個Java程序(包括一個主程序類,一個線程類。在主程序類中創(chuàng)建其中一個線程的優(yōu)先級設(shè)為10,另一個線程的優(yōu)先級設(shè)為6。讓優(yōu)先級為10的線程打印200次“線程1正在運(yùn)行”,優(yōu)先級為6的線程打印200次“線程2正在運(yùn)行”。package 優(yōu)先級;public class Main private int num ;public int getNum() return num ;public void setNum( int num ) this

46、.num = num ;public Main() super ();public Main( int num ) super ();this .num = num ;public void dayin( int num )for (int i=0; i< num ;i+)System. out .println(Thread. currentThread ().getName()+"正在運(yùn)行”);package 優(yōu)先級;public class Xiancheng1 implements Runnable private Main num ;public Xiancheng1() super ();public Xiancheng1(Main num ) this .num = num ;Override public void run() num .

溫馨提示

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

最新文檔

評論

0/150

提交評論