itatjava第三屆、第五屆復(fù)賽答案_第1頁
itatjava第三屆、第五屆復(fù)賽答案_第2頁
itatjava第三屆、第五屆復(fù)賽答案_第3頁
itatjava第三屆、第五屆復(fù)賽答案_第4頁
itatjava第三屆、第五屆復(fù)賽答案_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、操作題:(請(qǐng)將編寫好的源程序以題號(hào)命名,例如第1題的源程序保存為“1.java” )1、 編寫一個(gè)Java應(yīng)用程序,計(jì)算并輸出一維數(shù)組(9.8,12,45,67,23,1.98,2.55,45)中的最大值和最小值。(本題20分) public class itat1 public static void main(String ags) double a = 9.8, 12, 45, 67, 23, 1.98, 2.55, 45 ;double max = a0;double min = a0;for (int i = 0; i < a.length; i+) if (ai > m

2、ax) max = ai;if (ai < min) min = ai;System.out.println("最大值為:"+max);System.out.println("最小值為:"+min);2、 編寫一個(gè)Java應(yīng)用程序,該程序使用FileInputStream類,實(shí)現(xiàn)從磁盤讀取本應(yīng)用程序源代碼文件,并將文件內(nèi)容顯示在屏幕上。(本題20分)import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class itat2

3、 public static void main(String ags) throws IOException BufferedReader in = new BufferedReader(new FileReader("d:/hello.txt");String s;while (s = in.readLine() != null) System.out.println(s);讀取文件本身的類Ex1.java的源代碼如下:*import java.io.BufferedReader;import java.io.File;import java.io.FileReader

4、;/* * author Godwin * version 2010-05-16 */public class Ex1 public static void main(String args) throws Exception BufferedReader br = new BufferedReader(new FileReader(new File(System .getProperty("user.dir") + File.separator + "Ex1.java"); String s; while (s = br.readLine() != n

5、ull) System.out.println(s); *3、 編寫一個(gè)Java應(yīng)用程序,利用RandomAccessFile類,把幾個(gè)int型整數(shù)(1,2,3,4,5,6,7,8,9,10)寫入到一個(gè)名字為tom.dat文件中,然后按相反順序讀出這些數(shù)據(jù)并顯示在屏幕上。(注意,一個(gè)int型數(shù)據(jù)占4個(gè)字節(jié))(本題30分)package itat;import java.io.File;import java.io.RandomAccessFile;public class qq public static void main(String args) throws Exception Stri

6、ng directory = "D:/test123"String name = "tom.dat"File f = new File(directory, name);RandomAccessFile file = null;file = new RandomAccessFile(f, "rw");int a = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;for (int i = 0; i < a.length; i+) file.writeInt(ai);RandomAccessFile file1 =

7、new RandomAccessFile(f, "rw");for (int i = 0; i < a.length; i+) int j = file1.readInt();System.out.println(j);4、 編寫一個(gè)Java GUI應(yīng)用程序,采用Java多線程技術(shù),模擬自由落體和平拋運(yùn)動(dòng):一個(gè)球自由落下,一個(gè)球水平拋出。(本題30分)(自由落體物理公式:h= g *t2/2 ;平拋運(yùn)動(dòng)物理公式:h= g *t2/2 ,x=26*t ;h代表高度,t代表時(shí)間,g代表重力加速度=9.8 m/s2 )附加題:5、 編寫一個(gè)Java應(yīng)用程序,實(shí)現(xiàn)如下功能:1

8、) 判斷兩個(gè)字符串是否相同,s1=”you are a student”,s2=”how are you”;2) 判斷字符串”220302”的前綴、后綴是否和某個(gè)字符串”220302”相同;3) 按字典順序比較兩個(gè)字符串”你”和”我”的大小關(guān)系;4) 將數(shù)字型字符串”100”和”123.678”轉(zhuǎn)換為數(shù)字;5) 將字符串”FEDCBA”存放到數(shù)組中,調(diào)用for循環(huán)讀出數(shù)組數(shù)據(jù)顯示在屏幕上。 (本題20分)public class aa public static void main(String ags)String s1="you are a student",s2=&q

9、uot;how are you"if(s1.equals(s2)System.out.println("s1和s2相同。");elseSystem.out.println("s1和s2不同。");String s3="220302" String s4="220302" if(s3.startsWith(s4) System.out.println(s3+"的前綴為"+s4); else System.out.println(s3+"的前綴不為"+s4); if(s

10、3.endsWith(s4) System.out.println(s3+"的后綴為"+s4); else System.out.println(s3+"的后綴不為"+s4); String s5="你" String s6="我" if(pareTo(s6)<0) System.out.println("""+s5+""比""+s6+""大"); else if(pareTo(s6)=0) System.ou

11、t.println("""+s5+""和""+s6+""等"); else System.out.println("""+s5+""比""+s6+""小"); String s7="100" String s8="123.678" int num1=Integer.parseInt(s7); Double num2=Double.parseDouble(s8

12、); System.out.println(num1); System.out.println(num2); String s9="FEDCBA" char s99=new char6; for(int i=0;i<s99.length;i+) s99i=s9.charAt(i); System.out.println(s99i); String s92="FEDCBA" char arr=s92.toCharArray(); for (int i = 0; i < arr.length; i+) System.out.println(arr

13、i); 6、 編寫客戶/服務(wù)器程序,客戶端Client.java使用DatagramSocket對(duì)象將數(shù)據(jù)包發(fā)送到服務(wù)器,請(qǐng)求獲取服務(wù)器端的圖像(考生可自選圖像文件)。服務(wù)器端Server.java將圖像文件包裝成數(shù)據(jù)包,并使用DatagramSocket對(duì)象將該數(shù)據(jù)包發(fā)送到客戶端。首先將服務(wù)器端的程序編譯通過,并運(yùn)行起來,等待客戶的請(qǐng)求。(本題30分)程序的運(yùn)行效果如下圖所示:客戶端服務(wù)器端服務(wù)端代碼:package datasocket;import java.io.*;import .*;public class FortuneServer1 extends Thread Datagra

14、mSocket ServerSocket;public FortuneServer1() super("FortuneServer1");try ServerSocket = new DatagramSocket(1121);System.out.println("FortuneServer up and running."); catch (SocketException e) System.err.println("Exception: couldn't create datagram socket");System.ex

15、it(1);public void run() if (ServerSocket = null)return;while (true) try InetAddress address;int port;DatagramPacket packet;byte data = new byte16000;packet = new DatagramPacket(data, data.length);ServerSocket.receive(packet);address = packet.getAddress();port = packet.getPort();File inFile = new Fil

16、e("D:/q1.jpg");FileInputStream inStream = new FileInputStream(inFile);if (inStream.read(data) <= 0) System.err.println("Error: couldn't read fortunes");packet = new DatagramPacket(data, data.length, address, port);ServerSocket.send(packet); catch (Exception e) System.err.p

17、rintln("Exception: " + e);e.printStackTrace();public static void main(String args) FortuneServer1 server = new FortuneServer1();server.start();客戶端代碼:package datasocket;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;impor

18、t java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import .*;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;public class FortuneClient extends JFrame implements ActionListener /* * */private static final long serialVersionUID

19、 = 5440156L;Image image = null;private JButton button = new JButton("獲取圖像");private JLabel label = new JLabel();public FortuneClient() this.setTitle("I am a client");this.setSize(500, 500);this.setLocationRelativeTo(null);this.addWindowListener(new WindowAdapter() public void win

20、dowClosing(WindowEvent e) System.exit(0););this.add(button, "North");button.addActionListener(this);this.setVisible(true);public void getMessage() try DatagramSocket socket;DatagramPacket packet;byte data = new byte16000;socket = new DatagramSocket();packet = new DatagramPacket(data, data.

21、length, InetAddress.getByName("127.0.0.1"), 1121);socket.send(packet);packet = new DatagramPacket(data, data.length);socket.receive(packet);image = Toolkit.getDefaultToolkit().createImage(packet.getData(),0, packet.getData().length);socket.close(); catch (UnknownHostException e) System.err

22、.println("Exception: host could not be found"); catch (Exception e) System.err.println("Exception: " + e);e.printStackTrace();label = new JLabel(new ImageIcon(image);public static void main(String args) new FortuneClient();public void actionPerformed(ActionEvent e) / TODO Auto-ge

23、nerated method stubgetMessage();this.add(label);label.setVisible(true);this.setVisible(true);1、某人有5張3分和4張5分的郵票,請(qǐng)編寫一個(gè)程序,計(jì)算由這些郵票中的1張或若干張可以得到多少種不同的郵資,并按照郵資從小到大順序顯示。(20分) 2、采用Java多線程技術(shù)編寫程序,其中包括兩個(gè)線程:A和B,其中A線程準(zhǔn)備休眠一小時(shí),B線程每隔一秒輸入3句“起床”后,吵醒休眠的線程A。(25分)3、利用Java的GUI編程,編寫一個(gè)窗體,包含兩個(gè)文本框和一個(gè)命令按鈕。其中一個(gè)文本框接收用戶輸入的一行字符串,回

24、車后在另一個(gè)文本框中重復(fù)輸出三行,單擊命令按鈕可清空兩個(gè)文本框的所有內(nèi)容。(25分)4、編寫一個(gè)Java應(yīng)用程序,運(yùn)行后,首先列出當(dāng)前工作目錄,然后把當(dāng)前目錄下面的所有后綴為java的文件取出(設(shè)置一個(gè)過濾器進(jìn)行文件名后綴的過濾)。(30分)附加題:5、使用堆棧結(jié)構(gòu)輸出 ,其中 =2 +2 , =3, =8。(20分)6、訪問Access 2003數(shù)據(jù)庫,在Access數(shù)據(jù)庫中創(chuàng)建學(xué)生表(T_Student)表并配置ODBC驅(qū)動(dòng)源,學(xué)生表的結(jié)構(gòu)和示例數(shù)據(jù)如下所示:T_ Student表:字段名稱說明數(shù)據(jù)類型約束備注S_ID學(xué)號(hào)Integer主鍵S_Name姓名Varchar(10)不允許空S_

25、Email郵箱Varchar(30)S_Score英語成績Integer表數(shù)據(jù)示例:S_IDS_NameS_Email S_Score1001Jerry801002Mike901003John78使用Java語言編寫程序,要求如下:1)使用JDBC-ODBC橋驅(qū)動(dòng)程序;2)查詢出表中所有記錄,并按照主鍵升序顯示。參考提示代碼:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn=DriverManager.getConnection("jdbc:odbc:student","

26、","");(30分)mport java.util.*;public class T1public static void main(String args)ArrayList al = new ArrayList();for(int i=0;i<=5;i+)for(int j=0;j<=4;j+)if(i*3+j*5)!=0 && ! al.contains(i*3+j*5)al.add(i*3+j*5);Collections.sort(al);System.out.println("總共的郵資數(shù)是:"+al.

27、size();System.out.println("他們從小到大的依次書序是:");for(Object o: al)System.out.print(o+"-");System.out.println("b ");2import java.util.*;public class T2public static void main(String args)Object o1 = new Object();ThreadTwo t2 = new ThreadTwo(o1);ThreadOne t1 = new ThreadOne(o1,

28、t2);t1.start();/為確保t1先運(yùn)行我讓t2在t1的run方法里面啟動(dòng)static class ThreadOne extends ThreadObject a;Thread t;public ThreadOne(Object o1,Thread t2)a = o1;t= t2;public void run()t.start();synchronized(a)trySystem.out.println("我是A: 我要先睡一個(gè)小時(shí)");a.wait(1000*3600);System.out.println("我是A: 我被吵醒了");ca

29、tch(Exception e)e.printStackTrace();static class ThreadTwo extends ThreadObject a;public ThreadTwo(Object o1)a = o1;public void run()boolean flag = true;Scanner sc = new Scanner(System.in);ArrayList al = new ArrayList();trysynchronized(a)while(flag)sleep(1000);System.out.println("我是線程B: 起床"

30、;);al.add(1);System.out.println();if(al.size()=3)a.notify();flag=false;catch(Exception e)e.printStackTrace();3import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JTextAre

31、a;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;public class T3 JFrame frame;JTextField jtf;JTextArea jtf2;JButton button;public T3()frame = new JFrame("我的第三題");frame.setLayout(new GridLayout(3,1);jtf = new JTextField();jtf2 = new JTextArea ();button =

32、new JButton("請(qǐng)空");button.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae) jtf.setText("");jtf2.setText(""););frame.add(jtf);frame.add(jtf2);frame.add(button);frame.setBounds(200, 100, 400, 300);frame.setDefaultCloseOperation(3);jtf.addKe

33、yListener(new MyKeyListener();frame.addKeyListener(new MyKeyListener();frame.setResizable(false);frame.setVisible(true);public static void main(String args) new T3();class MyKeyListener implements KeyListenerpublic void keyPressed(KeyEvent ke) if(ke.getKeyCode()=KeyEvent.VK_ENTER)jtf2.setText(jtf.ge

34、tText()+"rn"+jtf.getText()+"rn"+jtf.getText();public void keyReleased(KeyEvent ke) public void keyTyped(KeyEvent ke) 4import java.io.File;import java.io.FilenameFilter;public class T4public static void main(String args)File file = new File("T4.java");String path =file.g

35、etAbsolutePath();String dir = path.substring(0,(path.lastIndexOf("");System.out.println("當(dāng)前目錄是:n"+dir);File MyDir = new File(dir);File myFiles = MyDir.listFiles(new FilenameFilter()Overridepublic boolean accept(File file, String fileName) if(fileName.endsWith(".java")re

36、turn true;return false;);System.out.println("在它下面的java文件有:");for(File f :myFiles)System.out.println(f.getName();5public class T5public static int fun(int n)if(n=1)return 3;else if(n=2)return 8;else return 2*(fun(n-1)+fun(n-2);public static void main(Stringargs)System.out.println(fun(10);6i

37、mport java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Collections;/將查詢的結(jié)果封裝成實(shí)現(xiàn)comparable接口的Student對(duì)象 。在Student對(duì)象里面添加比較規(guī)則/將Student都放到集合里面 再通過Collectionsd的sort排序就好了public class T6 statictry Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); catch (ClassNotFoundException

溫馨提示

  • 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)論