1. 程式人生 > >201771010102 常惠琢 2018面向物件程式設計(JAVA)第18周學習總結

201771010102 常惠琢 2018面向物件程式設計(JAVA)第18周學習總結

實驗十八  總複習

實驗時間 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)點選視窗關閉,程式退出。

 1 package 圖形使用者介面;
 2 
 3 import java.awt.EventQueue;
 4 
 5 import javax.swing.JFrame;
 6 
 7 public class Gui {
 8 
 9        public static void main(String[] args)
10        {
11 EventQueue.invokeLater(() -> { 12 JFrame frame = new FrameTest(); 13 frame.setTitle(" 常惠琢"); 14 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15 frame.setVisible(true); 16 }); 17 } 18 }
Gui
  1
package 圖形使用者介面; 2 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 import java.util.*; 7 import javax.swing.*; 8 9 class FrameTest extends JFrame 10 { 11 12 private JPanel panel; 13 private JTextArea text,text2; 14 private JRadioButton JRadioButton1,JRadioButton2; 15 private ButtonGroup ButtonGroup; 16 private JLabel JLabel; 17 private JCheckBox h1,h2,h3; 18 private JComboBox<String> JComboBox; 19 private JButton Button,Button2; 20 21 22 public FrameTest() 23 { 24 setSize(700,500); 25 panel=new JPanel(); 26 panel.setLayout(null); 27 28 ButtonGroup=new ButtonGroup(); 29 JRadioButton1=new JRadioButton("Male",false); JRadioButton1.setBounds(150,330, 80, 50); 30 JRadioButton2=new JRadioButton("Female",false); JRadioButton2.setBounds(150,300, 80,50); 31 ButtonGroup.add(JRadioButton1); 32 ButtonGroup.add(JRadioButton2); 33 34 addJLabel("sex:",100,300); 35 addJLabel("name:",100,50); 36 addJLabel("address:",100,150); 37 addJLabel("Qualification:",400,50); 38 addJLabel("Hobby:",400,150); 39 40 41 text=new JTextArea(1,1);text.setBounds(150,70, 120, 30);text.setLineWrap(true); 42 text2=new JTextArea(5,3);text2.setBounds(150,160, 130, 100);text2.setLineWrap(true); 43 44 45 h1=new JCheckBox("Reading");h1.setBounds(450,160,100,30); 46 h2=new JCheckBox("dancing");h2.setBounds(450,180,100,30); 47 h3=new JCheckBox("singing");h3.setBounds(450,200,100,30); 48 49 50 JComboBox=new JComboBox<>(); 51 JComboBox.addItem("Graduate"); 52 JComboBox.addItem("Graduate1"); 53 JComboBox.addItem("Graduate2"); 54 JComboBox.setBounds(500,65, 100, 20); 55 56 Button = new JButton("提交");Button.setBounds(200, 400, 100, 35); 57 Button2 = new JButton("重置");Button2.setBounds(400, 400, 100, 35); 58 59 Button.addActionListener(new Action1()); 60 Button2.addActionListener(new Action2()); 61 62 panel.add(h1); 63 panel.add(h2); 64 panel.add(h3); 65 panel.add(Button); 66 panel.add(Button2); 67 panel.add(JComboBox); 68 panel.add(text); 69 panel.add(text2); 70 panel.add(JRadioButton1); 71 panel.add(JRadioButton2); 72 add(panel); 73 74 75 } 76 77 78 public void addJLabel(String n,int a,int b) 79 { 80 JLabel = new JLabel(n); 81 JLabel.setBounds(a,b,100,50); 82 panel.add(JLabel); 83 } 84 85 private class Action1 implements ActionListener 86 { 87 public void actionPerformed(ActionEvent event) 88 { 89 System.out.println("name:"+text.getText()+"\n"+"address:"+text2.getText()); 90 System.out.println("Qualification:"+JComboBox.getSelectedItem()); 91 System.out.println("Hobby:"); 92 if(h1.isSelected()==true)System.out.print(h1.getText()); 93 if(h2.isSelected()==true)System.out.print(h2.getText()); 94 if(h3.isSelected()==true)System.out.print(h3.getText()); 95 System.out.println("\n"+"sex:"); 96 if(JRadioButton1.isSelected()==true)System.out.println(JRadioButton1.getText()); 97 if(JRadioButton2.isSelected()==true)System.out.println(JRadioButton2.getText()); 98 System.out.println("\n"); 99 } 100 } 101 private class Action2 implements ActionListener 102 { 103 public void actionPerformed(ActionEvent event) 104 { 105 text.setText(null); 106 text2.setText(null); 107 h1.setSelected(false); 108 h2.setSelected(false); 109 h3.setSelected(false); 110 ButtonGroup.clearSelection(); 111 JComboBox.setSelectedIndex(0); 112 } 113 } 114 private class Action21 implements ActionListener 115 { 116 public void actionPerformed(ActionEvent event) 117 { 118 text.setText(null); 119 text2.setText(null); 120 h1.setSelected(false); 121 h2.setSelected(false); 122 h3.setSelected(false); 123 ButtonGroup.clearSelection(); 124 JComboBox.setSelectedIndex(0); 125 } 126 } 127 }
FrameTest

練習2:採用GUI介面設計以下程式:

l 編制一個程式,將身份證號.txt 中的資訊讀入到記憶體中;

l 按姓名字典序輸出人員資訊;

l 查詢最大年齡的人員資訊;

l 查詢最小年齡人員資訊;

l 輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;

l 查詢人員中是否有你的同鄉。

l 輸入身份證資訊,查詢所提供身份證號的人員資訊,要求輸入一個身份證數字時,查詢介面就顯示滿足查詢條件的查詢結果,且隨著輸入的數字的增多,查詢匹配的範圍逐漸縮小。

  1 package unsynch;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.io.BufferedReader;
  7 import java.io.File;
  8 import java.io.FileInputStream;
  9 import java.io.FileNotFoundException;
 10 import java.io.IOException;
 11 import java.io.InputStreamReader;
 12 import java.util.*;
 13 import java.util.Timer;
 14 import javax.swing.*;
 15 
 16 public class Main1 extends JFrame
 17 {
 18     private static ArrayList<Person> Personlist;
 19     
 20     
 21     Scanner scanner = new Scanner(System.in);
 22     File file = new File("F:\\身份證號.txt");
 23     
 24     private JPanel Panel;
 25     private JLabel JLabel1;
 26     private JButton Button,Button2,Button3;
 27     private JTextArea text,text1,text2,text3;
 28     boolean tru=true;
 29     
 30     
 31     
 32     public  Main1() {
 33 
 34         
 35         Panel = new JPanel();Panel.setLayout(null);
 36         Button = new JButton("1:按姓名字典序輸出人員資訊");
 37         Button2 = new JButton("2:查詢最大年齡與最小年齡人員資訊");
 38         Button3 = new JButton("查詢相近年齡");
 39         JLabel1 = new JLabel("輸入身份證號或者地址查詢");JLabel1.setBounds(900, 50, 400, 30);
 40         
 41         text=new JTextArea(30,80);text.setBounds(50, 180, 700, 700);
 42         text1=new JTextArea(1,30);text1.setBounds(900, 80, 400, 30);
 43         text2=new JTextArea(30,80);text2.setBounds(900,180,700, 700);
 44         text3=new JTextArea(30,80);text3.setBounds(420,100,200,40);
 45        
 46         Button.addActionListener(new Action());Button.setBounds(50,50,300,40);
 47         Button2.addActionListener(new Action1());Button2.setBounds(50,100,300,40);
 48         Button3.addActionListener(new Action2());Button3.setBounds(650,100,120,40);
 49         Panel.add(JLabel1);
 50         Panel.add(Button);
 51         Panel.add(Button2);
 52         Panel.add(Button3);
 53         Panel.add(text);
 54         Panel.add(text2);           Panel.add(text1);
 55         Panel.add(text3);
 56         add(Panel);
 57         
 58         
 59         Timer timer = new Timer();      
 60         TimerTask timeTask=new TimerTask() {
 61             
 62              @Override
 63             public void run()
 64              {             
 65                      // TODO Auto-generated method stub
 66                      text2.setText(null);
 67                      String place=text1.getText().toString().trim();
 68                      for (int i = 0; i <Personlist.size(); i++) 
 69                      {                         
 70                          String Str=(String)Personlist.get(i).getbirthplace();
 71                          if(Str.contains(place)&&!place.equals("")) 
 72                              {
 73                              text2.append(Personlist.get(i).toString());
 74                              }  
 75                     }      
 76                      for (int i = 0; i <Personlist.size(); i++) 
 77                      {
 78                          
 79                          String Str=(String)Personlist.get(i).getID();
 80                          if(Str.contains(place)&&!place.equals("")) 
 81                              {
 82                              text2.append(Personlist.get(i).toString());
 83                              }  
 84                     }    
 85                      
 86             }
 87             
 88         };timer.schedule(timeTask, 0,100);
 89      
 90         Personlist = new ArrayList<>();       
 91         try {
 92             FileInputStream fis = new FileInputStream(file);
 93             BufferedReader in = new BufferedReader(new InputStreamReader(fis));
 94             String temp = null;
 95             while ((temp = in.readLine()) != null) {            
 96                 Scanner linescanner = new Scanner(temp);               
 97                 linescanner.useDelimiter(" ");    
 98                 String name = linescanner.next();
 99                 String ID = linescanner.next();
100                 String sex = linescanner.next();
101                 String age = linescanner.next();
102                 String place =linescanner.nextLine();
103                 Person Person = new Person();
104                 Person.setname(name);
105                 Person.setID(ID);
106                 Person.setsex(sex);
107                 int a = Integer.parseInt(age);
108                 Person.setage(a);
109                 Person.setbirthplace(place);
110                 Personlist.add(Person);
111 
112             }
113         } catch (FileNotFoundException e) {
114             System.out.println("查詢不到資訊");
115             e.printStackTrace();
116         } catch (IOException e) {
117             System.out.println("資訊讀取有誤");
118             e.printStackTrace();
119         }
120  
121         
122     }
123     
124     
125 
126 
127     private class Action implements ActionListener
128     {
129     public void actionPerformed(ActionEvent event)
130         {        
131          text.setText(null);
132          Collections.sort(Personlist);
133          text.append(Personlist.toString());
134         }
135 
136     }         
137 
138     private class Action1 implements ActionListener
139         {
140         public void actionPerformed(ActionEvent event)
141             {        
142             text.setText(null);
143             int max=0,min=100;int j,k1 = 0,k2=0;
144             for(int i=1;i<Personlist.size();i++)
145             {
146                 j=Personlist.get(i).getage();
147                if(j>max)
148                {
149                    max=j; 
150                    k1=i;
151                }
152                if(j<min)
153                {
154                    min=j; 
155                    k2=i;
156                }
157             }  
158             text.append("年齡最大:   "+Personlist.get(k1)+"\n"+"年齡最小:  "+Personlist.get(k2));     
159             }
160    
161         }          
162 
163   private class Action2 implements ActionListener
164     {
165     public void actionPerformed(ActionEvent event)
166         {        
167          text.setText(null);
168          int a = Integer.parseInt(text3.getText().toString().trim());         
169          int d_value=a-Personlist.get(agenear(a)).getage();
170          
171          for (int i = 0; i < Personlist.size(); i++)
172          {
173              int p=Personlist.get(i).getage()-a;
174             
175              if(p==d_value||-p==d_value) text.append(Personlist.get(i).toString());
176          } 
177         }
178 
179     } 
180     
181     
182     public static int agenear(int age) {
183         
184         int j=0,min=53,d_value=0,k=0;
185          for (int i = 0; i < Personlist.size(); i++)
186          {
187              d_value=Personlist.get(i).getage()-age;
188              if(d_value<0) d_value=-d_value; 
189              if (d_value<min) 
190              {
191                 min=d_value;                   k=i;
192              }
193 
194           }    return k;
195          
196       }
197 
198 }
199 package unsynch;
200 
201     import java.awt.Dimension;
202     import java.awt.EventQueue;
203     import java.awt.Toolkit;
204 
205     import javax.swing.JFrame;
206 
207     public class Out {
208 
209          public static void main (String args[])
210             {
211                  Toolkit t=Toolkit.getDefaultToolkit();
212                 Dimension s=t.getScreenSize(); 
213                 EventQueue.invokeLater(() -> {
214                     JFrame frame = new Main1();
215                     frame.setBounds(0, 0,(int)s.getWidth(),(int)s.getHeight());
216                     frame.setTitle("身份查詢系統");
217                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
218                     frame.setVisible(true);
219                  });        
220             }
221      
222     }
223 package unsynch;
224 
225 public class Person implements Comparable<Person> {
226         private String name;
227         private String ID;
228         private int age;
229         private String sex;
230         private String birthplace;
231 
232         public String getname() {
233         return name;
234         }
235         public void setname(String name) {
236         this.name = name;
237         }
238         public String getID() {
239         return ID;
240         }
241         public void setID(String ID) {
242         this.ID= ID;
243         }
244         public int getage() {
245 
246         return age;
247         }
248         public void setage(int age) {
249         this.age= age;
250         }
251         public String getsex() {
252         return sex;
253         }
254         public void setsex(String sex) {
255         this.sex= sex;
256         }
257         public String getbirthplace() {
258         return birthplace;
259         }
260         public void setbirthplace(String birthplace) {
261         this.birthplace= birthplace;
262         }
263 
264         public int compareTo(Person o) {
265            return this.name.compareTo(o.getname());
266 
267         }
268 
269         public String toString() {
270             return  name+"\t"+sex+"\t"+age+"\t"+ID+"\t"+birthplace+"\n";
271 
272         }
273   }
View Code

練習3:採用GUI介面設計以下程式

l 編寫一個計算器類,可以完成加、減、乘、除的操作

l 利用計算機類,設計一個小學生100以內數的四則運算練習程式,由計算機隨機產生10道加減乘除練習題,學生輸入答案,由程式檢查答案是否正確,每道題正確計10分,錯誤不計分,10道題測試結束後給出測試總分;

l 將程式中測試練習題及學生答題結果輸出到檔案,檔名為test.txt。

  1 import java.awt.Dimension;
  2 import java.awt.EventQueue;
  3 import java.awt.Toolkit;
  4 
  5 import javax.swing.JFrame;
  6 
  7 public class main {
  8 
  9      public static void main (String args[])
 10         {
 11              Toolkit t=Toolkit.getDefaultToolkit();
 12             Dimension s=t.getScreenSize(); 
 13             EventQueue.invokeLater(() -> {
 14                 JFrame frame = new nic();
 15                 frame.setBounds(0, 0,(int)s.getWidth()/2,(int)s.getHeight()/2);
 16                 frame.setTitle("大師");
 17                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 18                 frame.setVisible(true);
 19              });        
 20         }
 21  
 22 }
 23 import java.awt.Font;
 24 import java.awt.event.ActionEvent;
 25 import java.awt.event.ActionListener;
 26 import java.io.FileNotFoundException;
 27 import java.io.PrintWriter;
 28 import java.util.Collections;
 29 import java.util.Scanner;
 30 
 31 import javax.swing.*;
 32 
 33 import java.math.*;
 34 
 35 
 36 public class nic extends JFrame {
 37     
 38     private String[] c=new String[10];
 39     private String[] c1=new String[11];
 40     private int[] list=new int[10];
 41     int i=0,i1=0,sum = 0;
 42     private PrintWriter out = null;
 43     private JTextArea text,text1;
 44     private int counter;
 45     
 46     public nic()  {
 47         JPanel Panel = new JPanel();Panel.setLayout(null);
 48         JLabel JLabel1=new JLabel("...");JLabel1.setBounds(500, 800, 400, 30);JLabel1.setFont(new Font("Courier",Font.PLAIN,35));
 49         JButton Button = new JButton("題目");Button.setBounds(50,150,150,50);Button.setFont(new Font("Courier",Font.PLAIN,20)); Button.addActionListener(new Action());
 50         JButton Button2 = new JButton("確定");Button2.setBounds(300,150,150,50);Button2.setFont(new Font("Courier",Font.PLAIN,20));Button2.addActionListener(new Action1());
 51         JButton Button3 = new JButton("讀出檔案");Button3.setBounds(500,150,150,50);Button3.setFont(new Font("Courier",Font.PLAIN,20));Button3.addActionListener(new Action2());
 52          text=new JTextArea(30,80);text.setBounds(30, 50, 200, 50);text.setFont(new Font("Courier",Font.PLAIN,35));
 53          text1=new JTextArea(30,80);text1.setBounds(270, 50, 200, 50);text1.setFont(new Font("Courier",Font.PLAIN,35));
 54 
 55          Panel.add(text);
 56          Panel.add(text1);
 57 
 58          Panel.add(Button);
 59          Panel.add(Button2);
 60          Panel.add(Button3);
 61          Panel.add(JLabel1);
 62          add(Panel);             
 63     }
 64     
 65     private class Action implements ActionListener
 66     {
 67     public void actionPerformed(ActionEvent event)
 68         {        
 69         text1.setText("0");
 70         if(i<11) {
 71         
 72         int a = 1+(int)(Math.random() * 99);
 73         int b = 1+(int)(Math.random() * 99);
 74         int m= (int) Math.round(Math.random() * 3);
 75       switch(m)
 76       {
 77       case 1:
 78           while(a<b){  b = (int) Math.round(Math.random() * 100);a = (int) Math.round(Math.random() * 100); }  
 79           c[i]=((i+1)+":"+a+"/"+b+"=");
 80             list[(i+1)]=Math.floorDiv(a, b);
 81             text.setText((i+1)+":"+a+"/"+b+"=");
 82             i++;
 83             
 84           break; 
 85       case 2:
 86           c[i]=((i+1)+":"+a+"*"+b+"=");
 87           list[(i+1)]=Math.multiplyExact(a, b);
 88           text.setText((i+1)+":"+a+"*"+b+"=");        
 89          i++;
 90           break;
 91       case 3:
 92           c[i]=((i+1)+":"+a+"+"+b+"=");
 93           list[(i+1)]=Math.addExact(a, b);
 94          text.setText((i+1)+":"+a+"+"+b+"=");
 95          i++;
 96           break ;
 97       case 4:
 98           while(a<=b){  b = (int) Math.round(Math.random() * 100);a = (int) Math.round(Math.random() * 100); }    
 99           c[i]=((i+1)+":"+a+"-"+b+"=");
100           text.setText((i+1)+":"+a+"-"+b+"=");
101           list[(i+1)]=Math.subtractExact(a, b);
102           i++;
103 
104           break ;
105           }
106         }
107       }
108     }      
109     private class Action1 implements ActionListener
110     {
111     public void actionPerformed(ActionEvent event)
112         {    
113         if(i<10) {
114              
115         String daan=text1.getText().toString().trim();
116         int a = Integer.parseInt(daan);
117         if(text1.getText()!="  ") {
118          if(list[i1]==a) sum+=10;
119          }
120         
121          c1[i1]=daan;
122          
123          i1++;
124          
125         }
126       }
127     }   
View Code

任務3:本學期課程已結束,請彙總《面向物件程式設計課程學習進度條》的資料,統計個人專業能力提升的資料。並從學習內容、學習方法、學習心得幾個方面進行課程學習總結,也希望你對課程的不足提出建議和意見。

實驗總結:

實驗結束了,我覺得課程確實不錯,但是作業量還是有點多,所以最後作業效率感覺沒那麼高了。