itatjava第三届、第五届复赛答案.doc

上传人:scccc 文档编号:14378497 上传时间:2022-02-04 格式:DOC 页数:14 大小:138KB
返回 下载 相关 举报
itatjava第三届、第五届复赛答案.doc_第1页
第1页 / 共14页
itatjava第三届、第五届复赛答案.doc_第2页
第2页 / 共14页
itatjava第三届、第五届复赛答案.doc_第3页
第3页 / 共14页
itatjava第三届、第五届复赛答案.doc_第4页
第4页 / 共14页
itatjava第三届、第五届复赛答案.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《itatjava第三届、第五届复赛答案.doc》由会员分享,可在线阅读,更多相关《itatjava第三届、第五届复赛答案.doc(14页珍藏版)》请在三一文库上搜索。

1、操作题:(请将编写好的源程序以题号命名,例如第1题的源程序保存为“1.java” )1、 编写一个Java应用程序,计算并输出一维数组(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 max) max = ai;if (ai min) min =

2、ai;System.out.println(最大值为:+max);System.out.println(最小值为:+min);2、 编写一个Java应用程序,该程序使用FileInputStream类,实现从磁盘读取本应用程序源代码文件,并将文件内容显示在屏幕上。(本题20分)import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class itat2 public static void main(String ags) throws IOException Buff

3、eredReader 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;/* * author Godwin * version 2010-05-16 */public class Ex1 public stati

4、c 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() != null) System.out.println(s); *3、 编写一个Java应用程序,利用RandomAccessFile类,把几个int型整数(1,2,3,4,5,6,7,8,9,10)

5、写入到一个名字为tom.dat文件中,然后按相反顺序读出这些数据并显示在屏幕上。(注意,一个int型数据占4个字节)(本题30分)package itat;import java.io.File;import java.io.RandomAccessFile;public class qq public static void main(String args) throws Exception String directory = D:/test123;String name = tom.dat;File f = new File(directory, name);RandomAccessF

6、ile 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 = new RandomAccessFile(f, rw);for (int i = 0; i a.length; i+) int j = file1.readInt();System.out.println(j);4、 编写一个Java GUI应用程序,采用Java多线程

7、技术,模拟自由落体和平抛运动:一个球自由落下,一个球水平抛出。(本题30分)(自由落体物理公式:h= g *t2/2 ;平抛运动物理公式:h= g *t2/2 ,x=26*t ;h代表高度,t代表时间,g代表重力加速度=9.8 m/s2 )附加题:5、 编写一个Java应用程序,实现如下功能:1) 判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;2) 判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;3) 按字典顺序比较两个字符串”你”和”我”的大小关系;4) 将数字型字符串”100”和”1

8、23.678”转换为数字;5) 将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。 (本题20分)public class aa public static void main(String ags)String s1=you are a student,s2=how are you;if(s1.equals(s2)System.out.println(s1和s2相同。);elseSystem.out.println(s1和s2不同。);String s3=22030219851022024; String s4=220302; if(s3.startsWith(s

9、4) System.out.println(s3+的前缀为+s4); else System.out.println(s3+的前缀不为+s4); if(s3.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.out.println(+s5+和+s6+等); else Syste

10、m.out.println(+s5+比+s6+小); String s7=100; String s8=123.678; int num1=Integer.parseInt(s7); Double num2=Double.parseDouble(s8); System.out.println(num1); System.out.println(num2); String s9=FEDCBA; char s99=new char6; for(int i=0;is99.length;i+) s99i=s9.charAt(i); System.out.println(s99i); String s9

11、2=FEDCBA; char arr=s92.toCharArray(); for (int i = 0; i arr.length; i+) System.out.println(arri); 6、 编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像(考生可自选图像文件)。服务器端Server.java将图像文件包装成数据包,并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。(本题30分)程序的运行效果如下图所示:客户端服务器端服务端代码:pa

12、ckage datasocket;import java.io.*;import .*;public class FortuneServer1 extends Thread DatagramSocket ServerSocket;public FortuneServer1() super(FortuneServer1);try ServerSocket = new DatagramSocket(1121);System.out.println(FortuneServer up and running.); catch (SocketException e) System.err.println

13、(Exception: couldnt create datagram socket);System.exit(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.getAd

14、dress();port = packet.getPort();File inFile = new File(D:/q1.jpg);FileInputStream inStream = new FileInputStream(inFile);if (inStream.read(data) = 0) System.err.println(Error: couldnt read fortunes);packet = new DatagramPacket(data, data.length, address, port);ServerSocket.send(packet); catch (Excep

15、tion e) System.err.println(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.ActionListene

16、r;import 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 serialVer

17、sionUID = 2567338201545440156L;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 windowCl

18、osing(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.length, InetAddre

19、ss.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.println(Exception: host coul

20、d 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-generated method stubgetMessage();this.add(label);label.

21、setVisible(true);this.setVisible(true);1、某人有5张3分和4张5分的邮票,请编写一个程序,计算由这些邮票中的1张或若干张可以得到多少种不同的邮资,并按照邮资从小到大顺序显示。(20分) 2、采用Java多线程技术编写程序,其中包括两个线程:A和B,其中A线程准备休眠一小时,B线程每隔一秒输入3句“起床”后,吵醒休眠的线程A。(25分)3、利用Java的GUI编程,编写一个窗体,包含两个文本框和一个命令按钮。其中一个文本框接收用户输入的一行字符串,回车后在另一个文本框中重复输出三行,单击命令按钮可清空两个文本框的所有内容。(25分)4、编写一个Java应用

22、程序,运行后,首先列出当前工作目录,然后把当前目录下面的所有后缀为java的文件取出(设置一个过滤器进行文件名后缀的过滤)。(30分)附加题:5、使用堆栈结构输出 ,其中 =2 +2 , =3, =8。(20分)6、访问Access 2003数据库,在Access数据库中创建学生表(T_Student)表并配置ODBC驱动源,学生表的结构和示例数据如下所示:T_ Student表:字段名称说明数据类型约束备注S_ID学号Integer主键S_Name姓名Varchar(10)不允许空S_Email邮箱Varchar(30)S_Score英语成绩Integer表数据示例:S_IDS_NameS_

23、Email S_Score1001JerryJerry801002MikeMike901003JohnJohn78使用Java语言编写程序,要求如下:1)使用JDBC-ODBC桥驱动程序;2)查询出表中所有记录,并按照主键升序显示。参考提示代码:Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);Connection conn=DriverManager.getConnection(jdbc:odbc:student,);(30分)mport java.util.*;public class T1public static void main(String

24、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(总共的邮资数是:+al.size();System.out.println(他们从小到大的依次书序是:);for(Object o: al)System.out.print(o+-);System.out.println(b );2import java.uti

25、l.*;public class T2public static void main(String args)Object o1 = new Object();ThreadTwo t2 = new ThreadTwo(o1);ThreadOne t1 = new ThreadOne(o1,t2);t1.start();/为确保t1先运行我让t2在t1的run方法里面启动static class ThreadOne extends ThreadObject a;Thread t;public ThreadOne(Object o1,Thread t2)a = o1;t= t2;public vo

26、id run()t.start();synchronized(a)trySystem.out.println(我是A: 我要先睡一个小时);a.wait(1000*3600);System.out.println(我是A: 我被吵醒了);catch(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(S

27、ystem.in);ArrayList al = new ArrayList();trysynchronized(a)while(flag)sleep(1000);System.out.println(我是线程B: 起床);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.even

28、t.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JTextArea;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(我的

29、第三题);frame.setLayout(new GridLayout(3,1);jtf = new JTextField();jtf2 = new JTextArea ();button = new JButton(请空);button.addActionListener(new ActionListener()public void actionPerformed(ActionEvent ae) jtf.setText();jtf2.setText(););frame.add(jtf);frame.add(jtf2);frame.add(button);frame.setBounds(20

30、0, 100, 400, 300);frame.setDefaultCloseOperation(3);jtf.addKeyListener(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

31、 ke) if(ke.getKeyCode()=KeyEvent.VK_ENTER)jtf2.setText(jtf.getText()+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.

32、java);String path =file.getAbsolutePath();String dir = path.substring(0,(path.lastIndexOf();System.out.println(当前目录是:n+dir);File MyDir = new File(dir);File myFiles = MyDir.listFiles(new FilenameFilter()Overridepublic boolean accept(File file, String fileName) if(fileName.endsWith(.java)return true;r

33、eturn 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);6import java.sql.Connecti

34、on;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Collections;/将查询的结果封装成实现comparable接口的Student对象 。在Student对象里面添加比较规则/将Student都放到集合里面 再通过Collectionsd的sort排序就好了public class T6 statictry Class.fo

35、rName(sun.jdbc.odbc.JdbcOdbcDriver); catch (ClassNotFoundException e) e.printStackTrace();public static void main(Stringargs)ArrayList al = new ArrayList();Connection con = null;Statement sta = null;ResultSet rs = null;trycon = DriverManager.getConnection(url,user,password);sta = con.createStatement

36、();rs = sta.executeQuery(select * from T_student);/查询所有的学生信息while(rs.next()Student stu = new Student();stu.setId(rs.getInt(1);stu.setName(rs.getString(2);stu.setScore(rs.getInt(3);al.add(stu);catch(Exception ex)ex.printStackTrace();finallytry con.close();sta.close();rs.close(); catch (SQLException e

37、) e.printStackTrace();Collections.sort(al);for(Student s: al)System.out.print(学号:+s.getId();System.out.println(姓名:+s.getName();System.out.println(学分:+s.getScore();/将查询的结果封装成Student对象 。在Student对象里面添加比较规则class Student implements Comparableprivate int id;private String name;private int score;public int

38、 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 int getScore() return score;public void setScore(int score) this.score = score;Overridepublic int compareTo(Object o) Student s =(Student)o;return this.id-s.id;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1