201771010134杨其菊《面向对象程序设计》第十八周学习总结

实验十八  总复习

实验时间 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 AA;
 2 
 3 
 4 
 5 import java.awt.EventQueue;
 6 
 7 import javax.swing.JFrame;
 8 
 9 public class Main {
10     public static void main(String[] args) {
11         EventQueue.invokeLater(() -> {
12             DemoJFrame page = new DemoJFrame();
13         });
14     }
15 }
Main
 1 package AA;
 2 
 3 import java.awt.Dimension;
 4 import java.awt.Toolkit;
 5 import java.awt.Window;
 6 
 7 public class WinCenter {
 8     public static void center(Window win){
 9         Toolkit tkit = Toolkit.getDefaultToolkit();
10         Dimension sSize = tkit.getScreenSize();
11         Dimension wSize = win.getSize();
12         if(wSize.height > sSize.height){
13             wSize.height = sSize.height;
14         }
15         if(wSize.width > sSize.width){
16             wSize.width = sSize.width;
17         }
18         win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
19     }
20 }
WinCenter
  1 package AA;
  2 
  3 import java.awt.Dimension;
  4 import java.awt.FlowLayout;
  5 import java.awt.GridLayout;
  6 
  7 import javax.swing.BorderFactory;
  8 import javax.swing.ButtonGroup;
  9 import javax.swing.JButton;
 10 import javax.swing.JCheckBox;
 11 import javax.swing.JComboBox;
 12 import javax.swing.JFrame;
 13 import javax.swing.JLabel;
 14 import javax.swing.JPanel;
 15 import javax.swing.JRadioButton;
 16 import javax.swing.JTextField;
 17 
 18 public class DemoJFrame extends JFrame {
 19     private JPanel jPanel1;
 20     private JPanel jPanel2;
 21     private JPanel jPanel3;
 22     private JPanel jPanel4;
 23     private JTextField fieldname;
 24     private JComboBox comboBox;
 25     private JTextField fieldadress;
 26     private ButtonGroup bg;
 27     private JRadioButton Male;
 28     private JRadioButton Female;
 29     private JCheckBox read;
 30     private JCheckBox sing;
 31     private JCheckBox dance;
 32 
 33     public DemoJFrame() {
 34         // 设置窗口大小
 35         this.setSize(800, 400);
 36         // 设置可见性
 37         this.setVisible(true);
 38         // 设置标题
 39         this.setTitle("编程练习一");
 40         // 设置关闭操作
 41         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 42         // 设置窗口居中
 43         WinCenter.center(this);
 44         // 创建四个面板对象
 45         jPanel1 = new JPanel();
 46         setJPanel1(jPanel1);                
 47         jPanel2 = new JPanel();
 48         setJPanel2(jPanel2);
 49         jPanel3 = new JPanel();
 50         setJPanel3(jPanel3);
 51         jPanel4 = new JPanel();
 52         setJPanel4(jPanel4);
 53         // 设置容器的为流布局
 54         FlowLayout flowLayout = new FlowLayout();
 55         this.setLayout(flowLayout);
 56         // 将四个面板添加到容器中
 57         this.add(jPanel1);
 58         this.add(jPanel2);
 59         this.add(jPanel3);
 60         this.add(jPanel4);
 61 
 62     }
 63 
 64     /*
 65      * 设置面一
 66      */
 67     private void setJPanel1(JPanel jPanel) {
 68         // TODO 自动生成的方法存根
 69         jPanel.setPreferredSize(new Dimension(700, 45));
 70         // 给面板的布局设置为网格布局 一行4列
 71         jPanel.setLayout(new GridLayout(1, 4));
 72         
 73         JLabel name = new JLabel("name:");
 74         name.setSize(100, 50);
 75         fieldname = new JTextField("");
 76         fieldname.setSize(80, 20);
 77         
 78         JLabel study = new JLabel("qualification:");
 79         comboBox = new JComboBox();
 80         comboBox.addItem("初中");
 81         comboBox.addItem("高中");
 82         comboBox.addItem("本科");
 83         jPanel.add(name);
 84         jPanel.add(fieldname);
 85         jPanel.add(study);
 86         jPanel.add(comboBox);
 87 
 88     }
 89 
 90     /*
 91      * 设置面板二
 92      */
 93     private void setJPanel2(JPanel jPanel) {
 94         // TODO 自动生成的方法存根
 95         jPanel.setPreferredSize(new Dimension(700, 50));
 96         // 给面板的布局设置为网格布局 一行4列
 97         jPanel.setLayout(new GridLayout(1, 4));
 98         
 99         JLabel name = new JLabel("address:");
100         fieldadress = new JTextField();
101         fieldadress.setPreferredSize(new Dimension(150, 50));
102         
103         JLabel study = new JLabel("hobby:");
104         JPanel selectBox = new JPanel();
105         selectBox.setBorder(BorderFactory.createTitledBorder(""));
106         selectBox.setLayout(new GridLayout(3, 1));
107         read = new JCheckBox("reading");
108         sing = new JCheckBox("singing");
109         dance = new JCheckBox("danceing");
110         selectBox.add(read);
111         selectBox.add(sing);
112         selectBox.add(dance);
113         jPanel.add(name);
114         jPanel.add(fieldadress);
115         jPanel.add(study);
116         jPanel.add(selectBox);
117     }
118 
119     /*
120      * 设置面板三
121      */
122     private void setJPanel3(JPanel jPanel) {
123         // TODO 自动生成的方法存根
124         jPanel.setPreferredSize(new Dimension(700, 150));
125         FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
126         jPanel.setLayout(flowLayout);
127         JLabel sex = new JLabel("性别:");
128         JPanel selectBox = new JPanel();
129         selectBox.setBorder(BorderFactory.createTitledBorder(""));
130         selectBox.setLayout(new GridLayout(2, 1));
131         bg = new ButtonGroup();
132         Male = new JRadioButton("male");
133         Female = new JRadioButton("female");
134         bg.add(Male );
135         bg.add(Female);
136         selectBox.add(Male);
137         selectBox.add(Female);
138         jPanel.add(sex);
139         jPanel.add(selectBox);
140 
141     }
142 
143     /*
144      * 设置面板四
145      */
146     private void setJPanel4(JPanel jPanel) {
147         // TODO 自动生成的方法存根
148         jPanel.setPreferredSize(new Dimension(700, 150));
149         FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 50, 10);
150         jPanel.setLayout(flowLayout);
151         jPanel.setLayout(flowLayout);
152         JButton sublite = new JButton("提交");
153         JButton reset = new JButton("重置");
154         sublite.addActionListener((e) -> valiData());
155         reset.addActionListener((e) -> Reset());
156         jPanel.add(sublite);
157         jPanel.add(reset);
158     }
159 
160     /*
161      * 提交数据
162      */
163     private void valiData() {
164         // TODO 自动生成的方法存根
165         // 拿到数据
166         String name = fieldname.getText().toString().trim();
167         String xueli = comboBox.getSelectedItem().toString().trim();
168         String address = fieldadress.getText().toString().trim();
169         System.out.println(name);
170         System.out.println(xueli);
171         String hobbystring="";
172         if (read.isSelected()) {
173             hobbystring+="reading   ";
174         }
175         if (sing.isSelected()) {
176             hobbystring+="singing   ";
177         }
178         if (dance.isSelected()) {
179             hobbystring+="dancing  ";
180         }
181         System.out.println(address);
182         if (Male.isSelected()) {
183             System.out.println("male");
184         }
185         if (Female.isSelected()) {
186             System.out.println("female");
187         }
188         System.out.println(hobbystring);
189     }
190 
191     /*
192      * 重置
193      */
194     private void Reset() {
195         // TODO 自动生成的方法存根
196         fieldadress.setText(null);
197         fieldname.setText(null);
198         comboBox.setSelectedIndex(0);
199         read.setSelected(false);
200         sing.setSelected(false);
201         dance.setSelected(false);
202         bg.clearSelection();
203     }
204 }
View Code

 

练习2:采用GUI界面设计以下程序:

编制一个程序,将身份证号.txt 中的信息读入到内存中;

按姓名字典序输出人员信息;

查询最大年龄的人员信息;

查询最小年龄人员信息;

输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;

查询人员中是否有你的同乡。

输入身份证信息,查询所提供身份证号的人员信息,要求输入一个身份证数字时,查询界面就显示满足查询条件的查询结果,且随着输入的数字的增多,查询匹配的范围逐渐缩小。

 1 package AA;
 2 
 3 import java.awt.*;
 4 import javax.swing.*;
 5 
 6 public class IdTest {
 7     public static void main(String[] args) {
 8         EventQueue.invokeLater(() -> {
 9             JFrame frame = new Main();
10             frame.setTitle("身份证信息查询");
11             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
12             frame.setVisible(true);
13         });
14     }
15 }
IdTest
  1 package AA;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.InputStreamReader;
  7 import java.io.FileNotFoundException;
  8 import java.io.IOException;
  9 import java.util.ArrayList;
 10 import java.util.Arrays;
 11 import java.util.Collections;
 12 import java.util.Scanner;
 13 import java.awt.*;
 14 import javax.swing.*;
 15 import java.awt.event.*;
 16 
 17 public class Main extends JFrame {
 18     private static ArrayList<Student> studentlist;
 19     private static ArrayList<Student> list;
 20     private JPanel panel;
 21     private JPanel buttonPanel;
 22     private static final int DEFAULT_WITH = 900;
 23     private static final int DEFAULT_HEIGHT = 600;
 24 
 25     public Main() {
 26         studentlist = new ArrayList<>();
 27         Scanner scanner = new Scanner(System.in);
 28         File file = new File("G:\\身份证号.txt");
 29         try {
 30             FileInputStream fis = new FileInputStream(file);
 31             BufferedReader in = new BufferedReader(new InputStreamReader(fis));
 32             String temp = null;
 33             while ((temp = in.readLine()) != null) {
 34 
 35                 Scanner linescanner = new Scanner(temp);
 36 
 37                 linescanner.useDelimiter(" ");
 38                 String name = linescanner.next();
 39                 String number = linescanner.next();
 40                 String sex = linescanner.next();
 41                 String age = linescanner.next();
 42                 String province = linescanner.nextLine();
 43                 Student student = new Student();
 44                 student.setName(name);
 45                 student.setnumber(number);
 46                 student.setsex(sex);
 47                 int a = Integer.parseInt(age);
 48                 student.setage(a);
 49                 student.setprovince(province);
 50                 studentlist.add(student);
 51 
 52             }
 53         } catch (FileNotFoundException e) {
 54             System.out.println("文件找不到");
 55             e.printStackTrace();
 56         } catch (IOException e) {
 57             System.out.println("文件读取错误");
 58             e.printStackTrace();
 59         }
 60         panel = new JPanel();
 61         panel.setLayout(new BorderLayout());
 62         JTextArea A = new JTextArea();
 63         panel.add(A);
 64         add(panel, BorderLayout.NORTH);
 65         buttonPanel = new JPanel();
 66         
 67         buttonPanel.setLayout(new GridLayout(6, 2));
 68         JButton jButton = new JButton("字典排序");
 69         JButton jButton1 = new JButton("年龄最大和年龄最小");
 70         JLabel lab1 = new JLabel("寻找你的老乡");
 71         JTextField a1 = new JTextField();
 72         JLabel lab2 = new JLabel("寻找找同龄人:");
 73         JTextField a2 = new JTextField();
 74         JLabel lab3 = new JLabel("输入身份证号码查询信息:");
 75         JTextField a3 = new JTextField();
 76         JButton jButton2 = new JButton("退出");
 77         
 78         jButton.addActionListener(new ActionListener() {
 79             public void actionPerformed(ActionEvent e) {
 80                 Collections.sort(studentlist);
 81                 A.setText(studentlist.toString());
 82             }
 83         });
 84         jButton1.addActionListener(new ActionListener() {
 85             public void actionPerformed(ActionEvent e) {
 86                 int max = 0, min = 100;
 87                 int j, k1 = 0, k2 = 0;
 88                 for (int i = 1; i < studentlist.size(); i++) {
 89                     j = studentlist.get(i).getage();
 90                     if (j > max) {
 91                         max = j;
 92                         k1 = i;
 93                     }
 94                     if (j < min) {
 95                         min = j;
 96                         k2 = i;
 97                     }
 98 
 99                 }
100                A.setText("年龄最大:" + studentlist.get(k1) + "年龄最小:" + studentlist.get(k2));
101             }
102         });
103         jButton2.addActionListener(new ActionListener() {
104             public void actionPerformed(ActionEvent e) {
105                 dispose();
106                 System.exit(0);
107             }
108         });
109         a1.addActionListener(new ActionListener() {
110             public void actionPerformed(ActionEvent e) {
111                 String find = a1.getText();
112                 String text="";
113                 String place = find.substring(0, 3);
114                 for (int i = 0; i < studentlist.size(); i++) {
115                     if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) {
116                         text+="\n"+studentlist.get(i);
117                         A.setText("老乡:" + text);
118                     }
119                 }
120             }
121         });
122         a2.addActionListener(new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 String yourage = a2.getText();
125                 int a = Integer.parseInt(yourage);
126                 int near = agenear(a);
127                 int value = a - studentlist.get(near).getage();
128                 A.setText("年龄相近:" + studentlist.get(near));
129             }
130         });
131         a3.addActionListener(new ActionListener() {
132             public void actionPerformed(ActionEvent e) {
133                 list = new ArrayList<>();
134                 Collections.sort(studentlist);
135                 String key = a3.getText();
136                 for (int i = 1; i < studentlist.size(); i++) {
137                     if (studentlist.get(i).getnumber().contains(key)) {                        
138                         list.add(studentlist.get(i));                        
139                         A.setText("结果:\n" + list);
140                         
141                     }                    
142                 }
143             }
144         });
145         buttonPanel.add(jButton);
146         buttonPanel.add(jButton1);
147         buttonPanel.add(lab1);
148         buttonPanel.add(a1);
149         buttonPanel.add(lab2);
150         buttonPanel.add(a2);
151         buttonPanel.add(lab3);
152         buttonPanel.add(a3);
153         buttonPanel.add(jButton2);
154         add(buttonPanel, BorderLayout.SOUTH);
155         setSize(DEFAULT_WITH, DEFAULT_HEIGHT);
156     }
157 
158     public static int agenear(int age) {
159         int min = 53, value = 0, k = 0;
160         for (int i = 0; i < studentlist.size(); i++) {
161             value = studentlist.get(i).getage() - age;
162             if (value < 0)
163                 value = -value;
164             if (value < min) {
165                 min = value;
166                 k = i;
167             }
168         }
169         return k;
170     }
171 
172 }
Main
 1 package AA;
 2 
 3 public class Student implements Comparable<Student> {
 4 
 5     private String name;
 6     private String number ;
 7     private String sex ;
 8     private int age;
 9     private String province;
10    
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getnumber() {
18         return number;
19     }
20     public void setnumber(String number) {
21         this.number = number;
22     }
23     public String getsex() {
24         return sex ;
25     }
26     public void setsex(String sex ) {
27         this.sex =sex ;
28     }
29     public int getage() {
30 
31         return age;
32         }
33         public void setage(int age) {
34            
35         this.age= age;
36         }
37 
38     public String getprovince() {
39         return province;
40     }
41     public void setprovince(String province) {
42         this.province=province ;
43     }
44 
45     public int compareTo(Student o) {
46        return this.name.compareTo(o.getName());
47     }
48 
49     public String toString() {
50         return  name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";
51     }    
52 }
Student

练习3:采用GUI界面设计以下程序

编写一个计算器类,可以完成加、减、乘、除的操作

利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;

将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt

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

任务3:本学期课程已结束,请汇总《面向对象程序设计课程学习进度条》的数据,统计个人专业能力提升的数据。并从学习内容、学习方法、学习心得几个方面进行课程学习总结,也希望你对课程的不足提出建议和意见。

 学习内容:

Java语言特点与开发环境配置

Java基本程序结构

Java面向对象程序结构

类、类间关系、类图 

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并发程序设计

Java应用程序部署

学习方法:

      理论与实践相结合,理论学习作基础,实验实践学习为重点。

      主动结合教材,网络平台资源等自主学习,再结合老师和助教讲解,演示。

学习心得:

               本学期的学习,我掌握了基础的java知识,解决一些很基本的实际问题,但仅仅知识基础部分,要将其用于复杂一些的项目,还需要继续进行java的学习,我有计划在本次寒假继续学习一些java的相关课程。

建议和意见:

              我很喜欢这样的教学方式,觉得这样的机制是挺完美的,但本学期的效果远没有预想的好,主要原因在我们学生的自主性不足。所以是得有些环节没达到该有的意义。

 

猜你喜欢

转载自www.cnblogs.com/yqj-yf-111/p/10199194.html
今日推荐