《面向对象程序设计(java)》第十八周学习总结
第一部分:理论知识学习部分
第二部分:实验部分
实验名称:实验十八 总复习
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 2
2 import java.awt.Dimension;
3 import java.awt.FlowLayout;
4 import java.awt.GridLayout;
5
6 import javax.swing.BorderFactory;
7 import javax.swing.ButtonGroup;
8 import javax.swing.JButton;
9 import javax.swing.JCheckBox;
10 import javax.swing.JComboBox;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JRadioButton;
15 import javax.swing.JTextField;
16
17 public class DemoJFrame extends JFrame {
18 private JPanel jPanel1;
19 private JPanel jPanel2;
20 private JPanel jPanel3;
21 private JPanel jPanel4;
22 private JTextField fieldname;
23 private JComboBox comboBox;
24 private JTextField fieldadress;
25 private ButtonGroup bg;
26 private JRadioButton Male;
27 private JRadioButton Female;
28 private JCheckBox read;
29 private JCheckBox sing;
30 private JCheckBox dance;
31
32 public DemoJFrame() {
33 // 设置窗口大小
34 this.setSize(800, 400);
35 // 设置可见性
36 this.setVisible(true);
37 // 设置标题
38 this.setTitle("编程练习一");
39 // 设置关闭操作
40 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
41 // 设置窗口居中
42 WinCenter.center(this);
43 // 创建四个面板对象
44 jPanel1 = new JPanel();
45 setJPanel1(jPanel1);
46 jPanel2 = new JPanel();
47 setJPanel2(jPanel2);
48 jPanel3 = new JPanel();
49 setJPanel3(jPanel3);
50 jPanel4 = new JPanel();
51 setJPanel4(jPanel4);
52 // 设置容器的为流布局
53 FlowLayout flowLayout = new FlowLayout();
54 this.setLayout(flowLayout);
55 // 将四个面板添加到容器中
56 this.add(jPanel1);
57 this.add(jPanel2);
58 this.add(jPanel3);
59 this.add(jPanel4);
60
61 }
62
63 /*
64 * 设置面一
65 */
66 private void setJPanel1(JPanel jPanel) {
67 // TODO 自动生成的方法存根
68 jPanel.setPreferredSize(new Dimension(700, 45));
69 // 给面板的布局设置为网格布局 一行4列
70 jPanel.setLayout(new GridLayout(1, 4));
71
72 JLabel name = new JLabel("name:");
73 name.setSize(100, 50);
74 fieldname = new JTextField("");
75 fieldname.setSize(80, 20);
76
77 JLabel study = new JLabel("qualification:");
78 comboBox = new JComboBox();
79 comboBox.addItem("初中");
80 comboBox.addItem("高中");
81 comboBox.addItem("本科");
82 jPanel.add(name);
83 jPanel.add(fieldname);
84 jPanel.add(study);
85 jPanel.add(comboBox);
86
87 }
88
89 /*
90 * 设置面板二
91 */
92 private void setJPanel2(JPanel jPanel) {
93 // TODO 自动生成的方法存根
94 jPanel.setPreferredSize(new Dimension(700, 50));
95 // 给面板的布局设置为网格布局 一行4列
96 jPanel.setLayout(new GridLayout(1, 4));
97
98 JLabel name = new JLabel("address:");
99 fieldadress = new JTextField();
100 fieldadress.setPreferredSize(new Dimension(150, 50));
101
102 JLabel study = new JLabel("hobby:");
103 JPanel selectBox = new JPanel();
104 selectBox.setBorder(BorderFactory.createTitledBorder(""));
105 selectBox.setLayout(new GridLayout(3, 1));
106 read = new JCheckBox("reading");
107 sing = new JCheckBox("singing");
108 dance = new JCheckBox("danceing");
109 selectBox.add(read);
110 selectBox.add(sing);
111 selectBox.add(dance);
112 jPanel.add(name);
113 jPanel.add(fieldadress);
114 jPanel.add(study);
115 jPanel.add(selectBox);
116 }
117
118 /*
119 * 设置面板三
120 */
121 private void setJPanel3(JPanel jPanel) {
122 // TODO 自动生成的方法存根
123 jPanel.setPreferredSize(new Dimension(700, 150));
124 FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
125 jPanel.setLayout(flowLayout);
126 JLabel sex = new JLabel("性别:");
127 JPanel selectBox = new JPanel();
128 selectBox.setBorder(BorderFactory.createTitledBorder(""));
129 selectBox.setLayout(new GridLayout(2, 1));
130 bg = new ButtonGroup();
131 Male = new JRadioButton("male");
132 Female = new JRadioButton("female");
133 bg.add(Male );
134 bg.add(Female);
135 selectBox.add(Male);
136 selectBox.add(Female);
137 jPanel.add(sex);
138 jPanel.add(selectBox);
139
140 }
141
142 /*
143 * 设置面板四
144 */
145 private void setJPanel4(JPanel jPanel) {
146 // TODO 自动生成的方法存根
147 jPanel.setPreferredSize(new Dimension(700, 150));
148 FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 50, 10);
149 jPanel.setLayout(flowLayout);
150 jPanel.setLayout(flowLayout);
151 JButton sublite = new JButton("提交");
152 JButton reset = new JButton("重置");
153 sublite.addActionListener((e) -> valiData());
154 reset.addActionListener((e) -> Reset());
155 jPanel.add(sublite);
156 jPanel.add(reset);
157 }
158
159 /*
160 * 提交数据
161 */
162 private void valiData() {
163 // TODO 自动生成的方法存根
164 // 拿到数据
165 String name = fieldname.getText().toString().trim();
166 String xueli = comboBox.getSelectedItem().toString().trim();
167 String address = fieldadress.getText().toString().trim();
168 System.out.println(name);
169 System.out.println(xueli);
170 String hobbystring="";
171 if (read.isSelected()) {
172 hobbystring+="reading ";
173 }
174 if (sing.isSelected()) {
175 hobbystring+="singing ";
176 }
177 if (dance.isSelected()) {
178 hobbystring+="dancing ";
179 }
180 System.out.println(address);
181 if (Male.isSelected()) {
182 System.out.println("male");
183 }
184 if (Female.isSelected()) {
185 System.out.println("female");
186 }
187 System.out.println(hobbystring);
188 }
189
190 /*
191 * 重置
192 */
193 private void Reset() {
194 // TODO 自动生成的方法存根
195 fieldadress.setText(null);
196 fieldname.setText(null);
197 comboBox.setSelectedIndex(0);
198 read.setSelected(false);
199 sing.setSelected(false);
200 dance.setSelected(false);
201 bg.clearSelection();
202 }
203 }
204
205 DemoJFrame
1 2
2
3
4 import java.awt.EventQueue;
5
6 import javax.swing.JFrame;
7
8 public class Main {
9 public static void main(String[] args) {
10 EventQueue.invokeLater(() -> {
11 DemoJFrame page = new DemoJFrame();
12 });
13 }
14 }
15
16 Main
1 2
2 import java.awt.Dimension;
3 import java.awt.Toolkit;
4 import java.awt.Window;
5
6 public class WinCenter {
7 public static void center(Window win){
8 Toolkit tkit = Toolkit.getDefaultToolkit();
9 Dimension sSize = tkit.getScreenSize();
10 Dimension wSize = win.getSize();
11 if(wSize.height > sSize.height){
12 wSize.height = sSize.height;
13 }
14 if(wSize.width > sSize.width){
15 wSize.width = sSize.width;
16 }
17 win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
18 }
19 }
20
21 WinCenter
运行结果如下:
练习2:采用GUI界面设计以下程序:
l 编制一个程序,将身份证号.txt 中的信息读入到内存中;
l 按姓名字典序输出人员信息;
l 查询最大年龄的人员信息;
l 查询最小年龄人员信息;
l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;
l 查询人员中是否有你的同乡。
l 输入身份证信息,查询所提供身份证号的人员信息,要求输入一个身份证数字时,查询界面就显示满足查询条件的查询结果,且随着输入的数字的增多,查询匹配的范围逐渐缩小。
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.InputStreamReader;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.Scanner;
11 import java.awt.*;
12 import javax.swing.*;
13 import java.awt.event.*;
14
15 public class Main extends JFrame {
16 private static ArrayList<Student> studentlist;
17 private static ArrayList<Student> list;
18 private JPanel panel;
19 private JPanel buttonPanel;
20 private static final int DEFAULT_WITH = 800;
21 private static final int DEFAULT_HEIGHT = 600;
22
23 public Main() {
24 studentlist = new ArrayList<>();
25 Scanner scanner = new Scanner(System.in);
26 File file = new File("E:\\java\\身份证号.txt");
27 try {
28 FileInputStream fis = new FileInputStream(file);
29 BufferedReader in = new BufferedReader(new InputStreamReader(fis));
30 String temp = null;
31 while ((temp = in.readLine()) != null) {
32
33 Scanner linescanner = new Scanner(temp);
34
35 linescanner.useDelimiter(" ");
36 String name = linescanner.next();
37 String number = linescanner.next();
38 String sex = linescanner.next();
39 String age = linescanner.next();
40 String province = linescanner.nextLine();
41 Student student = new Student();
42 student.setName(name);
43 student.setnumber(number);
44 student.setsex(sex);
45 int a = Integer.parseInt(age);
46 student.setage(a);
47 student.setprovince(province);
48 studentlist.add(student);
49
50 }
51 } catch (FileNotFoundException e) {
52 System.out.println("学生信息文件找不到");
53 e.printStackTrace();
54 } catch (IOException e) {
55 System.out.println("学生信息文件读取错误");
56 e.printStackTrace();
57 }
58 panel = new JPanel();
59 panel.setLayout(new BorderLayout());
60 JTextArea jt = new JTextArea();
61 panel.add(jt);
62 add(panel, BorderLayout.NORTH);
63 buttonPanel = new JPanel();
64 buttonPanel.setLayout(new GridLayout(1, 7));
65 JButton jButton = new JButton("字典排序");
66 JButton jButton1 = new JButton("年龄最大和年龄最小");
67 JLabel lab = new JLabel("猜猜你的老乡");
68 JTextField jt1 = new JTextField();
69 JLabel lab1 = new JLabel("找找同龄人(年龄相近):");
70 JTextField jt2 = new JTextField();
71 JLabel lab2 = new JLabel("输入你的身份证号码:");
72 JTextField jt3 = new JTextField();
73 JButton jButton2 = new JButton("退出");
74 jButton.setBounds(110, 90, 60, 30);
75 jButton1.setBounds(110, 90, 60, 30);
76 jt1.setBounds(110, 90, 60, 30);
77 jt2.setBounds(110, 90, 60, 30);
78 jt3.setBounds(110, 90, 60, 30);
79 jButton2.setBounds(110, 90, 60, 30);
80 jButton.addActionListener(new ActionListener() {
81 public void actionPerformed(ActionEvent e) {
82 Collections.sort(studentlist);
83 jt.setText(studentlist.toString());
84 }
85 });
86 jButton1.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent e) {
88 int max = 0, min = 100;
89 int j, k1 = 0, k2 = 0;
90 for (int i = 1; i < studentlist.size(); i++) {
91 j = studentlist.get(i).getage();
92 if (j > max) {
93 max = j;
94 k1 = i;
95 }
96 if (j < min) {
97 min = j;
98 k2 = i;
99 }
100
101 }
102 jt.setText("年龄最大:" + studentlist.get(k1) + "年龄最小:" + studentlist.get(k2));
103 }
104 });
105 jButton2.addActionListener(new ActionListener() {
106 public void actionPerformed(ActionEvent e) {
107 dispose();
108 System.exit(0);
109 }
110 });
111 jt1.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
113 String find = jt1.getText();
114 String text="";
115 String place = find.substring(0, 3);
116 for (int i = 0; i < studentlist.size(); i++) {
117 if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) {
118 text+="\n"+studentlist.get(i);
119 jt.setText("老乡:" + text);
120 }
121 }
122 }
123 });
124 jt2.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
126 String yourage = jt2.getText();
127 int a = Integer.parseInt(yourage);
128 int near = agenear(a);
129 int value = a - studentlist.get(near).getage();
130 jt.setText("年龄相近:" + studentlist.get(near));
131 }
132 });
133 jt3.addActionListener(new ActionListener() {
134 public void actionPerformed(ActionEvent e) {
135 list = new ArrayList<>();
136 Collections.sort(studentlist);
137 String key = jt3.getText();
138 for (int i = 1; i < studentlist.size(); i++) {
139 if (studentlist.get(i).getnumber().contains(key)) {
140 list.add(studentlist.get(i));
141 jt.setText("emmm!你可能是:\n" + list);
142
143 }
144 }
145 }
146 });
147 buttonPanel.add(jButton);
148 buttonPanel.add(jButton1);
149 buttonPanel.add(lab);
150 buttonPanel.add(jt1);
151 buttonPanel.add(lab1);
152 buttonPanel.add(jt2);
153 buttonPanel.add(lab2);
154 buttonPanel.add(jt3);
155 buttonPanel.add(jButton2);
156 add(buttonPanel, BorderLayout.SOUTH);
157 setSize(DEFAULT_WITH, DEFAULT_HEIGHT);
158 }
159
160 public static int agenear(int age) {
161 int min = 53, value = 0, k = 0;
162 for (int i = 0; i < studentlist.size(); i++) {
163 value = studentlist.get(i).getage() - age;
164 if (value < 0)
165 value = -value;
166 if (value < min) {
167 min = value;
168 k = i;
169 }
170 }
171 return k;
172 }
173
174 }
1 public class Student implements Comparable<Student> {
2
3 private String name;
4 private String number ;
5 private String sex ;
6 private int age;
7 private String province;
8
9 public String getName() {
10 return name;
11 }
12 public void setName(String name) {
13 this.name = name;
14 }
15 public String getnumber() {
16 return number;
17 }
18 public void setnumber(String number) {
19 this.number = number;
20 }
21 public String getsex() {
22 return sex ;
23 }
24 public void setsex(String sex ) {
25 this.sex =sex ;
26 }
27 public int getage() {
28
29 return age;
30 }
31 public void setage(int age) {
32 // int a = Integer.parseInt(age);
33 this.age= age;
34 }
35
36 public String getprovince() {
37 return province;
38 }
39 public void setprovince(String province) {
40 this.province=province ;
41 }
42
43 public int compareTo(Student o) {
44 return this.name.compareTo(o.getName());
45 }
46
47 public String toString() {
48 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";
49 }
50 }
1 import java.awt.*;
2 import javax.swing.*;
3
4 public class ButtonTest {
5 public static void main(String[] args) {
6 EventQueue.invokeLater(() -> {
7 JFrame frame = new Main();
8 frame.setTitle("身份证信息");
9 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10 frame.setVisible(true);
11 });
12 }
13 }
运行结果如下:
练习3:采用GUI界面设计以下程序
l 编写一个计算器类,可以完成加、减、乘、除的操作
l 利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;
l 将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt。
1 import java.awt.BorderLayout;
2 import java.awt.GridLayout;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import javax.swing.JButton;
6 import javax.swing.JFrame;
7 import javax.swing.JPanel;
8 import javax.swing.JTextField;
9 public class Calculator extends JFrame {
10 JButton b0 = new JButton("0");
11 JButton b1 = new JButton("1");
12 JButton b2 = new JButton("2");
13 JButton b3 = new JButton("3");
14 JButton b4 = new JButton("4");
15 JButton b5 = new JButton("5");
16 JButton b6 = new JButton("6");
17 JButton b7 = new JButton("7");
18 JButton b8 = new JButton("8");
19 JButton b9 = new JButton("9");
20 JButton jiaButton = new JButton("+");
21 JButton jianButton = new JButton("-");
22 JButton chengButton = new JButton("*");
23 JButton chuButton = new JButton("/");
24 JButton yuButton = new JButton("%");
25 JButton jjButton = new JButton("+/-");
26 JButton sqrtButton = new JButton("sqrt");
27 JButton dianButton = new JButton(".");
28 JButton dengButton = new JButton("=");
29 JButton daoButton = new JButton("1/x");
30 JButton backButton = new JButton("Backpace");
31 JButton cButton = new JButton("C");
32 public double op1;
33 public double op2;
34 public static final int JIA = 0;
35 public static final int JIAN = 1;
36 public static final int CHENG = 2;
37 public static final int CHU = 3;
38 public static final int JJ = 4;
39 public static final int DIAN = 5;
40 public int current0p = 0;
41 private boolean opEnd = false;
42 JPanel panel1 = new JPanel();
43 JPanel panel2 = new JPanel();
44 JPanel panel3 = new JPanel();
45 JPanel panel4 = new JPanel();
46 JTextField result = new JTextField(20);
47 public Calculator() {
48 initPanel2();
49 initPanel3();
50 panel2.setLayout(new GridLayout(5, 4));
51 panel1.setLayout(new BorderLayout());
52 panel1.add(panel3, BorderLayout.NORTH);// 设置位置
53 panel1.add(panel2, BorderLayout.CENTER);// 设置位置
54 getContentPane().add(panel1);
55 addActionListeners();
56 setSize(260, 260);
57 setLocation(500, 300);
58 setVisible(true);
59 setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
60 this.setResizable(false);
61 this.setTitle("计算器");
62 }
63 private void initPanel2() {
64 // 把组件添加相应panel上
65 panel2.add(b7);
66 panel2.add(b8);
67 panel2.add(b9);
68 panel2.add(chuButton);
69 panel2.add(b4);
70 panel2.add(b5);
71 panel2.add(b6);
72 panel2.add(chengButton);
73 panel2.add(b1);
74 panel2.add(b2);
75 panel2.add(b3);
76 panel2.add(jianButton);
77 panel2.add(b0);
78 panel2.add(jjButton);
79 panel2.add(dianButton);
80 panel2.add(jiaButton);
81 panel2.add(daoButton);
82 panel2.add(yuButton);
83 panel2.add(sqrtButton);
84 panel2.add(dengButton);
85 }
86 private void addActionListeners() {
87 ActionHandler c = new ActionHandler();
88 b0.addActionListener(c);
89 b1.addActionListener(c);
90 b2.addActionListener(c);
91 b3.addActionListener(c);
92 b4.addActionListener(c);
93 b5.addActionListener(c);
94 b6.addActionListener(c);
95 b7.addActionListener(c);
96 b8.addActionListener(c);
97 b9.addActionListener(c);
98 jiaButton.addActionListener(c);
99 dengButton.addActionListener(c);
100 chengButton.addActionListener(c);
101 chuButton.addActionListener(c);
102 jianButton.addActionListener(c);
103 jjButton.addActionListener(c);
104 dianButton.addActionListener(c);
105 sqrtButton.addActionListener(c);
106 yuButton.addActionListener(c);
107 daoButton.addActionListener(c);
108 backButton.addActionListener(c);
109 cButton.addActionListener(c);
110 }
111 class ActionHandler implements ActionListener {
112 public void actionPerformed(ActionEvent e) {
113 if (e.getSource() == b0) {
114 if (opEnd == false) {
115 result.setText("");
116 }
117 result.setText(result.getText() + "0");
118 }
119 if (e.getSource() == b1) {
120 if (opEnd == false) {
121 result.setText("");
122 }
123 result.setText(result.getText() + "1");
124 opEnd = true;
125 }
126 if (e.getSource() == b2) {
127 if (opEnd == false) {
128 result.setText("");
129 }
130 result.setText(result.getText() + "2");
131 opEnd = true;
132 }
133 if (e.getSource() == b3) {
134 if (opEnd == false) {
135 result.setText("");
136 }
137 result.setText(result.getText() + "3");
138 opEnd = true;
139 }
140 if (e.getSource() == b4) {
141 if (opEnd == false) {
142 result.setText("");
143 }
144 result.setText(result.getText() + "4");
145 opEnd = true;
146 }
147 if (e.getSource() == b5) {
148 if (opEnd == false) {
149 result.setText("");
150 }
151 result.setText(result.getText() + "5");
152 opEnd = true;
153 }
154 if (e.getSource() == b6) {
155 if (opEnd == false) {
156 result.setText("");
157 }
158 result.setText(result.getText() + "6");
159 opEnd = true;
160 }
161 if (e.getSource() == b7) {
162 if (opEnd == false) {
163 result.setText("");
164 }
165 result.setText(result.getText() + "7");
166 opEnd = true;
167 }
168 if (e.getSource() == b8) {
169 if (opEnd == false) {
170 result.setText("");
171 }
172 result.setText(result.getText() + "8");
173 opEnd = true;
174 }
175 if (e.getSource() == b9) {
176 if (opEnd == false) {
177 result.setText("");
178 }
179 result.setText(result.getText() + "9");
180 opEnd = true;
181 }
182 try {
183 if (e.getSource() == jiaButton) {
184 op1 = Double.parseDouble(result.getText());
185 // 2、说明操作数已经输入完毕
186 opEnd = false;
187 current0p = JIA;
188 }
189 if (e.getSource() == chengButton) {
190 op1 = Double.parseDouble(result.getText());
191 // 2、说明操作数已经输入完毕
192 opEnd = false;
193 current0p = CHENG;
194 }
195 if (e.getSource() == chuButton) {
196 op1 = Double.parseDouble(result.getText());
197 // 2、说明操作数已经输入完毕
198 opEnd = false;
199 current0p = CHU;
200 }
201 if (e.getSource() == jianButton) {
202 op1 = Double.parseDouble(result.getText());
203 // 2、说明操作数已经输入完毕
204 opEnd = false;
205 current0p = JIAN;
206 }
207 if (e.getSource() == jjButton) {
208 String tmp = result.getText();
209 if (tmp.equals("") || tmp.equals("0")) {
210 return;
211 }
212 if (tmp.charAt(0) == '-') {
213 tmp = tmp.substring(1);
214 } else {
215 tmp = '-' + tmp;
216 }
217 result.setText(tmp);
218 }
219 if (e.getSource() == dianButton) {
220 String tmp = result.getText();
221 if (tmp.equals("")) {
222 return;
223 }
224 if (tmp.indexOf(".") != -1) {
225 return;
226 }
227 tmp = tmp + ".";
228 result.setText(tmp);
229 }
230 if (e.getSource() == sqrtButton) {
231 String tmp = result.getText();
232 if (tmp.equals(" ")) {
233 return;
234 }
235 double d;
236 d = Double.parseDouble(tmp);// 先定义double类型d
237 if (d < 0) {
238 result.setText("不能对负数求平方根");
239 return;
240 }
241 op2 = Math.sqrt(d);
242 result.setText(op2 + "");
243 }
244 if (e.getSource() == backButton) {
245 String s = result.getText();
246 result.setText("");
247 for (int i = 0; i < s.length() - 1; i++) {
248 char a = s.charAt(i);
249 result.setText(result.getText() + a);
250 }
251 }
252 if (e.getSource() == cButton) {
253 result.setText("0");
254 opEnd = false;
255 }
256 if (e.getSource() == dengButton) {
257 op2 = Double.parseDouble(result.getText());
258 switch (current0p) {
259 case JIA:
260 result.setText(op1 + op2 + "");
261 break;
262 case JIAN:
263 result.setText(op1 - op2 + "");
264 break;
265 case CHENG:
266 result.setText(op1 * op2 + "");
267 break;
268 case CHU:
269 if (op2 == 0) {
270 result.setText("被除数不能为零");
271 break;
272 }
273 result.setText(op1 / op2 + "");
274 break;
275 }
276 opEnd = false;
277 }
278 } catch (Exception e1) {
279 result.setText("Wrong");
280 opEnd = false;
281 }
282 }
283 }
284 private void initPanel3() {
285 panel3.setLayout(new GridLayout(2, 1));
286 panel3.add(result);
287 panel3.add(panel4);
288 panel4.setLayout(new GridLayout(1, 2));
289 panel4.add(backButton);
290 panel4.add(cButton);
291 }
292 public static void main(String[] args) {
293 Calculator c = new Calculator();// 生成类实例
294 }
295 }
1 import java.io.FileNotFoundException;
2 import java.io.PrintWriter;
3 import java.util.Scanner;
4
5 /*
6 * 该程序用来随机生成0到100以内的加减乘除题
7 */
8 public class Demo {
9 public static void main(String[] args) {
10 // 用户的答案要从键盘输入,因此需要一个键盘输入流
11 Scanner in = new Scanner(System.in);
12 Counter counter=new Counter();
13 PrintWriter out = null;
14 try {
15 out = new PrintWriter("text.txt");
16 } catch (FileNotFoundException e) {
17 // TODO Auto-generated catch block
18 e.printStackTrace();
19 }
20 // 定义一个变量用来统计得分
21 int sum = 0;
22 int k=0;
23 // 通过循环生成10道题
24 for (int i = 0; i < 10; i++) {
25
26 // 随机生成两个100以内的随机数作加减乘除
27 int a = (int) Math.round(Math.random() * 100);
28 int b = (int) Math.round(Math.random() * 100);
29 int d = (int) Math.round(Math.random() * 3);
30
31 switch (d){
32
33 case 0:
34 if(a%b == 0) {
35 System.out.println(a + "/" + b + "=");
36 break;
37 }
38 //int c = in.nextInt();
39 //out.println(a + "/" + b + "="+c);
40 case 1:
41 System.out.println(a + "*" + b + "=");
42 //int c1 = in.nextInt();
43 //out.println(a + "*" + b + "="+c1);
44 break;
45 case 2:
46 System.out.println(a + "+" + b + "=");
47 //int c2 = in.nextInt();
48 //out.println(a + "+" + b + "="+c2);
49 break;
50 case 3:
51 if(a>b) {
52 System.out.println(a + "-" + b + "=");
53 break;
54 }
55 //int c3 = in.nextInt();
56 //out.println(a + "-" + b + "="+c3);
57
58 }
59
60 // 定义一个整数用来接收用户输入的答案
61 double c = in.nextDouble();
62
63 // 判断用户输入的答案是否正确,正确给10分,错误不给分
64 if (c == a / b | c == a * b | c == a + b | c == a - b) {
65 sum += 10;
66 System.out.println("恭喜答案正确");
67 }
68 else {
69 System.out.println("抱歉,答案错误");
70
71 }
72 out.println(a + "/" + b + "="+c );
73 out.println(a + "*" + b + "="+c);
74 out.println(a + "+" + b + "="+c);
75 out.println(a + "-" + b + "="+c);
76
77 }
78 //输出用户的成绩
79 System.out.println("你的得分为"+sum);
80
81 out.println("成绩:"+sum);
82 out.close();
83 }
84 }
运行结果如下:
任务3:本学期课程已结束,请汇总《面向对象程序设计课程学习进度条》的数据,统计个人专业能力提升的数据。并从学习内容、学习方法、学习心得几个方面进行课程学习总结,也希望你对课程的不足提出建议和意见。
4. 实验总结:
通过本学期的实验课程学习,真的学到了很多,尤其是在老师和助教学长的耐心指导下,从最开始JDK的安装到后来自己能写出可以运行的程序,很开心。这并不是结束,而是另一个新的开始,我会继续努力,学习更多。
来源:oschina
链接:https://my.oschina.net/u/4415618/blog/4154128