JAVA

第五届全国ITAT教育工程就业技能大赛复赛试题
Java程序设计(A卷)

<!--[if !supportLists]-->1、  <!--[endif]-->某人有53分和45分的邮票,请编写一个程序,计算由这些邮票中的1张或若干张可以得到多少种不同的邮资,并按照邮资从小到大顺序显示。(20分)

 

<!--[if !supportLists]-->2、  <!--[endif]-->采用Java多线程技术编写程序,其中包括两个线程:AB,其中A线程准备休眠一小时,B线程每隔一秒输入3句“起床”后,吵醒休眠的线程A。(25分)

 

<!--[if !supportLists]-->3、  <!--[endif]-->利用JavaGUI编程,编写一个窗体,包含两个文本框和一个命令按钮。其中一个文本框接收用户输入的一行字符串,回车后在另一个文本框中重复输出三行,单击命令按钮可清空两个文本框的所有内容。(25分)

 

<!--[if !supportLists]-->4、  <!--[endif]-->编写一个Java应用程序,运行后,首先列出当前工作目录,然后把当前目录下面的所有后缀为java的文件取出(设置一个过滤器进行文件名后缀的过滤)。(30分)

 

答案:

 

第一题:

<!--[if !supportLists]-->1.  <!--[endif]-->package exercise1;  

<!--[if !supportLists]-->2.  <!--[endif]-->  

<!--[if !supportLists]-->3.  <!--[endif]-->import java.util.ArrayList;  

<!--[if !supportLists]-->4.  <!--[endif]-->import java.util.Arrays;  

<!--[if !supportLists]-->5.  <!--[endif]-->import java.util.HashSet;  

<!--[if !supportLists]-->6.  <!--[endif]-->import java.util.Iterator;  

<!--[if !supportLists]-->7.  <!--[endif]-->  

<!--[if !supportLists]-->8.  <!--[endif]-->public class PostCount {  

<!--[if !supportLists]-->9.  <!--[endif]-->  

<!--[if !supportLists]-->10. <!--[endif]-->    /** 

<!--[if !supportLists]-->11. <!--[endif]-->     * @param args 

<!--[if !supportLists]-->12. <!--[endif]-->     */  

<!--[if !supportLists]-->13. <!--[endif]-->    public static void main(String[] args) {  

<!--[if !supportLists]-->14. <!--[endif]-->        int count = 0;  

<!--[if !supportLists]-->15. <!--[endif]-->        HashSet<Integer> post = new HashSet<Integer>();  

<!--[if !supportLists]-->16. <!--[endif]-->        for (int i = 0; i < 6; i++)  

<!--[if !supportLists]-->17. <!--[endif]-->            for (int j = 0; j < 5; j++) {  

<!--[if !supportLists]-->18. <!--[endif]-->                count = i * 3 + j * 5;  

<!--[if !supportLists]-->19. <!--[endif]-->                post.add(new Integer(count));  

<!--[if !supportLists]-->20. <!--[endif]-->            }  

<!--[if !supportLists]-->21. <!--[endif]-->        post.remove(new Integer(0));  

<!--[if !supportLists]-->22. <!--[endif]-->        Iterator<Integer> it = post.iterator();  

<!--[if !supportLists]-->23. <!--[endif]-->        ArrayList<Integer> al = new ArrayList<Integer>();  

<!--[if !supportLists]-->24. <!--[endif]-->        while (it.hasNext()) {  

<!--[if !supportLists]-->25. <!--[endif]-->            al.add((Integer) it.next());  

<!--[if !supportLists]-->26. <!--[endif]-->        }  

<!--[if !supportLists]-->27. <!--[endif]-->        post.clear();  

<!--[if !supportLists]-->28. <!--[endif]-->        int b[] = new int[al.size()];  

<!--[if !supportLists]-->29. <!--[endif]-->        for (int i = 0; i < al.size(); i++) {  

<!--[if !supportLists]-->30. <!--[endif]-->            b[i] = (new Integer(al.get(i))).intValue();  

<!--[if !supportLists]-->31. <!--[endif]-->        }  

<!--[if !supportLists]-->32. <!--[endif]-->        al.clear();  

<!--[if !supportLists]-->33. <!--[endif]-->        Arrays.sort(b);  

<!--[if !supportLists]-->34. <!--[endif]-->        System.out.println(Arrays.toString(b));  

<!--[if !supportLists]-->35. <!--[endif]-->  

<!--[if !supportLists]-->36. <!--[endif]-->    }  

<!--[if !supportLists]-->37. <!--[endif]-->  

<!--[if !supportLists]-->38. <!--[endif]-->

 

 

 

第二题:

<!--[if !supportLists]-->1.  <!--[endif]-->package thread;  

<!--[if !supportLists]-->2.  <!--[endif]-->  

<!--[if !supportLists]-->3.  <!--[endif]-->public class Itat_2_thread {  

<!--[if !supportLists]-->4.  <!--[endif]-->    public final static Thread t1 = new SleepThread();  

<!--[if !supportLists]-->5.  <!--[endif]-->    public static void main(String[] args) {  

<!--[if !supportLists]-->6.  <!--[endif]-->        Thread t2 = new Thread("wake_up") {  

<!--[if !supportLists]-->7.  <!--[endif]-->            public void run() {  

<!--[if !supportLists]-->8.  <!--[endif]-->                while (true) {  

<!--[if !supportLists]-->9.  <!--[endif]-->                    try {  

<!--[if !supportLists]-->10. <!--[endif]-->                        Thread.sleep(1000);  

<!--[if !supportLists]-->11. <!--[endif]-->                        for (int i = 0; i < 3; i++)  

<!--[if !supportLists]-->12. <!--[endif]-->                            System.out.println("起床");  

<!--[if !supportLists]-->13. <!--[endif]-->                        synchronized (t1) {  

<!--[if !supportLists]-->14. <!--[endif]-->                            t1.interrupt();  

<!--[if !supportLists]-->15. <!--[endif]-->                        }//同步代码块  

<!--[if !supportLists]-->16. <!--[endif]-->                    } catch (InterruptedException e) {  

<!--[if !supportLists]-->17. <!--[endif]-->                        // TODO Auto-generated catch block  

<!--[if !supportLists]-->18. <!--[endif]-->                        e.printStackTrace();  

<!--[if !supportLists]-->19. <!--[endif]-->                    }  

<!--[if !supportLists]-->20. <!--[endif]-->                }  

<!--[if !supportLists]-->21. <!--[endif]-->            }  

<!--[if !supportLists]-->22. <!--[endif]-->        };  

<!--[if !supportLists]-->23. <!--[endif]-->        t1.start();  

<!--[if !supportLists]-->24. <!--[endif]-->        t2.start();  

<!--[if !supportLists]-->25. <!--[endif]-->    }  

<!--[if !supportLists]-->26. <!--[endif]-->}  

<!--[if !supportLists]-->27. <!--[endif]-->  

<!--[if !supportLists]-->28. <!--[endif]-->class SleepThread extends Thread {  

<!--[if !supportLists]-->29. <!--[endif]-->    public void run() {  

<!--[if !supportLists]-->30. <!--[endif]-->        while (true) {  

<!--[if !supportLists]-->31. <!--[endif]-->            try {  

<!--[if !supportLists]-->32. <!--[endif]-->                Thread.sleep(1000 * 60 * 60);  

<!--[if !supportLists]-->33. <!--[endif]-->            } catch (InterruptedException e) {  

<!--[if !supportLists]-->34. <!--[endif]-->                System.out.println("吵醒休眠线程A");  

<!--[if !supportLists]-->35. <!--[endif]-->                // do what you want after the thread are waked up  

<!--[if !supportLists]-->36. <!--[endif]-->            }  

<!--[if !supportLists]-->37. <!--[endif]-->        }  

<!--[if !supportLists]-->38. <!--[endif]-->    }  

<!--[if !supportLists]-->39. <!--[endif]-->}

 

 

第三题:

<!--[if !supportLists]-->1.  <!--[endif]-->package sugite.swing;  

<!--[if !supportLists]-->2.  <!--[endif]-->  

<!--[if !supportLists]-->3.  <!--[endif]-->import java.awt.Event;  

<!--[if !supportLists]-->4.  <!--[endif]-->import java.awt.GridLayout;  

<!--[if !supportLists]-->5.  <!--[endif]-->import java.awt.event.KeyAdapter;  

<!--[if !supportLists]-->6.  <!--[endif]-->import java.awt.event.KeyEvent;  

<!--[if !supportLists]-->7.  <!--[endif]-->import java.awt.event.MouseAdapter;  

<!--[if !supportLists]-->8.  <!--[endif]-->import java.awt.event.MouseEvent;  

<!--[if !supportLists]-->9.  <!--[endif]-->import java.awt.event.WindowAdapter;  

<!--[if !supportLists]-->10. <!--[endif]-->import java.awt.event.WindowEvent;  

<!--[if !supportLists]-->11. <!--[endif]-->  

<!--[if !supportLists]-->12. <!--[endif]-->import javax.swing.JButton;  

<!--[if !supportLists]-->13. <!--[endif]-->import javax.swing.JFrame;  

<!--[if !supportLists]-->14. <!--[endif]-->import javax.swing.JTextArea;  

<!--[if !supportLists]-->15. <!--[endif]-->import javax.swing.JTextField;  

<!--[if !supportLists]-->16. <!--[endif]-->  

<!--[if !supportLists]-->17. <!--[endif]-->public class Itat_3_ans extends JFrame {  

<!--[if !supportLists]-->18. <!--[endif]-->  

<!--[if !supportLists]-->19. <!--[endif]-->    /** 

<!--[if !supportLists]-->20. <!--[endif]-->     *  

<!--[if !supportLists]-->21. <!--[endif]-->     */  

<!--[if !supportLists]-->22. <!--[endif]-->    private static final long serialVersionUID = 1L;  

<!--[if !supportLists]-->23. <!--[endif]-->    static JTextField jt1;  

<!--[if !supportLists]-->24. <!--[endif]-->    static JTextArea jt2;  

<!--[if !supportLists]-->25. <!--[endif]-->    static JButton jb;  

<!--[if !supportLists]-->26. <!--[endif]-->  

<!--[if !supportLists]-->27. <!--[endif]-->    public static void main(String[] args) {  

<!--[if !supportLists]-->28. <!--[endif]-->        JFrame frame = new JFrame();  

<!--[if !supportLists]-->29. <!--[endif]-->        frame.setSize(500500);  

<!--[if !supportLists]-->30. <!--[endif]-->        frame.setLocation(200200);  

<!--[if !supportLists]-->31. <!--[endif]-->        jt1 = new JTextField();  

<!--[if !supportLists]-->32. <!--[endif]-->        jt2 = new JTextArea();  

<!--[if !supportLists]-->33. <!--[endif]-->        jb = new JButton("命令");  

<!--[if !supportLists]-->34. <!--[endif]-->        jt1.addKeyListener(new KeyAdapter() {  

<!--[if !supportLists]-->35. <!--[endif]-->            public void keyPressed(KeyEvent e) {  

<!--[if !supportLists]-->36. <!--[endif]-->                if (e.getKeyCode() == Event.ENTER) // 按键 执行相应操作;  

<!--[if !supportLists]-->37. <!--[endif]-->                {  

<!--[if !supportLists]-->38. <!--[endif]-->                    String str = jt1.getText();  

<!--[if !supportLists]-->39. <!--[endif]-->                    jt1.setText("");  

<!--[if !supportLists]-->40. <!--[endif]-->                    jt2.append(str+"\n" + str +"\n"+ str);  

<!--[if !supportLists]-->41. <!--[endif]-->                }  

<!--[if !supportLists]-->42. <!--[endif]-->            }  

<!--[if !supportLists]-->43. <!--[endif]-->        });  

<!--[if !supportLists]-->44. <!--[endif]-->        jb.addMouseListener(new MouseAdapter() {  

<!--[if !supportLists]-->45. <!--[endif]-->            public void mouseClicked(MouseEvent e) {  

<!--[if !supportLists]-->46. <!--[endif]-->                jt1.setText("");  

<!--[if !supportLists]-->47. <!--[endif]-->                jt2.setText("");  

<!--[if !supportLists]-->48. <!--[endif]-->            }  

<!--[if !supportLists]-->49. <!--[endif]-->        });  

<!--[if !supportLists]-->50. <!--[endif]-->        frame.add(jt1);  

<!--[if !supportLists]-->51. <!--[endif]-->        frame.add(jt2);  

<!--[if !supportLists]-->52. <!--[endif]-->        frame.add(jb);  

<!--[if !supportLists]-->53. <!--[endif]-->        frame.addWindowListener(new WindowAdapter(){  

<!--[if !supportLists]-->54. <!--[endif]-->            public void windowClosing(WindowEvent e){  

<!--[if !supportLists]-->55. <!--[endif]-->                System.exit(1) ;  

<!--[if !supportLists]-->56. <!--[endif]-->            }  

<!--[if !supportLists]-->57. <!--[endif]-->        } );  

<!--[if !supportLists]-->58. <!--[endif]-->        frame.setLayout(new GridLayout(22));  

<!--[if !supportLists]-->59. <!--[endif]-->        frame.setVisible(true);  

<!--[if !supportLists]-->60. <!--[endif]-->          

<!--[if !supportLists]-->61. <!--[endif]-->  

<!--[if !supportLists]-->62. <!--[endif]-->    }  

<!--[if !supportLists]-->63. <!--[endif]-->  

<!--[if !supportLists]-->64. <!--[endif]-->}

 

 

 

 

 

 

 

 

]

 

 

 

 

 

 

 

 

 

 

 

第四题:

<!--[if !supportLists]-->1.  <!--[endif]-->package file_operation;  

<!--[if !supportLists]-->2.  <!--[endif]-->  

<!--[if !supportLists]-->3.  <!--[endif]-->import java.io.File;  

<!--[if !supportLists]-->4.  <!--[endif]-->import java.io.FilenameFilter;  

<!--[if !supportLists]-->5.  <!--[endif]-->public class Itat_4_ans  

<!--[if !supportLists]-->6.  <!--[endif]-->{  

<!--[if !supportLists]-->7.  <!--[endif]-->    public static void main(String[] args)   

<!--[if !supportLists]-->8.  <!--[endif]-->    {  

<!--[if !supportLists]-->9.  <!--[endif]-->        File file = new File("Itat_4_ans.java");  

<!--[if !supportLists]-->10. <!--[endif]-->        String path = file.getAbsolutePath();  

<!--[if !supportLists]-->11. <!--[endif]-->        System.out.println(path);  

<!--[if !supportLists]-->12. <!--[endif]-->        String work_dir = path.substring(0,path.length() - file.toString().length());  

<!--[if !supportLists]-->13. <!--[endif]-->        System.out.println(work_dir);  

<!--[if !supportLists]-->14. <!--[endif]-->        PrintDirJava(new File(work_dir));  

<!--[if !supportLists]-->15. <!--[endif]-->    }  

<!--[if !supportLists]-->16. <!--[endif]-->  

<!--[if !supportLists]-->17. <!--[endif]-->    public static void PrintDirJava(File ff){  

<!--[if !supportLists]-->18. <!--[endif]-->        File f[] = ff.listFiles() ;  

<!--[if !supportLists]-->19. <!--[endif]-->        FileNameFilter fnf = new FileNameFilter();  

<!--[if !supportLists]-->20. <!--[endif]-->        for(int i  = 0 ; i<f.length ; i++){  

<!--[if !supportLists]-->21. <!--[endif]-->            if(fnf.accept(f[i],null))  

<!--[if !supportLists]-->22. <!--[endif]-->                System.out.println(f[i].getName());  

<!--[if !supportLists]-->23. <!--[endif]-->        }  

<!--[if !supportLists]-->24. <!--[endif]-->    }  

<!--[if !supportLists]-->25. <!--[endif]-->}  

<!--[if !supportLists]-->26. <!--[endif]-->  

<!--[if !supportLists]-->27. <!--[endif]-->class FileNameFilter implements FilenameFilter  

<!--[if !supportLists]-->28. <!--[endif]-->{  

<!--[if !supportLists]-->29. <!--[endif]-->    public boolean accept(File pathname,String b) {  

<!--[if !supportLists]-->30. <!--[endif]-->        String files = pathname.toString();        

<!--[if !supportLists]-->31. <!--[endif]-->        if (files.endsWith(".java"))   

<!--[if !supportLists]-->32. <!--[endif]-->            return true;        

<!--[if !supportLists]-->33. <!--[endif]-->        return false;  

<!--[if !supportLists]-->34. <!--[endif]-->    }  

<!--[if !supportLists]-->35. <!--[endif]-->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值