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 1 package fff; 2 2 import java.awt.EventQueue; 3 3 4 4 import javax.swing.JFrame; 5 5 6 6 public class Main { 7 7 public static void main(String[] args) { 8 8 EventQueue.invokeLater(() -> { 9 9 DemoJFrame page = new DemoJFrame(); 1010 }); 1111 } 1212 }
View Code

1 1 package fff; 2 2 import java.awt.Dimension; 3 3 import java.awt.Toolkit; 4 4 import java.awt.Window; 5 5 6 6 public class WinCenter { 7 7 public static void center(Window win){ 8 8 Toolkit tkit = Toolkit.getDefaultToolkit(); 9 9 Dimension sSize = tkit.getScreenSize(); 1010 Dimension wSize = win.getSize(); 1111 if(wSize.height > sSize.height){ 1212 wSize.height = sSize.height; 1313 } 1414 if(wSize.width > sSize.width){ 1515 wSize.width = sSize.width; 1616 } 1717 win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2); 1818 } 1919 }
View Code

1 1 package fff; 2 2 import java.awt.*; 3 3 import java.awt.event.*; 4 4 import javax.swing.*; 5 5 import javax.swing.border.*; 6 6 public class DemoJFrame extends JFrame { 7 7 public DemoJFrame() { 8 8 JPanel panel1 = new JPanel(); 9 9 panel1.setPreferredSize(new Dimension(700, 45)); 10 10 panel1.setLayout(new GridLayout(1, 4)); 11 11 JLabel label1 = new JLabel("Name:"); 12 12 JTextField j1 = new JTextField(""); 13 13 JLabel label2 = new JLabel("Qualification:"); 14 14 JComboBox<Object> j2 = new JComboBox<>(); 15 15 j2.addItem("chuzhong"); 16 16 j2.addItem("gaozhong"); 17 17 j2.addItem("undergraduate"); 18 18 panel1.add(label1); 19 19 panel1.add(j1); 20 20 panel1.add(label2); 21 21 panel1.add(j2); 22 22 23 23 JPanel panel2 = new JPanel(); 24 24 panel2.setPreferredSize(new Dimension(700, 65)); 25 25 panel2.setLayout(new GridLayout(1, 4)); 26 26 JLabel label3 = new JLabel("Address:"); 27 27 JTextArea j3 = new JTextArea(); 28 28 JLabel label4 = new JLabel("Hobby:"); 29 29 JPanel p = new JPanel(); 30 30 p.setLayout(new GridLayout(3, 1)); 31 31 p.setBorder(BorderFactory.createLineBorder(null)); 32 32 JCheckBox c1 = new JCheckBox("Reading"); 33 33 JCheckBox c2 = new JCheckBox("Singing"); 34 34 JCheckBox c3 = new JCheckBox("Dancing"); 35 35 p.add(c1); 36 36 p.add(c2); 37 37 p.add(c3); 38 38 panel2.add(label3); 39 39 panel2.add(j3); 40 40 panel2.add(label4); 41 41 panel2.add(p); 42 42 43 43 JPanel panel3 = new JPanel(); 44 44 panel3.setPreferredSize(new Dimension(700, 150)); 45 45 FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 70, 40); 46 46 panel3.setLayout(flowLayout1); 47 47 JLabel label5 = new JLabel("Sex:"); 48 48 JPanel p1 = new JPanel(); 49 49 p1.setLayout(new GridLayout(2,1)); 50 50 p1.setBorder(BorderFactory.createLineBorder(null)); 51 51 ButtonGroup bu = new ButtonGroup(); 52 52 JRadioButton jr1 = new JRadioButton("Male"); 53 53 JRadioButton jr2 = new JRadioButton("Female"); 54 54 bu.add(jr1); 55 55 bu.add(jr2); 56 56 p1.add(jr1); 57 57 p1.add(jr2); 58 58 panel3.add(label5); 59 59 panel3.add(p1); 60 60 add(panel1); 61 61 add(panel2); 62 62 add(panel3); 63 63 64 64 JPanel panel4 = new JPanel(); 65 65 panel4.setPreferredSize(new Dimension(700, 150)); 66 66 JButton b1 = new JButton("Validate"); 67 67 panel4.add(b1); 68 68 JButton b2 = new JButton("Reset"); 69 69 panel4.add(b2); 70 70 add(panel4); 71 71 72 72 FlowLayout flowLayout = new FlowLayout(); 73 73 this.setLayout(flowLayout); 74 74 this.setTitle("Students Detail"); 75 75 this.setBounds(300, 300, 800, 400); 76 76 this.setVisible(true); 77 77 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 78 78 79 79 b1.addActionListener(new ActionListener() { 80 80 81 81 82 82 public void actionPerformed(ActionEvent e) { 83 83 // TODO 自动生成的方法存根 84 84 String xueli = j2.getSelectedItem().toString(); 85 85 System.out.println("Name:" + j1.getText()); 86 86 System.out.println("Qualification:" + xueli); 87 87 String hobbystring = "Hobby:"; 88 88 if (c1.isSelected()) { 89 89 hobbystring += "Reading"; 90 90 } 91 91 if (c2.isSelected()) { 92 92 hobbystring += "Singing"; 93 93 } 94 94 if (c3.isSelected()) { 95 95 hobbystring += "Dancing"; 96 96 } 97 97 System.out.println("Address:" + j3.getText()); 98 98 if (jr1.isSelected()) { 99 99 System.out.println("Sex:Male"); 100100 } 101101 if (jr2.isSelected()) { 102102 System.out.println("Sex:Female"); 103103 } 104104 System.out.println(hobbystring); 105105 } 106106 }); 107107 b2.addActionListener(new ActionListener() { 108108 109109 public void actionPerformed(ActionEvent e) { 110110 // TODO 自动生成的方法存根 111111 j1.setText(null); 112112 j3.setText(null); 113113 j2.setSelectedIndex(0); 114114 c1.setSelected(false); 115115 c2.setSelected(false); 116116 c3.setSelected(false); 117117 bu.clearSelection(); 118118 } 119119 }); 120120 } 121121 122122 public static void main(String args[]) { 123123 new DemoJFrame(); 124124 } 125125 126126 }
View Code

练习2:采用GUI界面设计以下程序:
l 编制一个程序,将身份证号.txt 中的信息读入到内存中;
l 按姓名字典序输出人员信息;
l 查询最大年龄的人员信息;
l 查询最小年龄人员信息;
l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;
l 查询人员中是否有你的同乡。
l 输入身份证信息,查询所提供身份证号的人员信息,要求输入一个身份证数字时,查询界面就显示满足查询条件的查询结果,且随着输入的数字的增多,查询匹配的范围逐渐缩小。

1 1 package Demo; 2 2 3 3 import java.awt.*; 4 4 import javax.swing.*; 5 5 6 6 public class ButtonTest { 7 7 public static void main(String[] args) { 8 8 EventQueue.invokeLater(() -> { 9 9 JFrame frame = new Main(); 1010 frame.setTitle("身份证"); 1111 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 1212 frame.setVisible(true); 1313 }); 1414 } 1515 }
View Code

1 1 package Demo; 2 2 3 3 import java.io.BufferedReader; 4 4 import java.io.File; 5 5 import java.io.FileInputStream; 6 6 import java.io.InputStreamReader; 7 7 import java.io.FileNotFoundException; 8 8 import java.io.IOException; 9 9 import java.util.ArrayList; 10 10 import java.util.Arrays; 11 11 import java.util.Collections; 12 12 import java.util.Scanner; 13 13 import java.awt.*; 14 14 import javax.swing.*; 15 15 import java.awt.event.*; 16 16 17 17 public class Main extends JFrame { 18 18 private static ArrayList<Student> studentlist; 19 19 private static ArrayList<Student> list; 20 20 private JPanel panel; 21 21 private JPanel buttonPanel; 22 22 private static final int DEFAULT_WITH = 600; 23 23 private static final int DEFAULT_HEIGHT = 300; 24 24 25 25 public Main() { 26 26 studentlist = new ArrayList<>(); 27 27 Scanner scanner = new Scanner(System.in); 28 28 File file = new File("studentfile.txt"); 29 29 try { 30 30 FileInputStream fis = new FileInputStream(file); 31 31 BufferedReader in = new BufferedReader(new InputStreamReader(fis)); 32 32 String temp = null; 33 33 while ((temp = in.readLine()) != null) { 34 34 35 35 Scanner linescanner = new Scanner(temp); 36 36 37 37 linescanner.useDelimiter(" "); 38 38 String name = linescanner.next(); 39 39 String number = linescanner.next(); 40 40 String sex = linescanner.next(); 41 41 String age = linescanner.next(); 42 42 String province = linescanner.nextLine(); 43 43 Student student = new Student(); 44 44 student.setName(name); 45 45 student.setnumber(number); 46 46 student.setsex(sex); 47 47 int a = Integer.parseInt(age); 48 48 student.setage(a); 49 49 student.setprovince(province); 50 50 studentlist.add(student); 51 51 52 52 } 53 53 } catch (FileNotFoundException e) { 54 54 System.out.println("学生信息文件找不到"); 55 55 e.printStackTrace(); 56 56 } catch (IOException e) { 57 57 System.out.println("学生信息文件读取错误"); 58 58 e.printStackTrace(); 59 59 } 60 60 panel = new JPanel(); 61 61 panel.setLayout(new BorderLayout()); 62 62 JTextArea jt = new JTextArea(); 63 63 panel.add(jt); 64 64 add(panel, BorderLayout.NORTH); 65 65 66 66 buttonPanel = new JPanel(); 67 67 buttonPanel.setLayout(new GridLayout(1, 7)); 68 68 JButton jButton = new JButton("字典排序"); 69 69 JButton jButton1 = new JButton("年龄最大和年龄最小"); 70 70 JLabel lab = new JLabel("猜猜你的老乡"); 71 71 JTextField jt1 = new JTextField(); 72 72 JLabel lab1 = new JLabel("找找同龄人(年龄相近):"); 73 73 JTextField jt2 = new JTextField(); 74 74 JLabel lab2 = new JLabel("输入你的身份证号码:"); 75 75 JTextField jt3 = new JTextField(); 76 76 JButton jButton2 = new JButton("退出"); 77 77 78 78 jButton.addActionListener(new ActionListener() { 79 79 public void actionPerformed(ActionEvent e) { 80 80 Collections.sort(studentlist); 81 81 jt.setText(studentlist.toString()); 82 82 } 83 83 }); 84 84 jButton1.addActionListener(new ActionListener() { 85 85 public void actionPerformed(ActionEvent e) { 86 86 int max = 0, min = 100; 87 87 int j, k1 = 0, k2 = 0; 88 88 for (int i = 1; i < studentlist.size(); i++) { 89 89 j = studentlist.get(i).getage(); 90 90 if (j > max) { 91 91 max = j; 92 92 k1 = i; 93 93 } 94 94 if (j < min) { 95 95 min = j; 96 96 k2 = i; 97 97 } 98 98 99 99 } 100100 jt.setText("年龄最大:" + studentlist.get(k1) + "年龄最小:" + studentlist.get(k2)); 101101 } 102102 }); 103103 jButton2.addActionListener(new ActionListener() { 104104 public void actionPerformed(ActionEvent e) { 105105 dispose(); 106106 System.exit(0); 107107 } 108108 }); 109109 jt1.addActionListener(new ActionListener() { 110110 public void actionPerformed(ActionEvent e) { 111111 String find = jt1.getText(); 112112 String text = ""; 113113 String place = find.substring(0, 3); 114114 for (int i = 0; i < studentlist.size(); i++) { 115115 if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) { 116116 text += "\n" + studentlist.get(i); 117117 jt.setText("老乡:" + text); 118118 } 119119 } 120120 } 121121 }); 122122 jt2.addActionListener(new ActionListener() { 123123 public void actionPerformed(ActionEvent e) { 124124 String yourage = jt2.getText(); 125125 int a = Integer.parseInt(yourage); 126126 int near = agenear(a); 127127 int value = a - studentlist.get(near).getage(); 128128 jt.setText("年龄相近:" + studentlist.get(near)); 129129 } 130130 }); 131131 132132 jt3.addKeyListener(new KeyAdapter() { 133133 public void keyTyped(KeyEvent e) { 134134 list = new ArrayList<>(); 135135 Collections.sort(studentlist); 136136 String key = jt3.getText(); 137137 138138 for (int i = 1; i < studentlist.size(); i++) { 139139 if (studentlist.get(i).getnumber().contains(key)) { 140140 list.add(studentlist.get(i)); 141141 jt.setText("emmm!你可能是:\n" + list); 142142 } 143143 } 144144 } 145145 146146 }); 147147 buttonPanel.add(jButton); 148148 buttonPanel.add(jButton1); 149149 buttonPanel.add(lab); 150150 buttonPanel.add(jt1); 151151 buttonPanel.add(lab1); 152152 buttonPanel.add(jt2); 153153 buttonPanel.add(lab2); 154154 buttonPanel.add(jt3); 155155 buttonPanel.add(jButton2); 156156 add(buttonPanel, BorderLayout.SOUTH); 157157 setSize(DEFAULT_WITH, DEFAULT_HEIGHT); 158158 } 159159 160160 public static int agenear(int age) { 161161 int min = 53, value = 0, k = 0; 162162 for (int i = 0; i < studentlist.size(); i++) { 163163 value = studentlist.get(i).getage() - age; 164164 if (value < 0) 165165 value = -value; 166166 if (value < min) { 167167 min = value; 168168 k = i; 169169 } 170170 } 171171 return k; 172172 } 173173 174174 }
View Code

1 1 package Demo; 2 2 3 3 public class Student implements Comparable<Student> { 4 4 5 5 private String name; 6 6 private String number ; 7 7 private String sex ; 8 8 private int age; 9 9 private String province; 1010 1111 public String getName() { 1212 return name; 1313 } 1414 public void setName(String name) { 1515 this.name = name; 1616 } 1717 public String getnumber() { 1818 return number; 1919 } 2020 public void setnumber(String number) { 2121 this.number = number; 2222 } 2323 public String getsex() { 2424 return sex ; 2525 } 2626 public void setsex(String sex ) { 2727 this.sex =sex ; 2828 } 2929 public int getage() { 3030 3131 return age; 3232 } 3333 public void setage(int age) { 3434 // int a = Integer.parseInt(age); 3535 this.age= age; 3636 } 3737 3838 public String getprovince() { 3939 return province; 4040 } 4141 public void setprovince(String province) { 4242 this.province=province ; 4343 } 4444 4545 public int compareTo(Student o) { 4646 return this.name.compareTo(o.getName()); 4747 } 4848 4949 public String toString() { 5050 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; 5151 } 5252 }
View Code
练习3:采用GUI界面设计以下程序
l 编写一个计算器类,可以完成加、减、乘、除的操作
l 利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;
l 将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt。

1 1 package ui_test; 2 2 3 3 public class Main { 4 4 5 5 public static void main(String[] args) { 6 6 MyExGUI lg = new MyExGUI(); 7 7 //new MyExGUI(); 8 8 9 9 } 1010 1111 }
View Code

1 1 import java.awt.*; 2 2 import java.awt.event.ActionEvent; 3 3 import java.awt.event.ActionListener; 4 4 import java.io.BufferedOutputStream; 5 5 import java.io.File; 6 6 import java.io.FileOutputStream; 7 7 import java.io.IOException; 8 8 import java.util.ArrayList; 9 9 10 10 import javax.swing.*; 11 11 12 12 public class MyExGUI extends JFrame { 13 13 ArrayList<String> user_zongti = new ArrayList<String>(); 14 14 ArrayList<String> user_zonganswer = new ArrayList<String>(); 15 15 ArrayList<String> user_answer = new ArrayList<String>(); 16 16 ArrayList<String> true_answer = new ArrayList<String>(); 17 17 ArrayList<String> jta_timu = new ArrayList<String>(); 18 18 ArrayList<String> jta_zong = new ArrayList<String>(); 19 19 ArrayList<Integer> user_fenshu = new ArrayList<Integer>(); 20 20 JMenuBar jm; // 菜单条组件 21 21 JMenu menu;// 菜单 22 22 JMenuItem item1, item2;// 菜单项 23 23 JMenu build; // 二级菜单 24 24 JMenuItem file, project; 25 25 TextArea answer_all = new TextArea(); 26 26 TextField jta = new TextField(); 27 27 TextField jta_answer = new TextField(); 28 28 JLabel num_answer = new JLabel(); 29 29 JLabel answer; 30 30 JToolBar jtb;// 工具条 31 31 JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb_next; 32 32 int answer_count; 33 33 int answer_fenshu; 34 34 35 35 public MyExGUI() { 36 36 // 创建菜单 37 37 jm = new JMenuBar(); 38 38 39 39 menu = new JMenu("文件(F)"); 40 40 menu.setMnemonic('f'); // 助记符 41 41 42 42 build = new JMenu("新建"); 43 43 44 44 file = new JMenuItem("文件"); 45 45 project = new JMenuItem("答题"); 46 46 item1 = new JMenuItem("保存(S)"); 47 47 item2 = new JMenuItem("退出"); 48 48 49 49 answer = new JLabel("第 1 题"); 50 50 51 51 // 添加菜单项至菜单上 52 52 build.add(file); 53 53 build.add(project); 54 54 55 55 menu.add(build); 56 56 menu.add(item1); 57 57 menu.add(item2); 58 58 menu.addSeparator(); 59 59 // 将菜单加入至菜单栏 60 60 jm.add(menu); 61 61 62 62 JPanel contentPanel = new JPanel(); 63 63 contentPanel.setLayout(null); 64 64 JLabel daan = new JLabel("答案"); 65 65 JLabel dengyu = new JLabel("="); 66 66 num_answer = answer; 67 67 num_answer.setFont(new Font("宋体", Font.BOLD, 22)); 68 68 jb_next = new JButton("下一题"); 69 69 jta.setFont(new Font("宋体", Font.BOLD, 22)); 70 70 jta_answer.setFont(new Font("宋体", Font.BOLD, 22)); 71 71 jb_next.setFont(new Font("宋体", Font.BOLD, 22)); 72 72 daan.setFont(new Font("宋体", Font.BOLD, 22)); 73 73 dengyu.setFont(new Font("宋体", Font.BOLD, 22)); 74 74 75 75 contentPanel.add(num_answer); 76 76 contentPanel.add(daan); 77 77 contentPanel.add(dengyu); 78 78 contentPanel.add(jta); 79 79 80 80 contentPanel.add(jta_answer); 81 81 contentPanel.add(answer_all); 82 82 contentPanel.add(jb_next); 83 83 84 84 num_answer.setBounds(90, 20, 130, 50); 85 85 daan.setBounds(250, 20, 90, 50); 86 86 jta.setBounds(50, 70, 150, 30); 87 87 dengyu.setBounds(205, 70, 20, 20); 88 88 jta_answer.setBounds(230, 70, 100, 30); 89 89 jb_next.setBounds(350, 70, 110, 30); 90 90 answer_all.setBounds(50, 120, 400, 300); 91 91 92 92 this.setJMenuBar(jm); // 添加菜单栏,不能设定位置,会自动放在最上部 93 93 this.add(contentPanel); 94 94 95 95 this.setTitle("在线答题系统"); 96 96 this.setSize(600, 500); 97 97 this.setVisible(true); 98 98 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 99 99 item1.addActionListener(new ActionListener() { 100100 public void actionPerformed(ActionEvent arg0) { 101101 FileOutputStream outSTr = null; 102102 BufferedOutputStream Buff = null; 103103 boolean flag = true; 104104 File file; 105105 // String test ; 106106 do { 107107 // test = "test"+count; 108108 109109 String inputValue = JOptionPane.showInputDialog("Please input file name"); 110110 file = new File(inputValue + "test.txt"); 111111 if (!file.exists()) { 112112 // 创建文件 113113 try { 114114 115115 flag = file.createNewFile(); 116116 117117 } catch (IOException e) { 118118 e.printStackTrace(); 119119 120120 } 121121 flag = false; 122122 } else { 123123 124124 JOptionPane.showMessageDialog(null, "该文件名已存在,请重新输入", "ERROR", JOptionPane.ERROR_MESSAGE); 125125 flag = true; 126126 } 127127 } while (flag); 128128 // 写入文件 129129 String u_answer; 130130 try { 131131 outSTr = new FileOutputStream(file); 132132 Buff = new BufferedOutputStream(outSTr); 133133 System.out.println("选择是后执行的代码" + user_zongti.size() + user_answer.size()); 134134 for (int i = 0; i < user_zongti.size(); i++) { 135135 try { 136136 Buff.write(user_zongti.get(i).getBytes()); 137137 Buff.write(" ".getBytes()); 138138 u_answer = user_answer.get(i); 139139 if (u_answer.equals("")) 140140 u_answer = "没有作答"; 141141 142142 Buff.write(u_answer.getBytes()); 143143 Buff.write("\r\n".getBytes()); 144144 } catch (IOException e) { 145145 e.printStackTrace(); 146146 i--; 147147 } 148148 } 149149 Buff.flush(); 150150 Buff.close(); 151151 152152 } catch (IOException e) { 153153 e.printStackTrace(); 154154 } 155155 try { 156156 outSTr.close(); 157157 } catch (IOException e) { 158158 e.printStackTrace(); 159159 } 160160 user_zongti.clear(); 161161 user_answer.clear(); 162162 } 163163 }); 164164 165165 project.addActionListener(new ActionListener() { 166166 public void actionPerformed(ActionEvent arg0) { 167167 arithmetic art = new arithmetic(); 168168 true_answer = art.list_answer; 169169 jta_timu = art.list_timu; 170170 jta_zong = art.list; 171171 answer_count = 1; 172172 answer_all.setText(""); 173173 for (int i = 0; i < art.list_timu.size(); i++) { 174174 user_zongti.add(jta_zong.get(i)); 175175 answer_all.append(jta_timu.get(i)); 176176 answer_all.append("\r\n"); 177177 } 178178 num_answer.setText("第 " + answer_count + " 题"); 179179 jta.setText(jta_timu.get(answer_count - 1)); 180180 answer_count++; 181181 182182 } 183183 }); 184184 jb_next.addActionListener(new ActionListener() { 185185 public void actionPerformed(ActionEvent arg0) { 186186 String temp; 187187 temp = jta_answer.getText(); 188188 189189 if (jta.getText().equals("")) { 190190 JOptionPane.showMessageDialog(null, "错误,请导入题库", "错误", JOptionPane.ERROR_MESSAGE); 191191 return; 192192 } 193193 jta_answer.setText(""); 194194 if (answer_count <= 10) { 195195 if (isInteger(temp)) { 196196 197197 user_answer.add(temp); 198198 System.out.println("选择否后执行的代码" + temp + "user_size" + user_answer.size()); 199199 num_answer.setText("第 " + answer_count + " 题"); 200200 jta.setText(jta_timu.get(answer_count - 1)); 201201 answer_count++; 202202 } else { 203203 JOptionPane.showMessageDialog(null, "错误", "请输入数字", JOptionPane.ERROR_MESSAGE); 204204 } 205205 } else { 206206 user_answer.add(temp); 207207 System.out.println("选择否后执行的代码" + temp + "user_size" + user_answer.size()); 208208 answer_fenshu = 0; 209209 for (int i = 0; i < user_answer.size(); i++) { 210210 if (user_answer.get(i).equals(true_answer.get(i))) 211211 answer_fenshu += 5; 212212 } 213213 user_fenshu.add(answer_fenshu); 214214 Object[] options = { "是", "取消" }; 215215 int res = JOptionPane.showOptionDialog(null, "是否查看成绩", "答题完毕", JOptionPane.DEFAULT_OPTION, 216216 JOptionPane.YES_NO_OPTION, null, options, options[0]); 217217 if (res == JOptionPane.YES_OPTION) { 218218 chart ct = new chart(user_fenshu); 219219 ct.setVisible(true); 220220 221221 } else { 222222 Object[] option = { "是", "取消" }; 223223 int res1 = JOptionPane.showOptionDialog(null, "是否退出程序", "终止框", JOptionPane.DEFAULT_OPTION, 224224 JOptionPane.YES_NO_OPTION, null, option, option[0]); 225225 226226 if (res1 == JOptionPane.YES_OPTION) { 227227 dispose(); 228228 System.exit(0); 229229 230230 } else { 231231 232232 } 233233 234234 } 235235 236236 } 237237 238238 } 239239 }); 240240 241241 item2.addActionListener(new ActionListener() { 242242 public void actionPerformed(ActionEvent e) { 243243 dispose(); 244244 System.exit(0); 245245 } 246246 }); 247247 248248 } 249249 250250 public static boolean isInteger(String str) { 251251 for (int i = str.length(); --i >= 0;) { 252252 if (!Character.isDigit(str.charAt(i))) { 253253 return false; 254254 } 255255 } 256256 return true; 257257 } 258258 259259 }
View Code

1 1 import java.io.BufferedOutputStream; 2 2 import java.io.File; 3 3 import java.io.FileOutputStream; 4 4 import java.io.IOException; 5 5 import java.util.ArrayList; 6 6 import java.util.Random; 7 7 import java.util.Scanner; 8 8 9 9 public class arithmetic { 10 10 ArrayList<String> list = new ArrayList<String>(); 11 11 ArrayList<String> list_timu = new ArrayList<String>(); 12 12 ArrayList<String> list_answer = new ArrayList<String>(); 13 13 14 14 public arithmetic() { 15 15 FileOutputStream outSTr = null; 16 16 BufferedOutputStream Buff = null; 17 17 int number_n = 10, count; 18 18 19 19 ArrayList<String> list_temp = new ArrayList<String>(); 20 20 String[] operator = new String[] { "+", "-", "*", "/" }; 21 21 22 22 Random rand = new Random(); 23 23 File file1 = new File("test.txt"); 24 24 if (file1.exists()) { 25 25 // 创建文件 26 26 try { 27 27 file1.createNewFile(); 28 28 } catch (IOException e) { 29 29 e.printStackTrace(); 30 30 } 31 31 } 32 32 33 33 while (number_n > 0) { 34 34 int[] number_temp = new int[rand.nextInt(2) + 3]; 35 35 String[] str_temp = new String[number_temp.length - 1]; 36 36 for (int i = 0; i < number_temp.length; i++) { 37 37 if (i < number_temp.length - 1) { 38 38 number_temp[i] = rand.nextInt(100); 39 39 list_temp.add(String.valueOf(number_temp[i])); 40 40 str_temp[i] = operator[rand.nextInt(4)]; 41 41 list_temp.add(str_temp[i]); 42 42 43 43 } 44 44 45 45 else { 46 46 number_temp[i] = rand.nextInt(100); 47 47 list_temp.add(String.valueOf(number_temp[i])); 48 48 } 49 49 } 50 50 51 51 count = calculate_RPN(produce_RPN(list_temp)); 52 52 if (count != -1) { 53 53 list_timu.add(transform_string(list_temp)); 54 54 list_answer.add(String.valueOf(count)); 55 55 list_temp.add(" = " + count); 56 56 list.add(transform_string(list_temp)); 57 57 number_n--; 58 58 list_temp.clear(); 59 59 } else 60 60 list_temp.clear(); 61 61 System.out.println(number_n); 62 62 63 63 } 64 64 try { 65 65 outSTr = new FileOutputStream(file1); 66 66 Buff = new BufferedOutputStream(outSTr); 67 67 for (int i = 0; i < list.size(); i++) { 68 68 try { 69 69 Buff.write(list.get(i).getBytes()); 70 70 Buff.write("\r\n".getBytes()); 71 71 } catch (IOException e) { 72 72 e.printStackTrace(); 73 73 i--; 74 74 } 75 75 } 76 76 Buff.flush(); 77 77 Buff.close(); 78 78 79 79 } catch (IOException e) { 80 80 e.printStackTrace(); 81 81 } 82 82 // Buff.close(); 83 83 try { 84 84 outSTr.close(); 85 85 } catch (IOException e) { 86 86 e.printStackTrace(); 87 87 } 88 88 89 89 for (int i = 0; i < list.size(); i++) { 90 90 System.out.print(list.get(i)); 91 91 System.out.println(); 92 92 } 93 93 System.out.print("计算完毕!"); 94 94 95 95 } 96 96 97 97 public static int calculate_RPN(ArrayList<String> list_temp) { 98 98 int i = 0, t; 99 99 double a = 0, b = 0; 100100 String l_temp; 101101 Stack sk = new Stack(10); 102102 for (t = 0; t < list_temp.size(); t++) { 103103 l_temp = list_temp.get(i++); 104104 if (!isInteger(l_temp)) { 105105 b = sk.mypop(); 106106 a = sk.mypop(); 107107 switch (l_temp) { 108108 case "+": 109109 sk.mypush(a + b); 110110 break; 111111 case "-": 112112 if(!(a<b)) { 113113 sk.mypush(a - b); 114114 } 115115 else 116116 return -1; 117117 break; 118118 case "*": 119119 sk.mypush(a * b); 120120 break; 121121 case "/": 122122 if (b == 0||a<b) 123123 return -1; 124124 sk.mypush(a / b); 125125 break; 126126 } 127127 System.out.println("st.mytop: " + sk.mypeek()); 128128 } else { 129129 sk.mypush((double) Integer.parseInt(l_temp)); 130130 } 131131 132132 } 133133 if (!sk.myisempty()) { 134134 a = sk.mypop(); 135135 b = a - (int) a; 136136 System.out.println("a: " + a); 137137 if (a > 0 && b == 0) { 138138 return (int) a; 139139 } else 140140 return -1; 141141 } else 142142 return -1; 143143 144144 } 145145 146146 public static ArrayList<String> produce_RPN(ArrayList<String> list_temp) { 147147 int t = 0, i = 0; 148148 String tmp; 149149 Tack mytack = new Tack(10); 150150 ArrayList<String> lt_temp = new ArrayList<String>(); 151151 while (true) { 152152 tmp = list_temp.get(i++); 153153 if (isInteger(tmp)) { 154154 lt_temp.add(tmp); 155155 } else { 156156 if (mytack.myisempty()) { 157157 mytack.mypush(tmp); 158158 } 159159 160160 else { 161161 if (isCPriority(tmp, mytack.mypeek())) 162162 mytack.mypush(tmp); 163163 else { 164164 lt_temp.add(mytack.mypop()); 165165 mytack.mypush(tmp); 166166 } 167167 168168 } 169169 } 170170 if (i >= list_temp.size()) { 171171 while (!mytack.myisempty()) 172172 lt_temp.add(mytack.mypop()); 173173 System.out.println(transform_string(list_temp)); 174174 list_temp = lt_temp; 175175 System.out.println(list_temp); 176176 return list_temp; 177177 } 178178 } 179179 180180 } 181181 182182 public static boolean isInteger(String str) { 183183 for (int i = str.length(); --i >= 0;) { 184184 if (!Character.isDigit(str.charAt(i))) { 185185 return false; 186186 } 187187 } 188188 return true; 189189 } 190190 191191 public static boolean isCPriority(String str, String s) { 192192 if ((str + s).equals("*+") || (str + s).equals("*-") || (str + s).equals("/+") || (str + s).equals("/-")) 193193 return true; 194194 else 195195 return false; 196196 } 197197 198198 public static String transform_string(ArrayList<String> list_temp) { 199199 String s = ""; 200200 for (int i = 0; i < list_temp.size(); i++) { 201201 s += list_temp.get(i); 202202 } 203203 return s; 204204 205205 } 206206 207207 static class Stack { 208208 int mytop; 209209 double stk[]; 210210 211211 public Stack(int num) { 212212 mytop = -1; 213213 stk = new double[num]; 214214 } 215215 216216 /* 出栈 */ 217217 double mypop() { 218218 double peek = stk[mytop]; 219219 mytop--; 220220 return peek; 221221 } 222222 223223 /* 入栈 */ 224224 void mypush(double x) { 225225 mytop++; 226226 stk[mytop] = x; 227227 228228 } 229229 230230 /* 判空 */ 231231 Boolean myisempty() { 232232 if (mytop == -1) 233233 return true; 234234 else 235235 return false; 236236 } 237237 238238 /* 取栈顶元素 */ 239239 double mypeek() { 240240 double peek = stk[mytop]; 241241 return peek; 242242 } 243243 244244 /* 栈大小 */ 245245 int mysize() { 246246 return mytop + 1; 247247 } 248248 } 249249 250250 static class Tack { 251251 int mytop; 252252 String tk[]; 253253 254254 public Tack(int num) { 255255 mytop = -1; 256256 tk = new String[num]; 257257 } 258258 259259 /* 出栈 */ 260260 String mypop() { 261261 String peek = tk[mytop]; 262262 mytop--; 263263 return peek; 264264 } 265265 266266 /* 入栈 */ 267267 void mypush(String x) { 268268 mytop++; 269269 tk[mytop] = x; 270270 271271 } 272272 273273 /* 判空 */ 274274 Boolean myisempty() { 275275 if (mytop == -1) 276276 return true; 277277 else 278278 return false; 279279 } 280280 281281 /* 取栈顶元素 */ 282282 String mypeek() { 283283 String peek = tk[mytop]; 284284 return peek; 285285 } 286286 287287 /* 栈大小 */ 288288 int mysize() { 289289 return mytop + 1; 290290 } 291291 292292 } 293293 294294 }
View Code
任务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.理论课:老师讲授基础知识
2.实验课:老师和助教演示示例程序,并进行相应的更改和创新
3.课下自主学习:
(1)结合书籍内容和相关的示例程序,并自主查阅资料,完成编程练习。
(2)编程练习完成后,助教进行网络在线示范与讲解;老师根据出现的问题,在课堂上进行讲解。
学习心得:
经过一学期的学习,对面向对象编程有了一个清晰的概念,理解了Java历久弥新的强大生命力。但在语法上还有一些东西没有掌握,都需要尽快复习,以后也不能放下这门技能。还需继续努力学习Java,提高自己的编程水平。