實驗十八 總複習java
1、實驗目的與要求 算法
(1) 綜合掌握java基本程序結構;編程
(2) 綜合掌握java面向對象程序設計特色;多線程
(3) 綜合掌握java GUI 程序設計結構;併發
(4) 綜合掌握java多線程編程模型;app
(5) 綜合編程練習。dom
2、實驗內容和步驟編程語言
任務1:填寫課程課後調查問卷,網址:https://www.wjx.cn/jq/33108969.aspx。ide
已完成佈局
任務2:綜合編程練習
練習1:設計一個用戶信息採集程序,要求以下:
(1) 用戶信息輸入界面以下圖所示:
(1)用戶點擊提交按鈕時,用戶輸入信息顯示控制檯界面;
(2)用戶點擊重置按鈕後,清空用戶已輸入信息;
(3)點擊窗口關閉,程序退出。
package 練習1; import java.awt.EventQueue; import javax.swing.JFrame; public class Mian { public static void main(String[] args) { EventQueue.invokeLater(() -> { DemoJFrame page = new DemoJFrame(); }); } }
package 練習1; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.Window; public class WinCenter { public static void center(Window win){ Toolkit tkit = Toolkit.getDefaultToolkit(); Dimension sSize = tkit.getScreenSize(); Dimension wSize = win.getSize(); if(wSize.height > sSize.height){ wSize.height = sSize.height; } if(wSize.width > sSize.width){ wSize.width = sSize.width; } win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2); } }
package 練習1; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.LayoutManager; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; public class DemoJFrame extends JFrame { private JPanel jPanel1; private JPanel jPanel2; private JPanel jPanel3; private JPanel jPanel4; private JTextField fieldname; private JComboBox comboBox; private JTextField fieldadress; private ButtonGroup bg; private JRadioButton nan; private JRadioButton nv; private JCheckBox sing; private JCheckBox dance; private JCheckBox draw; public DemoJFrame() { // 設置窗口大小 this.setSize(800, 400); // 設置可見性 this.setVisible(true); // 設置標題 this.setTitle("編程練習一"); // 設置關閉操做 this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 設置窗口居中 WinCenter.center(this); // 建立四個面板對象 jPanel1 = new JPanel(); setJPanel1(jPanel1); jPanel2 = new JPanel(); setJPanel2(jPanel2); jPanel3 = new JPanel(); setJPanel3(jPanel3); jPanel4 = new JPanel(); setJPanel4(jPanel4); // 設置容器的爲流佈局 FlowLayout flowLayout = new FlowLayout(); this.setLayout(flowLayout); // 將四個面板添加到容器中 this.add(jPanel1); this.add(jPanel2); this.add(jPanel3); this.add(jPanel4); } /* * 設置面一 */ private void setJPanel1(JPanel jPanel) { // TODO 自動生成的方法存根 jPanel.setPreferredSize(new Dimension(700, 45)); // 給面板的佈局設置爲網格佈局 一行4列 jPanel.setLayout(new GridLayout(1, 4)); JLabel name = new JLabel("姓名:"); name.setSize(100, 50); fieldname = new JTextField(""); fieldname.setSize(80, 20); JLabel study = new JLabel("學歷:"); comboBox = new JComboBox(); comboBox.addItem("初中"); comboBox.addItem("高中"); comboBox.addItem("本科"); jPanel.add(name); jPanel.add(fieldname); jPanel.add(study); jPanel.add(comboBox); } /* * 設置面板二 */ private void setJPanel2(JPanel jPanel) { // TODO 自動生成的方法存根 jPanel.setPreferredSize(new Dimension(700, 50)); // 給面板的佈局設置爲網格佈局 一行4列 jPanel.setLayout(new GridLayout(1, 4)); JLabel name = new JLabel("地址:"); fieldadress = new JTextField(); fieldadress.setPreferredSize(new Dimension(150, 50)); JLabel study = new JLabel("愛好:"); JPanel selectBox = new JPanel(); selectBox.setBorder(BorderFactory.createTitledBorder("")); selectBox.setLayout(new GridLayout(3, 1)); sing = new JCheckBox("唱歌"); dance = new JCheckBox("跳舞"); draw = new JCheckBox("畫畫"); selectBox.add(sing); selectBox.add(dance); selectBox.add(draw); jPanel.add(name); jPanel.add(fieldadress); jPanel.add(study); jPanel.add(selectBox); } /* * 設置面板三 */ private void setJPanel3(JPanel jPanel) { // TODO 自動生成的方法存根 jPanel.setPreferredSize(new Dimension(700, 150)); FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT); jPanel.setLayout(flowLayout); JLabel sex = new JLabel("性別:"); JPanel selectBox = new JPanel(); selectBox.setBorder(BorderFactory.createTitledBorder("")); selectBox.setLayout(new GridLayout(2, 1)); bg = new ButtonGroup(); nan = new JRadioButton("男"); nv = new JRadioButton("女"); bg.add(nan); bg.add(nv); selectBox.add(nan); selectBox.add(nv); jPanel.add(sex); jPanel.add(selectBox); } /* * 設置面板四 */ private void setJPanel4(JPanel jPanel) { // TODO 自動生成的方法存根 jPanel.setPreferredSize(new Dimension(700, 150)); FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 50, 10); jPanel.setLayout(flowLayout); jPanel.setLayout(flowLayout); JButton sublite = new JButton("提交"); JButton reset = new JButton("重置"); sublite.addActionListener((e) -> valiData()); reset.addActionListener((e) -> Reset()); jPanel.add(sublite); jPanel.add(reset); } /* * 提交數據 */ private void valiData() { // TODO 自動生成的方法存根 // 拿到數據 String name = fieldname.getText().toString().trim(); String xueli = comboBox.getSelectedItem().toString().trim(); String address = fieldadress.getText().toString().trim(); System.out.println(name); System.out.println(xueli); String hobbystring=""; if (sing.isSelected()) { hobbystring+="唱歌 "; } if (dance.isSelected()) { hobbystring+="跳舞 "; } if (draw.isSelected()) { hobbystring+="畫畫 "; } System.out.println(address); if (nan.isSelected()) { System.out.println("男"); } if (nv.isSelected()) { System.out.println("女"); } System.out.println(hobbystring); } /* * 重置 */ private void Reset() { // TODO 自動生成的方法存根 fieldadress.setText(null); fieldname.setText(null); comboBox.setSelectedIndex(0); sing.setSelected(false); dance.setSelected(false); draw.setSelected(false); bg.clearSelection(); } }
練習2:採用GUI界面設計如下程序:
l 編制一個程序,將身份證號.txt 中的信息讀入到內存中;
l 按姓名字典序輸出人員信息;
l 查詢最大年齡的人員信息;
l 查詢最小年齡人員信息;
l 輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;
l 查詢人員中是否有你的同鄉。
l 輸入身份證信息,查詢所提供身份證號的人員信息,要求輸入一個身份證數字時,查詢界面就顯示知足查詢條件的查詢結果,且隨着輸入的數字的增多,查詢匹配的範圍逐漸縮小。
package 練習2; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.Timer; import javax.swing.*; public class find extends JFrame { private static ArrayList<Person> Personlist; Scanner scanner = new Scanner(System.in); File file = new File("E:\\身份證號.txt"); private JPanel Panel; private JLabel JLabel1; private JButton Button,Button2,Button3,Button4; private JTextArea text,text1,text2,text3; boolean tru=true; public find() { Panel = new JPanel();Panel.setLayout(null); Button = new JButton("1:按姓名字典序輸出人員信息"); Button2 = new JButton("2:查詢最大年齡信息"); Button4 = new JButton("3:最小年齡人員信息"); Button3 = new JButton("查詢相近年齡"); JLabel1 = new JLabel("輸入身份證號或者地址查詢"); JLabel1.setBounds(900, 50, 400, 30); text=new JTextArea(30,80);text.setBounds(50, 180, 700, 700); text1=new JTextArea(1,30);text1.setBounds(900, 80, 400, 30); text2=new JTextArea(30,80);text2.setBounds(900,180,700, 700); text3=new JTextArea(30,80);text3.setBounds(420,100,200,40); Button.addActionListener(new Action());Button.setBounds(50,30,300,40); Button2.addActionListener(new Action1());Button2.setBounds(50,80,300,40); Button3.addActionListener(new Action2());Button3.setBounds(650,100,120,40); Button4.addActionListener(new Action3());Button4.setBounds(50,130,300,40); Panel.add(JLabel1); Panel.add(Button); Panel.add(Button2); Panel.add(Button3); Panel.add(Button4); Panel.add(text); Panel.add(text2); Panel.add(text1); Panel.add(text3); add(Panel); Timer timer = new Timer(); TimerTask timeTask=new TimerTask() { @Override public void run() { // TODO Auto-generated method stub text2.setText(null); String place=text1.getText().toString().trim(); for (int i = 0; i <Personlist.size(); i++) { String Str=(String)Personlist.get(i).getbirthplace(); if(Str.contains(place)&&!place.equals("")) { text2.append(Personlist.get(i).toString()); } } for (int i = 0; i <Personlist.size(); i++) { String Str=(String)Personlist.get(i).getID(); if(Str.contains(place)&&!place.equals("")) { text2.append(Personlist.get(i).toString()); } } } };timer.schedule(timeTask, 0,100); Personlist = new ArrayList<>(); try { FileInputStream fis = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String ID = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String place =linescanner.nextLine(); Person Person = new Person(); Person.setname(name); Person.setID(ID); Person.setsex(sex); int a = Integer.parseInt(age); Person.setage(a); Person.setbirthplace(place); Personlist.add(Person); } } catch (FileNotFoundException e) { System.out.println("查找不到信息"); e.printStackTrace(); } catch (IOException e) { System.out.println("信息讀取有誤"); e.printStackTrace(); } } private class Action implements ActionListener { public void actionPerformed(ActionEvent event) { text.setText(null); Collections.sort(Personlist); text.append(Personlist.toString()); } } private class Action1 implements ActionListener { public void actionPerformed(ActionEvent event) { text.setText(null); int max=0;int j,k1 = 0; for(int i=1;i<Personlist.size();i++) { j=Personlist.get(i).getage(); if(j>max) { max=j; k1=i; } } text.append("年齡最大: "+Personlist.get(k1)); } } private class Action3 implements ActionListener { public void actionPerformed(ActionEvent event) { text.setText(null); int min=100;int j,k2=0; for(int i=1;i<Personlist.size();i++) { j=Personlist.get(i).getage(); if(j<min) { min=j; k2=i; } } text.append("年齡最小: "+Personlist.get(k2)); } } private class Action2 implements ActionListener { public void actionPerformed(ActionEvent event) { text.setText(null); int a = Integer.parseInt(text3.getText().toString().trim()); int d_value=a-Personlist.get(agenear(a)).getage(); for (int i = 0; i < Personlist.size(); i++) { int p=Personlist.get(i).getage()-a; if(p==d_value||-p==d_value) text.append(Personlist.get(i).toString()); } } } public static int agenear(int age) { int j=0,min=53,d_value=0,k=0; for (int i = 0; i < Personlist.size(); i++) { d_value=Personlist.get(i).getage()-age; if(d_value<0) d_value=-d_value; if (d_value<min) { min=d_value; k=i; } } return k; } }
package 練習2; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Toolkit; import javax.swing.JFrame; public class Out { public static void main (String args[]) { Toolkit t=Toolkit.getDefaultToolkit(); Dimension s=t.getScreenSize(); EventQueue.invokeLater(() -> { JFrame frame = new find(); frame.setBounds(0, 0,(int)s.getWidth(),(int)s.getHeight()); frame.setTitle("第七組"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package 練習2; public class Person implements Comparable<Person> { private String name; private String ID; private int age; private String sex; private String birthplace; public String getname() { return name; } public void setname(String name) { this.name = name; } public String getID() { return ID; } public void setID(String ID) { this.ID= ID; } public int getage() { return age; } public void setage(int age) { this.age= age; } public String getsex() { return sex; } public void setsex(String sex) { this.sex= sex; } public String getbirthplace() { return birthplace; } public void setbirthplace(String birthplace) { this.birthplace= birthplace; } public int compareTo(Person o) { return this.name.compareTo(o.getname()); } public String toString() { return name+"\t"+sex+"\t"+age+"\t"+ID+"\t"+birthplace+"\n"; } }
練習3:採用GUI界面設計如下程序
l 編寫一個計算器類,能夠完成加、減、乘、除的操做
l 利用計算機類,設計一個小學生100之內數的四則運算練習程序,由計算機隨機產生10道加減乘除練習題,學生輸入答案,由程序檢查答案是否正確,每道題正確計10分,錯誤不計分,10道題測試結束後給出測試總分;
l 將程序中測試練習題及學生答題結果輸出到文件,文件名爲test.txt。
package 練習3; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Collections; import java.util.Scanner; import javax.swing.*; import java.math.*; public class Demo extends JFrame { private String[] c = new String[10]; private String[] c1 = new String[10]; private int[] list = new int[10]; int i = 0, i1 = 0, sum = 0; private PrintWriter out = null; private JTextArea text, text1; private int counter; public Demo() { JPanel Panel = new JPanel(); Panel.setLayout(null); JLabel JLabel1 = new JLabel(""); JLabel1.setBounds(500, 800, 400, 30); JLabel1.setFont(new Font("Courier", Font.PLAIN, 35)); JButton Button = new JButton("題目"); Button.setBounds(50, 150, 150, 50); Button.setFont(new Font("Courier", Font.PLAIN, 20)); Button.addActionListener(new Action()); JButton Button2 = new JButton("答案"); Button2.setBounds(300, 150, 150, 50); Button2.setFont(new Font("Courier", Font.PLAIN, 20)); Button2.addActionListener(new Action1()); JButton Button3 = new JButton("輸出"); Button3.setBounds(500, 150, 150, 50); Button3.setFont(new Font("Courier", Font.PLAIN, 20)); Button3.addActionListener(new Action2()); text = new JTextArea(30, 80); text.setBounds(30, 50, 200, 50); text.setFont(new Font("Courier", Font.PLAIN, 35)); text1 = new JTextArea(30, 80); text1.setBounds(270, 50, 200, 50); text1.setFont(new Font("Courier", Font.PLAIN, 35)); Panel.add(text); Panel.add(text1); Panel.add(Button); Panel.add(Button2); Panel.add(Button3); Panel.add(JLabel1); add(Panel); } private class Action implements ActionListener { public void actionPerformed(ActionEvent event) { text1.setText("0"); if (i < 10) { int a = 1 + (int) (Math.random() * 99); int b = 1 + (int) (Math.random() * 99); int m = (int) Math.round(Math.random() * 3); switch (m) { case 0: while (a < b) { b = (int) Math.round(Math.random() * 100); a = (int) Math.round(Math.random() * 100); } c[i] = ( a + "/" + b + "="); list[i] = Math.floorDiv(a, b); text.setText(a + "/" + b + "="); i++; break; case 1: c[i] = (a + "*" + b + "="); list[i] = Math.multiplyExact(a, b); text.setText(a + "*" + b + "="); i++; break; case 2: c[i] = (a + "+" + b + "="); list[i] = Math.addExact(a, b); text.setText(a + "+" + b + "="); i++; break; case 3: while (a <= b) { b = (int) Math.round(Math.random() * 100); a = (int) Math.round(Math.random() * 100); } c[i] = (a + "-" + b + "="); text.setText(a + "-" + b + "="); list[i] = Math.subtractExact(a, b); i++; break; } } } } private class Action1 implements ActionListener { public void actionPerformed(ActionEvent event) { if (i < 10) { text.setText(null); String daan = text1.getText().toString().trim(); int a = Integer.parseInt(daan); if (text1.getText() != "") { if (list[i1] == a) sum += 10; } c1[i1] = daan; i1++; } } } private class Action2 implements ActionListener { public void actionPerformed(ActionEvent event) { try { out = new PrintWriter("text.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int counter = 0; counter < 10; counter++) { out.println(c[counter] + c1[counter]); } out.println("成績" + sum); out.close(); } } }
package 練習3; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Toolkit; import javax.swing.JFrame; public class New { public static void main (String args[]) { Toolkit t=Toolkit.getDefaultToolkit(); Dimension s=t.getScreenSize(); EventQueue.invokeLater(() -> { JFrame frame = new Demo(); frame.setBounds(0, 0,(int)s.getWidth()/2,(int)s.getHeight()/2); frame.setTitle("第七組"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
、
任務3:本學期課程已結束,請彙總《面向對象程序設計課程學習進度條》的數據,統計我的專業能力提高的數據。並從學習內容、學習方法、學習心得幾個方面進行課程學習總結,也但願你對課程的不足提出建議和意見。
彙總《面向對象程序設計課程學習進度條》的數據:
周次 | (閱讀/編寫)代碼行數 | 發佈博文量/評論他人博文數量 | 課餘學習時間(小時) | 學習收穫最大的程序閱讀或編程任務 |
1 | 30/40 | 1/0 | 8 | 實驗題 7 |
2 | 50/80 | 1/0 | 6 | 實驗題 4 |
3 | 80/120 | 1/1 | 8 | 實驗題 3 |
4 | 300/100 | 1/0 | 10 | 實驗題3 |
5 | —— | —— | —— | —— |
6 | 280/200 | 1/0 | 6 | 實驗題2和實驗3(例題) |
7 | 300/288 | 1/0 | 8 | 測試題 |
8 | 400/320 | 1/0 | 6 | 實驗3 |
9 | 450/480 | 1/0 | 8 | 實驗一、2 |
10 | 400/320 | 1/0 | 6 | 總結 |
11 | 600/550 | 1/4 | 6 | 結對編程 |
12 | 600/500 | 1/0 | 10 | 習題練習 |
13 | 600/450 | 1/0 | 8 | 隨機點名器實驗 |
14 | 650/400 | 1/0 | 6 | 小組討論 |
15 | 550/550 | 1/0 | 8 | 模糊查找,13-6測試 |
16 | 600/450 | 1/0 | 6 | 練習2 |
17 | 350/400 | 1/0 | 4 | 練習1 |
18 | 400/500 | 1/0 | 5 | |
|
學習內容:
Java語言特色與開發環境配置(第1章、第2章)
Java基本程序結構(第3章)
Java面向對象程序結構(第4章、第5章、第6章)
類、類間關係、類圖
Java JDK預約義類/接口及其API(String-第3章、 Arrays-第3章、Files-第3章62頁、LocalDate-第4章、 Object-第5章、對象包裝器-第5章、Comparator-第6章、 異常類-第7章、ArrayList-第5+8章、第9章、第10-12章、 第14章) — Java異常處理編程模型 — Java GUI編程模型
Java併發程序設計(第14章)
Java應用程序部署(第13章)
學習方法以及學習心得:
1.在學習Java以前咱們已經接觸過C語言,Java和C都是指令式語言,但它們也存在不少不一樣,因此在學習過程當中咱們須要運用一些C的知識,但更應該從它們的不一樣入手,深入的去學習;
2.在咱們學習過程當中,代老師每週都會提早將課件發到QQ學習羣裏,而且經過提早完成實驗而後再進行知識總結的方式授學,在這種教學方式下咱們能夠經過實驗找出本身的不足,對編程能力的提高很是有用;
3.在編寫程序的過程當中,我老是採起最基本的操做,因此編程速度很慢,編程中會遇到各類各樣的問題,我我的認爲問題最多的可能就是程序思路正確,但在編寫過程當中老是出現各類錯誤,在查找資料修改錯誤時,最好在網上或其餘參考資料上查找,由於Java課本太厚,可能好久都不會查到,可是在此以前你須要弄明白你出錯的緣由。網上有不少的學習資源,以前代老師給咱們推薦過mooc平臺翁愷老師的課程,並且博客園也是一個很好的平臺,有不少大神也會在博客園分享Java的知識,只要願意學,資源你永遠用不完,因此咱們本身要有學習的意識;
4.編程須要本身多動手,那算法就應該是動腦的時候的,給出算法,咱們要自覺地去分析它是在進行什麼操做,可不能夠用其它的代碼表達一樣的操做;
5.語法是須要不斷積累的,我這一點作的很是很差,我老是記不住東西,有時候碰見過的問題再出現時我只是以爲好熟悉,但是就是想不起怎麼解決,其實有時候笨方法或許真的頗有用,我記得翁愷老師在講課視頻中也說過:遇到好的編程語句就把它摘抄在小筆記本上,有時候老是記不住的東西寫一遍印象會深不少,並且就算你依然沒有記住,下次查找也會比較方便,慢慢地就會掌握這些知識了。
6.英語學的好的同窗對字母比較敏感,學編程語言也稍微容易,而且也容易經過英文提示找到代碼的錯誤,但英語學的很差的同窗也並非學很差編程語言。我本身的英語就比較差,因此一直對編程語言也有一種莫名的恐懼,助教學長是我很好的榜樣,他英語也不太好,但他的編程能力真的很優秀,我很崇拜他,也但願能夠像他同樣厲害。
其實每一個人的學習方法都不一樣,按照本身的實際狀況去制定適合本身的學習方法纔是最好的,但堅持是同樣的,堅持,天天進步一點,必定會有收穫的,以上是個人我的觀點,但願能夠幫助到你們,也但願本身這個Java菜鳥能夠學好這門語言。