201771010102 常惠琢 2018面向对象程序设计(JAVA)第18周学习总结
Posted hongyanohongyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
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()+" "+"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(" "+"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(" "); 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 }
练习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)+" "+"年龄最小: "+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+" "+sex+" "+age+" "+ID+" "+birthplace+" "; 271 272 } 273 }
练习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 }
任务3:本学期课程已结束,请汇总《面向对象程序设计课程学习进度条》的数据,统计个人专业能力提升的数据。并从学习内容、学习方法、学习心得几个方面进行课程学习总结,也希望你对课程的不足提出建议和意见。
实验总结:
实验结束了,我觉得课程确实不错,但是作业量还是有点多,所以最后作业效率感觉没那么高了。
以上是关于201771010102 常惠琢 2018面向对象程序设计(JAVA)第18周学习总结的主要内容,如果未能解决你的问题,请参考以下文章
201771010102 常惠琢 《2018面向对象程序设计(Java)》第9周学习总结
201771010102 常惠琢 《2018面向对象程序设计(Java)》第11周学习总结
201771010102 常惠琢《2018面向对象程序设计(Java)》第10周学习总结
常惠琢 201771010102《面向对象程序设计(java)》第七周学习总结