




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上第六章 Java輸入輸出1、說明程序功能:import java.io.*;public class CharWrite public static void main(String args) try FileWriter fw=new FileWriter("charset.txt "); for ( int i=32;i<126;i+) fw.write(i); fw.close(); catch (IOException e) 答:向文件charset.txt中輸入ASCII碼從32到126共94個(gè)字符。2、先用隨機(jī)函數(shù)產(chǎn)生出任意的2
2、0個(gè)整數(shù),再按由小到大的順序排序,然后將結(jié)果寫入一個(gè)文件中,最后從該文件中讀出后顯示出來。請(qǐng)分別用順序文件和隨機(jī)文件的讀寫形式進(jìn)行編程測(cè)試。代碼:import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;im
3、port java.io.OutputStream;import java.util.Random;public class RandNum public static void main(String args) Random rand = new Random();/實(shí)例化一個(gè)產(chǎn)生隨機(jī)數(shù)的類 int num = new int20;/定義一個(gè)整型數(shù)組,用來存儲(chǔ)產(chǎn)生的隨機(jī)數(shù) int r = 0;/隨機(jī)數(shù) for(int i = 0;i<num.length;i+) r = rand.nextInt(1000);/產(chǎn)生一個(gè)1000以內(nèi)的隨機(jī)數(shù) /放入數(shù)組 numi = r; int k;
4、 /進(jìn)行排序 for(int i = 0;i<num.length;i+) for(int j = 0;j<i;j+) if(numi < numj) k = numi; numi = numj; numj = k; RandNum rn = new RandNum(); /寫入文件 rn.WriteFile("src/123",num); /讀取文件 int x = rn.ReadFile("src/123"); /讀取的數(shù)組 for(int i = 0;i<num.length;i+) System.out.print(xi
5、+ "t"); if(i%5 = 4) System.out.println(); /* * 讀取文件的方法 * param path */ public int ReadFile(String path) int a = null; try / 實(shí)例化一個(gè)File輸入流對(duì)象InputStream is = new FileInputStream(path);/ 實(shí)例化一個(gè)Buffer輸入流對(duì)象BufferedInputStream bs = new BufferedInputStream(is);int length = bs.available();/讀取第一個(gè)為數(shù)組大
6、小int k = bs.read();/用來計(jì)數(shù)int count = 0;/實(shí)例化數(shù)組a = new intk;String str = ""/中間變量for(int i = 0;i<length;i+)/繼續(xù)讀取k = bs.read();/若讀取的不是換行if(k != 13 && k != 10 && k != -1)/讀取到空格while(k != 32)str += (char)k;k = bs.read();/把字符串里的數(shù)字轉(zhuǎn)換成整型放入數(shù)組中acount = Integer.parseInt(str);count+;s
7、tr = ""/清空字符串is.close();bs.close();System.out.println("讀取成功"); catch (FileNotFoundException e)System.out.println("找不到指定文件,請(qǐng)確認(rèn)文件路徑"); catch (IOException e) e.printStackTrace(); return a; /* * 寫入文件 * param path 路徑 * param a 數(shù)組 */ public void WriteFile(String path,int a)tr
8、y / 實(shí)例化一個(gè)File輸出流OutputStream os = new FileOutputStream(path);/ 實(shí)例化一個(gè)Buffered輸出流BufferedOutputStream bos = new BufferedOutputStream(os);/寫入數(shù)組大小bos.write(a.length);/寫入換行符bos.write(13);bos.write(10);/循環(huán)寫入數(shù)組的值for(int i = 0;i<a.length;i+)bos.write(Integer.toString(ai).getBytes();/寫入1個(gè)空格bos.write(32);b
9、os.flush();bos.close();os.close();System.out.println("寫入成功"); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 截圖3、統(tǒng)計(jì)一個(gè)文本文件中單詞的個(gè)數(shù)。文本文件的名字從命令行中獲得。代碼import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;public cla
10、ss test public static void main(String args) throws IOException FileReader fileReader=new FileReader(args0); StringBuffer stringBuffer=new StringBuffer(); char b=new char506; while(fileReader.read(b,0,505)!=-1) stringBuffer.append(b); StringTokenizer stringTokenizer=new StringTokenizer(stringBuffer.
11、toString(); System.out.println("文本文件中單詞的個(gè)數(shù)為:"+stringTokenizer.countTokens(); 結(jié)果截圖第七章 Java圖形用戶界面1、閱讀下面的程序,回答問題。 import java.awt.*; import javax.swing.*; public class T extends JFrame public T ( ) super("GridLayout"); Container con=this.getContentPane(); con.setLayout(new GridLayou
12、t(2,3); con.add(new JButton("a"); con.add(new JButton("b"); con.add(new JButton("c"); con.add(new JButton("d"); con.add(new JButton("e"); con.add(new JButton("f"); setSize(200, 80); setVisible(true); public static void main(String args) new
13、 T(); 畫圖表示程序運(yùn)行后的圖形界面。如果程序通過實(shí)現(xiàn)某個(gè)接口處理按鈕的動(dòng)作事件,則該接口名為何?接口中的方法頭聲明如何?答:實(shí)現(xiàn)的接口是ActionListener,該接口的方法聲明是public void actionPerformed(ActionEvent e)2、編寫一個(gè)簡(jiǎn)單的計(jì)算器,要求圖形用戶界面如下圖所示。代碼import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionL
14、istener;import java.util.HashSet;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class jisuanqi public static void main(String args) new MyCalculator(); class MyCalculator private double result=0.0; private String string=""
15、; private JTextField jtext = new JTextField("",15); String str=new String"Clear","+","0","1","2","3","-","*",".","4","5","6","/","%","=",&qu
16、ot;7","8","9" public MyCalculator() JFrame frame = new JFrame("calculator"); frame.setBounds(400,200,450,250); JPanel jp = new JPanel(new GridLayout(3,6,10,10); frame.setLayout(new BorderLayout(); frame.add(jtext,BorderLayout.NORTH); frame.add(jp,BorderLayout.CENTE
17、R); final HashSet hashSet=new HashSet(); class MyEvent implements ActionListener public void actionPerformed(ActionEvent e) for(Object k:hashSet) if(e.getSource()=k) JButton jb=(JButton)k; if(jb.getText()="Clear") jtext.setText(""); else jtext.setText(jtext.getText()+jb.getText()
18、; if(jb.getText()="=") char c=jtext.getText().toCharArray(); int i; for(i=0;i<c.length;i+) if(ci='+'|ci='-'|ci='*'|ci='/'|ci='%') break; String astr=String.copyValueOf(c, 0, i); String bstr=String.copyValueOf(c,i+1,c.length-i-2); MyCompute mycompu
19、te = new MyCompute(); result=pute(Double.parseDouble(astr),Double.parseDouble(bstr),ci); jtext.setText(""+result); for(int i=0;i<18;i+) JButton b=new JButton(stri); jp.add(b); b.addActionListener(new MyEvent(); hashSet.add(b); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.
20、setVisible(true); class MyCompute private double result=0.0; public MyCompute() public double compute(double a,double b,char c) if(c='+') result=a+b; if(c='-') result=a-b; if(c='*') result=a*b; if(c='/') result=a/b; if(c='%') result=a%b; return result; 3. 編程題:
21、請(qǐng)編寫一個(gè)簡(jiǎn)單的用戶登錄程序。要求使用圖形用戶界面,* 用戶名和密碼假定均為java,且密碼輸入時(shí)全部顯示為星號(hào)(*)。 */代碼public class TheExam7_3 public static void main(String args) MyLand my=new MyLand(); class MyLand int a=0; public MyLand() JFrame frame = new JFrame(); frame.setBounds(450,300,370,220); Font font=new Font("宋體",Font.PLAIN,30);
22、 frame.setFont(font); frame.setLayout(new BorderLayout(); JPanel paneltop = new JPanel(); paneltop.add(new JLabel("用戶登陸",JLabel.CENTER); frame.add(paneltop,BorderLayout.NORTH); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel_1 = new JPanel(new Gri
23、dLayout(3,1); frame.add(panel_1,BorderLayout.CENTER); JPanel panel_11 = new JPanel(new FlowLayout(); JPanel panel_12 = new JPanel(new FlowLayout(); JPanel panel_13 = new JPanel(new GridLayout(1,5); panel_1.add(panel_11); panel_1.add(panel_12); panel_1.add(panel_13); panel_11.add(new JLabel("用戶名
24、:"); final JTextField text1 = new JTextField("",10); panel_11.add(text1); panel_12.add(new JLabel("用戶密碼:"); final JPasswordField text2 = new JPasswordField("",10); text2.setEchoChar('*'); panel_12.add(text2); JButton b=new JButton("確定"); class MyE
25、xam7_3 implements ActionListener public void actionPerformed(ActionEvent e) JDialog dialog=new JDialog(); dialog.setBounds(450, 200,560,400); dialog.setVisible(true); Font font1 = new Font("宋體",Font.BOLD,70); dialog.setFont(font1); if(text1.getText().equalsIgnoreCase("java")&
26、&String.valueOf(text2.getPassword().equals("java") dialog.add(new JLabel("登陸成功!",JLabel.CENTER); / new MyCalculator(); else dialog.add(new JLabel("密碼或用戶名錯(cuò)誤!",JLabel.CENTER); b.addActionListener(new MyExam7_3(); panel_13.add(new Label(" "); panel_13.add(new
27、 Label(" "); panel_13.add(b); panel_13.add(new Label(" "); panel_13.add(new Label(" "); 結(jié)果截圖第八章 Java多線程技術(shù)1、程序如下:public class Borley extends Threadpublic static void main(String argv) Borley b = new Borley(); b.start(); public void run() System.out.println("Running&
28、quot;); 下面描述正確的是:(B)單選A) 通過編譯和運(yùn)行但是沒有任何輸出。B) 通過編譯,運(yùn)行后輸出"Running"C) 編譯出錯(cuò),沒有線程可供運(yùn)行D) 編譯出錯(cuò),沒有權(quán)限使用Thread2、 編寫程序,啟動(dòng)1000個(gè)線程,每個(gè)線程給初值為0的變量sum加1。(提示,需要通過引用傳遞,將sum傳遞給每個(gè)線程,為了能夠進(jìn)行引用傳遞,需要定義一個(gè)Integer包裝對(duì)象來保存sum)。代碼public class Borley extends Threadpublic static void main(String argv)/通過Integer包裝對(duì)象sumInteg
29、er sum = new Integer(0);/循環(huán)1000次,開啟1000個(gè)線程for(int i = 0;i<1000;i+)Borley b = new Borley(sum);b.start();private Integer sum;public Borley(Integer sum)this.sum = sum;public void run() sum+;System.out.println(sum);截圖3、利用多線程技術(shù)模擬龜兔賽跑的場(chǎng)面。提示:先設(shè)計(jì)一個(gè)線程類模擬兩個(gè)參與賽跑的選手的行為;然后創(chuàng)建該類的兩個(gè)對(duì)象分別代表烏龜和兔子;讓兔子跑快些,但在路上睡眠休息時(shí)間長(zhǎng)
30、些,而烏龜跑慢些卻不休息;當(dāng)某個(gè)選手到達(dá)終點(diǎn)時(shí)其線程運(yùn)行結(jié)束;二個(gè)選手均到達(dá)終點(diǎn)時(shí),顯示并祝賀獲勝選手,然后主線程結(jié)束。代碼Import java.util.Date;public class xiancheng extends Thread private int tortoise_walk = 0; / 烏龜已跑長(zhǎng)度存放變量 private int rabbit_walk = 0; / 兔子已跑長(zhǎng)度存放變量 private int finish = 1000; / 終點(diǎn) private volatile boolean hasWinner = false;/ 勝利者誕生 /* class
31、Tortoise_Run implements Runnable public void run() try while (!hasWinner) if (tortoise_walk % 100 = 0 && (tortoise_walk != 0|tortoise_walk>=finish) /烏龜每100米休息500毫秒 Thread.sleep(500); tortoise_walk+; System.out.println("烏龜已跑"+tortoise_walk+"米"); catch (InterruptedException e) e.printStackTrace(); class Rabbit_Run implements Runnable public void run() try while (!hasWinner) if (rabbit_walk % 20 = 0 && (rabbit_walk != 0|rabbit_walk>=finish) /兔子每20米休息500毫秒 System.out.pr
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 西華大學(xué)《數(shù)值計(jì)算》2023-2024學(xué)年第二學(xué)期期末試卷
- 江陰職業(yè)技術(shù)學(xué)院《計(jì)算機(jī)操作系統(tǒng)》2023-2024學(xué)年第二學(xué)期期末試卷
- 包頭鋼鐵職業(yè)技術(shù)學(xué)院《國際會(huì)展實(shí)務(wù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 鶴崗師范高等??茖W(xué)校《操作系統(tǒng)原理與應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 遼寧廣告職業(yè)學(xué)院《中小學(xué)音樂教學(xué)設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 內(nèi)蒙古商貿(mào)職業(yè)學(xué)院《生化工程》2023-2024學(xué)年第二學(xué)期期末試卷
- 浙江音樂學(xué)院《管理學(xué)全英》2023-2024學(xué)年第二學(xué)期期末試卷
- 河北機(jī)電職業(yè)技術(shù)學(xué)院《國際貿(mào)易概論》2023-2024學(xué)年第二學(xué)期期末試卷
- 成都師范學(xué)院《工程測(cè)量課程設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 范梅南現(xiàn)象學(xué)教育學(xué)理論體系
- 《埃菲爾鐵塔》課件
- 形象設(shè)計(jì)概論試題及答案
- 紅細(xì)胞生成素靶向治療策略-全面剖析
- 寧夏銀川市2023?2024學(xué)年高一下學(xué)期期中考試 數(shù)學(xué)試卷(含解析)
- 浙江浙達(dá)環(huán)境科技有限公司年收集、貯存及轉(zhuǎn)運(yùn)危險(xiǎn)廢物5000噸的搬遷項(xiàng)目環(huán)評(píng)報(bào)告
- 2025年留置輔警筆試真題及答案
- 不同來源硫酸軟骨素的化學(xué)結(jié)構(gòu)、抗氧化與降脂活性對(duì)比
- 抗凝劑皮下注射技術(shù)臨床實(shí)踐指南(2024版)解讀
- 小學(xué)政治 (道德與法治)人教部編版二年級(jí)下冊(cè)14 學(xué)習(xí)有方法教學(xué)設(shè)計(jì)
- 2024年全球及中國一次性喉鏡片和手柄行業(yè)頭部企業(yè)市場(chǎng)占有率及排名調(diào)研報(bào)告
- 湖南張家界事業(yè)單位招聘考試高頻題庫帶答案2025年
評(píng)論
0/150
提交評(píng)論