1. 程式人生 > >楊玲 201771010133《面向物件程式設計(java)》第十八週學習總結

楊玲 201771010133《面向物件程式設計(java)》第十八週學習總結

 《面向物件程式設計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的安裝到後來自己能寫出可以執行的程式,很開心。這並不是結束,而是另一個新的開始,我會繼續努力,學習更多。