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 packagefff;2 importjava.awt.EventQueue;3

4 importjavax.swing.JFrame;5

6 public classMain {7 public static voidmain(String[] args) {8 EventQueue.invokeLater(() ->{9 DemoJFrame page = newDemoJFrame();10 });11 }12 }

View Code

1 packagefff;2 importjava.awt.Dimension;3 importjava.awt.Toolkit;4 importjava.awt.Window;5

6 public classWinCenter {7 public static voidcenter(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 }

View Code

1 packagefff;2 import java.awt.*;3 import java.awt.event.*;4 import javax.swing.*;5 import javax.swing.border.*;6 public class DemoJFrame extendsJFrame {7 publicDemoJFrame() {8 JPanel panel1 = newJPanel();9 panel1.setPreferredSize(new Dimension(700, 45));10 panel1.setLayout(new GridLayout(1, 4));11 JLabel label1 = new JLabel("Name:");12 JTextField j1 = new JTextField("");13 JLabel label2 = new JLabel("Qualification:");14 JComboBox j2 = new JComboBox<>();15 j2.addItem("chuzhong");16 j2.addItem("gaozhong");17 j2.addItem("undergraduate");18 panel1.add(label1);19 panel1.add(j1);20 panel1.add(label2);21 panel1.add(j2);22

23 JPanel panel2 = newJPanel();24 panel2.setPreferredSize(new Dimension(700, 65));25 panel2.setLayout(new GridLayout(1, 4));26 JLabel label3 = new JLabel("Address:");27 JTextArea j3 = newJTextArea();28 JLabel label4 = new JLabel("Hobby:");29 JPanel p = newJPanel();30 p.setLayout(new GridLayout(3, 1));31 p.setBorder(BorderFactory.createLineBorder(null));32 JCheckBox c1 = new JCheckBox("Reading");33 JCheckBox c2 = new JCheckBox("Singing");34 JCheckBox c3 = new JCheckBox("Dancing");35 p.add(c1);36 p.add(c2);37 p.add(c3);38 panel2.add(label3);39 panel2.add(j3);40 panel2.add(label4);41 panel2.add(p);42

43 JPanel panel3 = newJPanel();44 panel3.setPreferredSize(new Dimension(700, 150));45 FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 70, 40);46 panel3.setLayout(flowLayout1);47 JLabel label5 = new JLabel("Sex:");48 JPanel p1 = newJPanel();49 p1.setLayout(new GridLayout(2,1));50 p1.setBorder(BorderFactory.createLineBorder(null));51 ButtonGroup bu = newButtonGroup();52 JRadioButton jr1 = new JRadioButton("Male");53 JRadioButton jr2 = new JRadioButton("Female");54 bu.add(jr1);55 bu.add(jr2);56 p1.add(jr1);57 p1.add(jr2);58 panel3.add(label5);59 panel3.add(p1);60 add(panel1);61 add(panel2);62 add(panel3);63

64 JPanel panel4 = newJPanel();65 panel4.setPreferredSize(new Dimension(700, 150));66 JButton b1 = new JButton("Validate");67 panel4.add(b1);68 JButton b2 = new JButton("Reset");69 panel4.add(b2);70 add(panel4);71

72 FlowLayout flowLayout = newFlowLayout();73 this.setLayout(flowLayout);74 this.setTitle("Students Detail");75 this.setBounds(300, 300, 800, 400);76 this.setVisible(true);77 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);78

79 b1.addActionListener(newActionListener() {80

81

82 public voidactionPerformed(ActionEvent e) {83 //TODO 自動生成的方法存根

84 String xueli =j2.getSelectedItem().toString();85 System.out.println("Name:" +j1.getText());86 System.out.println("Qualification:" +xueli);87 String hobbystring = "Hobby:";88 if(c1.isSelected()) {89 hobbystring += "Reading";90 }91 if(c2.isSelected()) {92 hobbystring += "Singing";93 }94 if(c3.isSelected()) {95 hobbystring += "Dancing";96 }97 System.out.println("Address:" +j3.getText());98 if(jr1.isSelected()) {99 System.out.println("Sex:Male");100 }101 if(jr2.isSelected()) {102 System.out.println("Sex:Female");103 }104 System.out.println(hobbystring);105 }106 });107 b2.addActionListener(newActionListener() {108

109 public voidactionPerformed(ActionEvent e) {110 //TODO 自動生成的方法存根

111 j1.setText(null);112 j3.setText(null);113 j2.setSelectedIndex(0);114 c1.setSelected(false);115 c2.setSelected(false);116 c3.setSelected(false);117 bu.clearSelection();118 }119 });120 }121

122 public static voidmain(String args[]) {123 newDemoJFrame();124 }125

126 }

View Code

練習2:采用GUI界面設計以下程序:

l  編制一個程序,將身份證號.txt 中的信息讀入到內存中;

l  按姓名字典序輸出人員信息;

l  查詢最大年齡的人員信息;

l  查詢最小年齡人員信息;

l  輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;

l  查詢人員中是否有你的同鄉。

l  輸入身份證信息,查詢所提供身份證號的人員信息,要求輸入一個身份證數字時,查詢界面就顯示滿足查詢條件的查詢結果,且隨着輸入的數字的增多,查詢匹配的范圍逐漸縮小。

1 packageDemo;2

3 import java.awt.*;4 import javax.swing.*;5

6 public classButtonTest {7 public static voidmain(String[] args) {8 EventQueue.invokeLater(() ->{9 JFrame frame = newMain();10 frame.setTitle("身份證");11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);12 frame.setVisible(true);13 });14 }15 }

View Code

1 packageDemo;2

3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.InputStreamReader;7 importjava.io.FileNotFoundException;8 importjava.io.IOException;9 importjava.util.ArrayList;10 importjava.util.Arrays;11 importjava.util.Collections;12 importjava.util.Scanner;13 import java.awt.*;14 import javax.swing.*;15 import java.awt.event.*;16

17 public class Main extendsJFrame {18 private static ArrayListstudentlist;19 private static ArrayListlist;20 privateJPanel panel;21 privateJPanel buttonPanel;22 private static final int DEFAULT_WITH = 600;23 private static final int DEFAULT_HEIGHT = 300;24

25 publicMain() {26 studentlist = new ArrayList<>();27 Scanner scanner = newScanner(System.in);28 File file = new File("studentfile.txt");29 try{30 FileInputStream fis = newFileInputStream(file);31 BufferedReader in = new BufferedReader(newInputStreamReader(fis));32 String temp = null;33 while ((temp = in.readLine()) != null) {34

35 Scanner linescanner = newScanner(temp);36

37 linescanner.useDelimiter(" ");38 String name =linescanner.next();39 String number =linescanner.next();40 String sex =linescanner.next();41 String age =linescanner.next();42 String province =linescanner.nextLine();43 Student student = newStudent();44 student.setName(name);45 student.setnumber(number);46 student.setsex(sex);47 int a =Integer.parseInt(age);48 student.setage(a);49 student.setprovince(province);50 studentlist.add(student);51

52 }53 } catch(FileNotFoundException e) {54 System.out.println("學生信息文件找不到");55 e.printStackTrace();56 } catch(IOException e) {57 System.out.println("學生信息文件讀取錯誤");58 e.printStackTrace();59 }60 panel = newJPanel();61 panel.setLayout(newBorderLayout());62 JTextArea jt = newJTextArea();63 panel.add(jt);64 add(panel, BorderLayout.NORTH);65

66 buttonPanel = newJPanel();67 buttonPanel.setLayout(new GridLayout(1, 7));68 JButton jButton = new JButton("字典排序");69 JButton jButton1 = new JButton("年齡最大和年齡最小");70 JLabel lab = new JLabel("猜猜你的老鄉");71 JTextField jt1 = newJTextField();72 JLabel lab1 = new JLabel("找找同齡人(年齡相近):");73 JTextField jt2 = newJTextField();74 JLabel lab2 = new JLabel("輸入你的身份證號碼:");75 JTextField jt3 = newJTextField();76 JButton jButton2 = new JButton("退出");77

78 jButton.addActionListener(newActionListener() {79 public voidactionPerformed(ActionEvent e) {80 Collections.sort(studentlist);81 jt.setText(studentlist.toString());82 }83 });84 jButton1.addActionListener(newActionListener() {85 public voidactionPerformed(ActionEvent e) {86 int max = 0, min = 100;87 int j, k1 = 0, k2 = 0;88 for (int i = 1; i < studentlist.size(); i++) {89 j =studentlist.get(i).getage();90 if (j >max) {91 max =j;92 k1 =i;93 }94 if (j

99 }100 jt.setText("年齡最大:" + studentlist.get(k1) + "年齡最小:" +studentlist.get(k2));101 }102 });103 jButton2.addActionListener(newActionListener() {104 public voidactionPerformed(ActionEvent e) {105 dispose();106 System.exit(0);107 }108 });109 jt1.addActionListener(newActionListener() {110 public voidactionPerformed(ActionEvent e) {111 String find =jt1.getText();112 String text = "";113 String place = find.substring(0, 3);114 for (int i = 0; i < studentlist.size(); i++) {115 if (studentlist.get(i).getprovince().substring(1, 4).equals(place)) {116 text += "\n" +studentlist.get(i);117 jt.setText("老鄉:" +text);118 }119 }120 }121 });122 jt2.addActionListener(newActionListener() {123 public voidactionPerformed(ActionEvent e) {124 String yourage =jt2.getText();125 int a =Integer.parseInt(yourage);126 int near =agenear(a);127 int value = a -studentlist.get(near).getage();128 jt.setText("年齡相近:" +studentlist.get(near));129 }130 });131

132 jt3.addKeyListener(newKeyAdapter() {133 public voidkeyTyped(KeyEvent e) {134 list = new ArrayList<>();135 Collections.sort(studentlist);136 String key =jt3.getText();137

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(intage) {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

174 }

View Code

1 packageDemo;2

3 public class Student implements Comparable{4

5 privateString name;6 privateString number ;7 privateString sex ;8 private intage;9 privateString province;10

11 publicString getName() {12 returnname;13 }14 public voidsetName(String name) {15 this.name =name;16 }17 publicString getnumber() {18 returnnumber;19 }20 public voidsetnumber(String number) {21 this.number =number;22 }23 publicString getsex() {24 returnsex ;25 }26 public voidsetsex(String sex ) {27 this.sex =sex ;28 }29 public intgetage() {30

31 returnage;32 }33 public void setage(intage) {34 //int a = Integer.parseInt(age);

35 this.age=age;36 }37

38 publicString getprovince() {39 returnprovince;40 }41 public voidsetprovince(String province) {42 this.province=province ;43 }44

45 public intcompareTo(Student o) {46 return this.name.compareTo(o.getName());47 }48

49 publicString toString() {50 return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";51 }52 }

View Code

練習3:采用GUI界面設計以下程序

l  編寫一個計算器類,可以完成加、減、乘、除的操作

l  利用計算機類,設計一個小學生100以內數的四則運算練習程序,由計算機隨機產生10道加減乘除練習題,學生輸入答案,由程序檢查答案是否正確,每道題正確計10分,錯誤不計分,10道題測試結束后給出測試總分;

l  將程序中測試練習題及學生答題結果輸出到文件,文件名為test.txt。

1 packageui_test;2

3 public classMain {4

5 public static voidmain(String[] args) {6 MyExGUI lg = newMyExGUI();7 //new MyExGUI();

8

9 }10

11 }

View Code

1 import java.awt.*;2 importjava.awt.event.ActionEvent;3 importjava.awt.event.ActionListener;4 importjava.io.BufferedOutputStream;5 importjava.io.File;6 importjava.io.FileOutputStream;7 importjava.io.IOException;8 importjava.util.ArrayList;9

10 import javax.swing.*;11

12 public class MyExGUI extendsJFrame {13 ArrayList user_zongti = new ArrayList();14 ArrayList user_zonganswer = new ArrayList();15 ArrayList user_answer = new ArrayList();16 ArrayList true_answer = new ArrayList();17 ArrayList jta_timu = new ArrayList();18 ArrayList jta_zong = new ArrayList();19 ArrayList user_fenshu = new ArrayList();20 JMenuBar jm; //菜單條組件

21 JMenu menu;//菜單

22 JMenuItem item1, item2;//菜單項

23 JMenu build; //二級菜單

24 JMenuItem file, project;25 TextArea answer_all = newTextArea();26 TextField jta = newTextField();27 TextField jta_answer = newTextField();28 JLabel num_answer = newJLabel();29 JLabel answer;30 JToolBar jtb;//工具條

31 JButton jb1, jb2, jb3, jb4, jb5, jb6, jb7, jb_next;32 intanswer_count;33 intanswer_fenshu;34

35 publicMyExGUI() {36 //創建菜單

37 jm = newJMenuBar();38

39 menu = new JMenu("文件(F)");40 menu.setMnemonic('f'); //助記符

41

42 build = new JMenu("新建");43

44 file = new JMenuItem("文件");45 project = new JMenuItem("答題");46 item1 = new JMenuItem("保存(S)");47 item2 = new JMenuItem("退出");48

49 answer = new JLabel("第 1 題");50

51 //添加菜單項至菜單上

52 build.add(file);53 build.add(project);54

55 menu.add(build);56 menu.add(item1);57 menu.add(item2);58 menu.addSeparator();59 //將菜單加入至菜單欄

60 jm.add(menu);61

62 JPanel contentPanel = newJPanel();63 contentPanel.setLayout(null);64 JLabel daan = new JLabel("答案");65 JLabel dengyu = new JLabel("=");66 num_answer =answer;67 num_answer.setFont(new Font("宋體", Font.BOLD, 22));68 jb_next = new JButton("下一題");69 jta.setFont(new Font("宋體", Font.BOLD, 22));70 jta_answer.setFont(new Font("宋體", Font.BOLD, 22));71 jb_next.setFont(new Font("宋體", Font.BOLD, 22));72 daan.setFont(new Font("宋體", Font.BOLD, 22));73 dengyu.setFont(new Font("宋體", Font.BOLD, 22));74

75 contentPanel.add(num_answer);76 contentPanel.add(daan);77 contentPanel.add(dengyu);78 contentPanel.add(jta);79

80 contentPanel.add(jta_answer);81 contentPanel.add(answer_all);82 contentPanel.add(jb_next);83

84 num_answer.setBounds(90, 20, 130, 50);85 daan.setBounds(250, 20, 90, 50);86 jta.setBounds(50, 70, 150, 30);87 dengyu.setBounds(205, 70, 20, 20);88 jta_answer.setBounds(230, 70, 100, 30);89 jb_next.setBounds(350, 70, 110, 30);90 answer_all.setBounds(50, 120, 400, 300);91

92 this.setJMenuBar(jm); //添加菜單欄,不能設定位置,會自動放在最上部

93 this.add(contentPanel);94

95 this.setTitle("在線答題系統");96 this.setSize(600, 500);97 this.setVisible(true);98 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);99 item1.addActionListener(newActionListener() {100 public voidactionPerformed(ActionEvent arg0) {101 FileOutputStream outSTr = null;102 BufferedOutputStream Buff = null;103 boolean flag = true;104 File file;105 //String test ;

106 do{107 //test = "test"+count;

108

109 String inputValue = JOptionPane.showInputDialog("Please input file name");110 file = new File(inputValue + "test.txt");111 if (!file.exists()) {112 //創建文件

113 try{114

115 flag =file.createNewFile();116

117 } catch(IOException e) {118 e.printStackTrace();119

120 }121 flag = false;122 } else{123

124 JOptionPane.showMessageDialog(null, "該文件名已存在,請重新輸入", "ERROR", JOptionPane.ERROR_MESSAGE);125 flag = true;126 }127 } while(flag);128 //寫入文件

129 String u_answer;130 try{131 outSTr = newFileOutputStream(file);132 Buff = newBufferedOutputStream(outSTr);133 System.out.println("選擇是后執行的代碼" + user_zongti.size() +user_answer.size());134 for (int i = 0; i < user_zongti.size(); i++) {135 try{136 Buff.write(user_zongti.get(i).getBytes());137 Buff.write(" ".getBytes());138 u_answer =user_answer.get(i);139 if (u_answer.equals(""))140 u_answer = "沒有作答";141

142 Buff.write(u_answer.getBytes());143 Buff.write("\r\n".getBytes());144 } catch(IOException e) {145 e.printStackTrace();146 i--;147 }148 }149 Buff.flush();150 Buff.close();151

152 } catch(IOException e) {153 e.printStackTrace();154 }155 try{156 outSTr.close();157 } catch(IOException e) {158 e.printStackTrace();159 }160 user_zongti.clear();161 user_answer.clear();162 }163 });164

165 project.addActionListener(newActionListener() {166 public voidactionPerformed(ActionEvent arg0) {167 arithmetic art = newarithmetic();168 true_answer =art.list_answer;169 jta_timu =art.list_timu;170 jta_zong =art.list;171 answer_count = 1;172 answer_all.setText("");173 for (int i = 0; i < art.list_timu.size(); i++) {174 user_zongti.add(jta_zong.get(i));175 answer_all.append(jta_timu.get(i));176 answer_all.append("\r\n");177 }178 num_answer.setText("第 " + answer_count + " 題");179 jta.setText(jta_timu.get(answer_count - 1));180 answer_count++;181

182 }183 });184 jb_next.addActionListener(newActionListener() {185 public voidactionPerformed(ActionEvent arg0) {186 String temp;187 temp =jta_answer.getText();188

189 if (jta.getText().equals("")) {190 JOptionPane.showMessageDialog(null, "錯誤,請導入題庫", "錯誤", JOptionPane.ERROR_MESSAGE);191 return;192 }193 jta_answer.setText("");194 if (answer_count <= 10) {195 if(isInteger(temp)) {196

197 user_answer.add(temp);198 System.out.println("選擇否后執行的代碼" + temp + "user_size" +user_answer.size());199 num_answer.setText("第 " + answer_count + " 題");200 jta.setText(jta_timu.get(answer_count - 1));201 answer_count++;202 } else{203 JOptionPane.showMessageDialog(null, "錯誤", "請輸入數字", JOptionPane.ERROR_MESSAGE);204 }205 } else{206 user_answer.add(temp);207 System.out.println("選擇否后執行的代碼" + temp + "user_size" +user_answer.size());208 answer_fenshu = 0;209 for (int i = 0; i < user_answer.size(); i++) {210 if(user_answer.get(i).equals(true_answer.get(i)))211 answer_fenshu += 5;212 }213 user_fenshu.add(answer_fenshu);214 Object[] options = { "是", "取消"};215 int res = JOptionPane.showOptionDialog(null, "是否查看成績", "答題完畢", JOptionPane.DEFAULT_OPTION,216 JOptionPane.YES_NO_OPTION, null, options, options[0]);217 if (res ==JOptionPane.YES_OPTION) {218 chart ct = newchart(user_fenshu);219 ct.setVisible(true);220

221 } else{222 Object[] option = { "是", "取消"};223 int res1 = JOptionPane.showOptionDialog(null, "是否退出程序", "終止框", JOptionPane.DEFAULT_OPTION,224 JOptionPane.YES_NO_OPTION, null, option, option[0]);225

226 if (res1 ==JOptionPane.YES_OPTION) {227 dispose();228 System.exit(0);229

230 } else{231

232 }233

234 }235

236 }237

238 }239 });240

241 item2.addActionListener(newActionListener() {242 public voidactionPerformed(ActionEvent e) {243 dispose();244 System.exit(0);245 }246 });247

248 }249

250 public static booleanisInteger(String str) {251 for (int i = str.length(); --i >= 0;) {252 if (!Character.isDigit(str.charAt(i))) {253 return false;254 }255 }256 return true;257 }258

259 }

View Code

1 importjava.io.BufferedOutputStream;2 importjava.io.File;3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.util.ArrayList;6 importjava.util.Random;7 importjava.util.Scanner;8

9 public classarithmetic {10 ArrayList list = new ArrayList();11 ArrayList list_timu = new ArrayList();12 ArrayList list_answer = new ArrayList();13

14 publicarithmetic() {15 FileOutputStream outSTr = null;16 BufferedOutputStream Buff = null;17 int number_n = 10, count;18

19 ArrayList list_temp = new ArrayList();20 String[] operator = new String[] { "+", "-", "*", "/"};21

22 Random rand = newRandom();23 File file1 = new File("test.txt");24 if(file1.exists()) {25 //創建文件

26 try{27 file1.createNewFile();28 } catch(IOException e) {29 e.printStackTrace();30 }31 }32

33 while (number_n > 0) {34 int[] number_temp = new int[rand.nextInt(2) + 3];35 String[] str_temp = new String[number_temp.length - 1];36 for (int i = 0; i < number_temp.length; i++) {37 if (i < number_temp.length - 1) {38 number_temp[i] = rand.nextInt(100);39 list_temp.add(String.valueOf(number_temp[i]));40 str_temp[i] = operator[rand.nextInt(4)];41 list_temp.add(str_temp[i]);42

43 }44

45 else{46 number_temp[i] = rand.nextInt(100);47 list_temp.add(String.valueOf(number_temp[i]));48 }49 }50

51 count =calculate_RPN(produce_RPN(list_temp));52 if (count != -1) {53 list_timu.add(transform_string(list_temp));54 list_answer.add(String.valueOf(count));55 list_temp.add(" = " +count);56 list.add(transform_string(list_temp));57 number_n--;58 list_temp.clear();59 } else

60 list_temp.clear();61 System.out.println(number_n);62

63 }64 try{65 outSTr = newFileOutputStream(file1);66 Buff = newBufferedOutputStream(outSTr);67 for (int i = 0; i < list.size(); i++) {68 try{69 Buff.write(list.get(i).getBytes());70 Buff.write("\r\n".getBytes());71 } catch(IOException e) {72 e.printStackTrace();73 i--;74 }75 }76 Buff.flush();77 Buff.close();78

79 } catch(IOException e) {80 e.printStackTrace();81 }82 //Buff.close();

83 try{84 outSTr.close();85 } catch(IOException e) {86 e.printStackTrace();87 }88

89 for (int i = 0; i < list.size(); i++) {90 System.out.print(list.get(i));91 System.out.println();92 }93 System.out.print("計算完畢!");94

95 }96

97 public static int calculate_RPN(ArrayListlist_temp) {98 int i = 0, t;99 double a = 0, b = 0;100 String l_temp;101 Stack sk = new Stack(10);102 for (t = 0; t < list_temp.size(); t++) {103 l_temp = list_temp.get(i++);104 if (!isInteger(l_temp)) {105 b =sk.mypop();106 a =sk.mypop();107 switch(l_temp) {108 case "+":109 sk.mypush(a +b);110 break;111 case "-":112 if(!(a

116 return -1;117 break;118 case "*":119 sk.mypush(a *b);120 break;121 case "/":122 if (b == 0||a

132 }133 if (!sk.myisempty()) {134 a =sk.mypop();135 b = a - (int) a;136 System.out.println("a: " +a);137 if (a > 0 && b == 0) {138 return (int) a;139 } else

140 return -1;141 } else

142 return -1;143

144 }145

146 public static ArrayList produce_RPN(ArrayListlist_temp) {147 int t = 0, i = 0;148 String tmp;149 Tack mytack = new Tack(10);150 ArrayList lt_temp = new ArrayList();151 while (true) {152 tmp = list_temp.get(i++);153 if(isInteger(tmp)) {154 lt_temp.add(tmp);155 } else{156 if(mytack.myisempty()) {157 mytack.mypush(tmp);158 }159

160 else{161 if(isCPriority(tmp, mytack.mypeek()))162 mytack.mypush(tmp);163 else{164 lt_temp.add(mytack.mypop());165 mytack.mypush(tmp);166 }167

168 }169 }170 if (i >=list_temp.size()) {171 while (!mytack.myisempty())172 lt_temp.add(mytack.mypop());173 System.out.println(transform_string(list_temp));174 list_temp =lt_temp;175 System.out.println(list_temp);176 returnlist_temp;177 }178 }179

180 }181

182 public static booleanisInteger(String str) {183 for (int i = str.length(); --i >= 0;) {184 if (!Character.isDigit(str.charAt(i))) {185 return false;186 }187 }188 return true;189 }190

191 public static booleanisCPriority(String str, String s) {192 if ((str + s).equals("*+") || (str + s).equals("*-") || (str + s).equals("/+") || (str + s).equals("/-"))193 return true;194 else

195 return false;196 }197

198 public static String transform_string(ArrayListlist_temp) {199 String s = "";200 for (int i = 0; i < list_temp.size(); i++) {201 s +=list_temp.get(i);202 }203 returns;204

205 }206

207 static classStack {208 intmytop;209 doublestk[];210

211 public Stack(intnum) {212 mytop = -1;213 stk = new double[num];214 }215

216 /*出棧*/

217 doublemypop() {218 double peek =stk[mytop];219 mytop--;220 returnpeek;221 }222

223 /*入棧*/

224 void mypush(doublex) {225 mytop++;226 stk[mytop] =x;227

228 }229

230 /*判空*/

231 Boolean myisempty() {232 if (mytop == -1)233 return true;234 else

235 return false;236 }237

238 /*取棧頂元素*/

239 doublemypeek() {240 double peek =stk[mytop];241 returnpeek;242 }243

244 /*棧大小*/

245 intmysize() {246 return mytop + 1;247 }248 }249

250 static classTack {251 intmytop;252 String tk[];253

254 public Tack(intnum) {255 mytop = -1;256 tk = newString[num];257 }258

259 /*出棧*/

260 String mypop() {261 String peek =tk[mytop];262 mytop--;263 returnpeek;264 }265

266 /*入棧*/

267 voidmypush(String x) {268 mytop++;269 tk[mytop] =x;270

271 }272

273 /*判空*/

274 Boolean myisempty() {275 if (mytop == -1)276 return true;277 else

278 return false;279 }280

281 /*取棧頂元素*/

282 String mypeek() {283 String peek =tk[mytop];284 returnpeek;285 }286

287 /*棧大小*/

288 intmysize() {289 return mytop + 1;290 }291

292 }293

294 }

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,提高自己的編程水平。

java书籍 李清华_201772020113 李清華《面向對象程序設計(java)》第18周學習總結...相关推荐

  1. java写便签_如何编写一个便签程序(用Java语言编写)

    如何编写一个便签程序(用Java语言编写) 热度:336   发布时间:2011-02-18 11:44:16 如何编写一个便签程序(用Java语言编写) 因为以前没有好好学习Java,都搞忘了,请大 ...

  2. java网页安全提示_Win7系统打开网页提示应用程序已被JAVA安全阻止的解决方法

    打开电脑浏览网页是我们每天都会接触到的事情,而最近,不少网友表示,打开网页浏览的时候会提示"应用程序已被JAVA安全阻止",这是怎么回事呢?这是由于网页上运行JAVA脚本,但是wi ...

  3. java程序大笨钟怎么打,L1-018.大笨鍾(Java)PAT團體程序設計天梯賽-練習集

    微博上有個自稱"大笨鍾V"的家伙,每天敲鍾催促碼農們愛惜身體早點睡覺.不過由於笨鍾自己作息也不是很規律,所以敲鍾並不定時.一般敲鍾的點數是根據敲鍾時間而定的,如果正好在某個整點敲, ...

  4. 用java求解一元四次方程_Java程序設計(十四)----一個求一元二次方程根

    * 程序的版權和版本聲明部分 * Copyright (c) 2012, 煙台大學計算機學院學生 * All rights reserved. * 作 者: 劉鎮 * 完成日期: 2012 年 11 ...

  5. python java通过socket交互数据 等到python服务端停止程序才能在java客户端看到数据的问题

    发送的数据如果不以\n   \r\n结尾 客户端就会一直以为数据还在发送,就不打印,  以为是"数据数据数据数据....."的形式 所以必须自己截断自己的数据形式 //我发送的数据 ...

  6. 人工智能 java 坦克机器人系列: 强化学习_人工智能 Java 坦克機器人系列: 強化學習...

    級別:中級 2006 年 7 月13日 本文中,我們將使用強化學習來實現一個機器人.使用強化學習能創建一個自適應的戰斗機器人.這個機器人能在戰斗中根據環境取得最好的策略,並盡力使戰斗行為最佳.並在此過 ...

  7. 面向對象在VB6語言中的應用

    原文链接:http://blog.csdn.net/areyan/archive/2006/05/28/758765.aspx 一:面向對象(Object-Oriente)編程的基本概念 什麼是面向對 ...

  8. JAVA基础-栈与堆,static、final修饰符、内部类和Java内存分配

    JAVA基础-栈与堆,static.final修饰符.内部类和Java内存分配 发布时间: 2013/01/12 22:29 QQ空间 新浪微博 腾讯微博 人人网 豆瓣网 百度空间 百度搜藏 开心网 ...

  9. java初学者书籍_面向初学者的5本最佳Java核心书籍

    java初学者书籍 Today I am sharing the best java books to learn java programming. Java is one of the most ...

最新文章

  1. python中open函数的使用
  2. Intent以及IntentFilter详解
  3. mencoder视频旋转
  4. 转载、Python的编码处理(二)
  5. 【C++ Primer学习笔记】第2章:变量和基本类型
  6. chromebook刷机_如何将网站添加到您的Chromebook架子上
  7. WordPress获取当前分类ID的四种方法
  8. 第二节:如何正确使用WebApi和使用过程中的一些坑
  9. 【最佳实践】授权子账号进行OSS图片样式设置
  10. python的if语句后面怎么加布尔运算符号是_python if 语句,布尔运算
  11. smarty中js的调用方法
  12. C#LeetCode刷题之#849-到最近的人的最大距离(Maximize Distance to Closest Person)
  13. 老表笔记之电商项目实战测试流程
  14. 编译安装PCL点云库,Kinect2驱动,乐视Astra相机驱动
  15. 常见问题与常见算法的时间复杂度
  16. laravel 与 tp5 获取控制器 方法名
  17. [奶奶看了都会]京东自动签到薅羊毛-完整教程
  18. C++判断两个链表是否相交算法
  19. html传递只能用this,Html中使用JS $(this).attr('action'); 无法获值
  20. Java零基础个人学习路线总结

热门文章

  1. 太原理工web课程答案——web程序设计课程答案 (第八章 CSS3页面布局)持续发布中——
  2. mysql和linux的题目_最强Linux和Mysql面试题套餐,让你的面试无懈可击!
  3. Java中double类型输出小数点后两位
  4. IT项目管理的六种错误思维
  5. C/C++黑魔法-编译期运行的sizeof
  6. Mysql我国省市区字典数据
  7. 患癌女孩冯莹的爱情梦想
  8. 墨客UTXO和account模型
  9. ESXI-vCenter-NSX-系统虚拟化
  10. 信息安全密码学:DES算法的核心 E盒、S盒、P盒