天天看點

結對程式設計1-四則運算(基于GUI)

林曉芳201421123092、陳惠201421123096

coding 位址:https://git.coding.net/lianlian/92.96.1.git

一.題目描述

我們在個人作業1中,用各種語言實作了一個指令行的四則運算小程式。進一步,本次要求把這個程式做成GUI(可以是Windows PC 上的,也可以是Mac、Linux,web,手機上的),成為一個有基本功能、一定價值的程式。在下面的功能需求中實作兩個:

  1. 記錄使用者的對錯總數,程式退出再啟動的時候,能把以前的對錯數量儲存并在此基礎上增量計算;
  2. 有計時功能,能顯示使用者開始答題後的消耗時間;
  3. 界面支援中文簡體/中文繁體/英語,使用者可以選擇一種。

二.分析實作

a.需求分析:

1.實作GUI界面;

2.實作曆史對錯題數記錄;

3.能夠顯示使用者答題時間;

4.提供中文簡體/中文繁體/英語多種語言選擇。

b.功能設計:

1.基本功能:多種語言選擇、顯示曆史正确率、答題計時;

2.擴充功能:生成使用者錯題集。

c.設計實作:

  • Background//背景事件處理類:

        方法:createTest()//定義一個随機數

                 checkAnswer(String[] answers)//傳入一組答案,校驗答案

                 getQuestions()//擷取完整的題目

                 getStandardAnswer()//擷取标準答案

                 upDate(Integer right,Integer all)//上傳正确率

  • Client//用戶端類:

        方法:Client()//用戶端構造器

                 createComponent()//建立面闆

                 showTime()//顯示答題時間

                 showHistory()//顯示曆史正确率

                 setLanguage()//設定用戶端語言

                 actionPerformed(ActionEvent e)//按鈕事件

  • Calculator//計算類:

        方法:add(Fraction a, Fraction b)//加法

                 sub(Fraction a, Fraction b)//減法 

                 mul(Fraction a, Fraction b)//乘法

                 div(Fraction a, Fraction b)//除法

  • Fraction//分數類:

        屬性:numerator//分子

                 denominator//分母

        方法:creatfraction()//建立分數   

                 GCD(int m, int n)//計算最大公約數

                 Reduction(int m, int n)//約分

                 compare(String m, String n)//比較輸入結果與答案

  • Test//運作類:

        main函數執行

d.代碼說明:

上傳正确率:

結對程式設計1-四則運算(基于GUI)
public void upDate(Integer right,Integer all){
        try {
            writer = new BufferedWriter(new FileWriter(new File("history/accuracy.txt")));
            writer.write(right.toString());
            writer.newLine();
            writer.write(all.toString());
            writer.newLine();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }      
結對程式設計1-四則運算(基于GUI)

顯示答題時間:

結對程式設計1-四則運算(基于GUI)
public void showTime() {
        labTime = new JLabel(tips.get(1) + "00:00");
        labTime.setBounds(580, 0, 120, 50);
        jpMain.add(labTime);
        // 啟動記時線程
        new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        seconds++;
                        if (seconds >= 60) {
                            seconds = 0;
                            minutes++;
                        }
                        // 修正分鐘數和秒鐘數
                        if (seconds < 10)
                            secStr = "0" + seconds.toString();
                        else
                            secStr = seconds.toString();
                        if (minutes < 10)
                            minStr = "0" + minutes.toString();
                        else
                            minStr = minutes.toString();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    labTime.setText(tips.get(1) + minStr + ":" + secStr);
                    if(isEnd)
                        break;
                }
            }
        }.start();
    }      
結對程式設計1-四則運算(基于GUI)

顯示曆史正确率:

結對程式設計1-四則運算(基于GUI)
public void showHistory() {
        labAccuracy = new JLabel();
        labAccuracy.setBounds(150, 0, 120, 50);
        try {
            reader = new BufferedReader(new FileReader(new File("history/accuracy.txt")));
            his_rightNum = reader.readLine();
            his_allNum = reader.readLine();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        labAccuracy.setText(tips.get(2) + his_rightNum + "/" + his_allNum);
        jpMain.add(labAccuracy);
    }      
結對程式設計1-四則運算(基于GUI)

設定用戶端語言:

結對程式設計1-四則運算(基于GUI)
public void setLanguage() {
        String[] choiceLanguage = { "簡體中文", "繁體中文", "English" };
        String language = (String) JOptionPane.showInputDialog(null, "請選擇用戶端的語言:\n", "Choice a language for client",
                JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), choiceLanguage, "簡體中文");
        if (language == null) {
            System.exit(-1);
        } else {
            try {
                reader = new BufferedReader(new FileReader(new File("language/" + language + ".txt")));
                String s;
                while ((s = reader.readLine()) != null) {
                    tips.add(s);
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }      
結對程式設計1-四則運算(基于GUI)

e.測試運作:

結對程式設計1-四則運算(基于GUI)
結對程式設計1-四則運算(基于GUI)
結對程式設計1-四則運算(基于GUI)
結對程式設計1-四則運算(基于GUI)
結對程式設計1-四則運算(基于GUI)

三.PSP展示

PSP2.1 Personal Software Process Stages Time (%) Senior Student Time (%)
Planning 計劃 2h 2.5h
· Estimate 估計這個任務需要多少時間 40h 42h
· Analysis 需求分析 (包括學習新技術) 3h 4h
· Coding Standard 代碼規範 1h
· Design 具體設計
· Coding 具體編碼 25h 30h
· Test 測試(自我測試,修改代碼,送出修改) 1.5h
Reporting 報告

四.總結

      不同的兩個人在面對同件事情上會産生不一樣的想法,這使得我們在剛剛開始合作的時候發生了一些小争執,但是我們倆都屬于“說服型”的人,能夠按照自己的分析将對方說服并與對方建立共識,最後成功的完成了這次的結對程式設計作業。對我來說,我認為結對程式設計是一次有趣的體驗,它能夠讓你發現自身的不足,更能讓你體會團隊的魅力。但是,兩個人在交流的時候一定要敢于提出自己的想法和看法,一定在大緻上要達成共識,這樣兩個人的作用才能發揮到最大。互相影響、互相鞭策,我想這就是結對程式設計最大的魅力。

結對程式設計1-四則運算(基于GUI)