第十八週java實驗作業
實驗十八 總複習
實驗時間 2018-12-30
1、實驗目的與要求
(1) 綜合掌握java基本程式結構;
(2) 綜合掌握java面向物件程式設計特點;
(3) 綜合掌握java GUI 程式設計結構;
(4) 綜合掌握java多執行緒程式設計模型;
(5) 綜合程式設計練習。
2、實驗內容和步驟
任務1:填寫課程課後調查問卷,網址:https://www.wjx.cn/jq/33108969.aspx。
任務2:綜合程式設計練習
練習1:設計一個使用者資訊採集程式,要求如下:
(1) 使用者資訊輸入介面如下圖所示:
(1)使用者點選提交按鈕時,使用者輸入資訊顯示控制檯介面;
(2)使用者點選重置按鈕後,清空使用者已輸入資訊;
(3)點選視窗關閉,程式退出。
package bounceThread; 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 bounceThread; import java.awt.EventQueue; import javax.swing.JFrame; public class Mian { public static void main(String[] args) { EventQueue.invokeLater(() -> { DemoJFrame page = new DemoJFrame(); page.setTitle("使用者資訊錄入框");// 設定標題 page.setSize(800, 400);// 設定視窗大小 page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定關閉操作 page.setVisible(true);//設定可見性 }); } }
package bounceThread; 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 man; private JRadioButton woman; private JCheckBox sing; private JCheckBox dance; private JCheckBox draw; public DemoJFrame() { //設定視窗居中 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) { 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) { 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) { 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(); man = new JRadioButton("男"); woman = new JRadioButton("女"); bg.add(man); bg.add(woman); selectBox.add(man); selectBox.add(woman); 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() { // 拿到資料(控制檯輸出) String name = fieldname.getText().toString().trim(); String diploma = comboBox.getSelectedItem().toString().trim(); String address = fieldadress.getText().toString().trim(); System.out.println("姓名:"+name); System.out.println("學歷:"+diploma); String hobbystring=""; if (sing.isSelected()) { hobbystring+="唱歌 "; } if (dance.isSelected()) { hobbystring+="跳舞 "; } if (draw.isSelected()) { hobbystring+="畫畫 "; } System.out.println("地址:"+(address)); if (man.isSelected()) { System.out.println("性別:"+"男"); } if (woman.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介面設計以下程式:
編制一個程式,將身份證號.txt 中的資訊讀入到記憶體中;
按姓名字典序輸出人員資訊;
查詢最大年齡的人員資訊;
查詢最小年齡人員資訊;
輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;
查詢人員中是否有你的同鄉。
輸入身份證資訊,查詢所提供身份證號的人員資訊,要求輸入一個身份證數字時,查詢介面就顯示滿足查詢條件的查詢結果,且隨著輸入的數字的增多,查詢匹配的範圍逐漸縮小。
package bounceThread; import java.awt.*; import javax.swing.*; public class CardTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new Main(); frame.setTitle("身份證"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
package bounceThread; public class Card implements Comparable<Card> { private String name; private String id; private String sex; private int age; private String area; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public int compareTo(Card c) { return this.name.compareTo(c.getName()); } public String toString() { return name + "\t" + id + "\t" + sex + "\t" + age + "\t" + area + "\n"; } }
package bounceThread; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class Main extends JFrame { private static ArrayList<Card> cardlist; private static ArrayList<Card> list; private JPanel panel; private JPanel Panel1; private static final int DEFAULT_WITH = 600; private static final int DEFAULT_HEIGHT = 300; public Main() { cardlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("C:\\身份證號.txt"); 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 area = linescanner.nextLine(); Card card = new Card(); card.setName(name); card.setId(id); card.setSex(sex); int a = Integer.parseInt(age); card.setAge(a); card.setArea(area); cardlist.add(card); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("資訊檔案找不到"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("資訊檔案讀取錯誤"); e.printStackTrace(); } panel = new JPanel(); panel.setLayout(new BorderLayout()); JTextArea jt = new JTextArea(); panel.add(jt); add(panel, BorderLayout.NORTH); Panel1 = new JPanel(); Panel1.setLayout(new GridLayout(1, 9)); JButton jButton = new JButton("字典排序"); JButton jButton1 = new JButton("年齡最大和年齡最小"); JLabel lab = new JLabel("尋找老鄉:", Label.LEFT); JTextField jt1 = new JTextField(); JLabel lab1 = new JLabel("尋找年齡相近的人:", Label.LEFT); JTextField jt2 = new JTextField(); JLabel lab2 = new JLabel("輸入你的身份證號碼:", Label.LEFT); JTextField jt3 = new JTextField(); JButton jButton2 = new JButton("退出"); jButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自動生成的方法存根 Collections.sort(cardlist); jt.setText(cardlist.toString()); } }); jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO 自動生成的方法存根 int max = 0, min = 100; int j, k1 = 0, k2 = 0; for (int i = 1; i < cardlist.size(); i++) { j = cardlist.get(i).getAge(); if (j > max) { max = j; k1 = i; } if (j < min) { min = j; k2 = i; } } jt.setText("年齡最大:" + cardlist.get(k1) + "年齡最小:" + cardlist.get(k2)); } }); jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); System.exit(0); } }); jt1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String find = jt1.getText(); String text = ""; String place = find.substring(0, 3); for (int i = 0; i < cardlist.size(); i++) { if (cardlist.get(i).getArea().substring(1, 4).equals(place)) { text += "\n" + cardlist.get(i); jt.setText("老鄉:" + text); } } } }); jt2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String yourage = jt2.getText(); int a = Integer.parseInt(yourage); int near = agenear(a); jt.setText("年齡相近:" + cardlist.get(near)); } }); jt3.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { // TODO 自動生成的方法存根 list = new ArrayList<>(); Collections.sort(cardlist); String key = jt3.getText(); for (int i = 1; i < cardlist.size(); i++) { if (cardlist.get(i).getId().contains(key)) { list.add(cardlist.get(i)); jt.setText("你可能是:\n" + list); } } } }); Panel1.add(jButton); Panel1.add(jButton1); Panel1.add(lab); Panel1.add(jt1); Panel1.add(lab1); Panel1.add(jt2); Panel1.add(lab2); Panel1.add(jt3); Panel1.add(jButton2); add(Panel1, BorderLayout.SOUTH); setSize(DEFAULT_WITH, DEFAULT_HEIGHT); } public static int agenear(int age) { int j = 0, min = 100, value = 0, k = 0; for (int i = 0; i < cardlist.size(); i++) { value = cardlist.get(i).getAge() - age; if (value < 0) value = -value; if (value < min) { min = value; k = i; } } return k; } }
執行結果:
練習3:採用GUI介面設計以下程式
編寫一個計算器類,可以完成加、減、乘、除的操作
利用計算機類,設計一個小學生100以內數的四則運算練習程式,由計算機隨機產生10道加減乘除練習題,學生輸入答案,由程式檢查答案是否正確,每道題正確計10分,錯誤不計分,10道題測試結束後給出測試總分;
將程式中測試練習題及學生答題結果輸出到檔案,檔名為test.txt。
package bounceThread; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class Demo extends JFrame { int i = 0, i1 = 0, k = 0, sum = 0; private PrintWriter out = null; private String[] c1 = new String[10]; private String[] c2 = new String[10]; public Demo() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 3)); JTextArea jt1 = new JTextArea(); JTextField jt2 = new JTextField(); JTextArea jt3 = new JTextArea(); panel.add(jt1); panel.add(jt2); panel.add(jt3); add(panel, BorderLayout.NORTH); JPanel panel1 = new JPanel(); panel1.setLayout(new GridLayout(1, 2)); Button button1 = new Button("生成題目"); Button button2 = new Button("生成檔案"); panel1.add(button1); panel1.add(button2); add(panel1, BorderLayout.SOUTH); setSize(600, 300); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jt2.setText(null); jt3.setText(null); if (i < 10) { int a = (int) (Math.random() * 100); int b = (int) (Math.random() * 100); int m = (int) Math.round(Math.random() * 3); switch (m) { case 0: while (b == 0 || a % b != 0) { b = (int) Math.round(Math.random() * 100); a = (int) Math.round(Math.random() * 100); } jt1.setText(i + 1 + ": " + a + "/" + b + "="); c1[i] = jt1.getText(); k = a / b; i++; break; case 1: jt1.setText(i + 1 + ": " + a + "*" + b + "="); c1[i] = jt1.getText(); k = a * b; i++; break; case 2: jt1.setText(i + 1 + ": " + a + "+" + b + "="); c1[i] = jt1.getText(); k = a + b; i++; break; case 3: while (a < b) { int x = a; a = b; b = x; } jt1.setText(i + 1 + ": " + a + "-" + b + "="); c1[i] = jt1.getText(); k = a - b; i++; break; } } } }); jt2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (i < 11) { int find = Integer.parseInt(jt2.getText()); String d = jt2.getText().toString().trim(); if (jt2.getText() != "") { if (find == k) { sum += 10; jt3.setText("答案正確"); } else jt3.setText("答案錯誤"); } c2[i1] = d; i1++; } } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { out = new PrintWriter("text.txt"); } catch (FileNotFoundException e1) { e1.printStackTrace(); } for (int counter = 0; counter < 10; counter++) { out.println(c1[counter] + c2[counter]); } out.println("成績為" + sum); out.close(); } }); } }
package bounceThread; import java.awt.*; import javax.swing.*; public class DemoTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new Demo(); frame.setTitle("計算器"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } }
執行結果:
任務3:本學期課程已結束,請彙總《面向物件程式設計課程學習進度條》的資料,統計個人專業能力提升的資料。並從學習內容、學習方法、學習心得幾個方面進行課程學習總結,也希望你對課程的不足提出建議和意見。
學期學習總結:學習內容
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理論知識,實驗課上可以自己進行java程式碼的註釋,同時有助教進行一些程式設計示範
(2)課下學習:主要有周末完成實驗作業,同時助教也會直播進行程式設計示範,還有利用浙江大學翁凱老師的視訊資料進行自主學習。
個人總結:通過本學期的學習,我對java這門程式語言有了一定的瞭解和認識,遺憾的是自己對著門語言的自主應用能力還不是很好,程式設計能力非常欠缺,在今後的學習生涯中,java還有很大的應用價值,我不會放棄對這門語言的繼續學習,希望自己以後可以利用java解決一些小的實際問題。同時感謝老師和助教這一學習的教導。幫助我學到了很多知識。