实验楼第三次实验

实验三 敏捷开发与XP实践

l  实验内容

  1. XP基础
  2. XP核心实践
  3. 相关工具

l  实验步骤

(一)敏捷开发与XP

软件工程是把系统的、有序的、可量化的方法应用到软件的开发、运营和维护上的过程

软件工程包括下列领域:软件需求分析、软件设计、软件构建、软件测试和软件维护

人们在开发、运营、维护软件的过程中有很多技术、做法、习惯和思想体系,软件工程把这些相关的技术和过程统一到一个体系中,叫“软件开发流程”

软件开发流程的目的是为了提高软件开发、运营、维护的效率,并提高软件的质量、用户满意度、可靠性和软件的可维护性

软件工程=开发流程+工具 

软件=程序+软件工程

软件企业=软件+商业模式 

常见的开发流程有:

RUP(Rational Unified Process)

RSP(Personal Software Process)

TSP(Team Software Process)

Agile Process

敏捷开发(Agile Development)是一种以人为核心、迭代、循序渐进的开发方法,包括很多模式,其中,极限编程(eXtreme Programming,XP)是一种全新而快捷的软件开发方法

 

   XP软件开发通过XP准则来表达:

 

XP的法则:快速反馈、假设简单性、递增更改、提倡更改、优质工作。

XP的活动:编码、测试、倾听、设计

 

(二)     编码标准

    编写代码一个重要的认识是“程序大多时候是给人看的”,编程标准使代码更容易阅读和理解,甚至可以保证其中的错误更少

    编程标准包含:具有说明性的名字、清晰的表达式、直截了当的控制流、可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性

编码标准中的版式就是一个很好的例子,版式虽然不会影响程序的功能,但会影响可读性

我们常见的是这样的代码:

 

程序没有最基本的缩进,解决方式Eclipse->source->Format/Ctrl+Shift+F

 

效果如下

 

代码标准中很重要的一项是如何给包、类、变量、方法等标识符命名,命名规则如下

 

 

(三)     结队编程

结对编程是XP中的重要实践,其中有两个角色

 

 

(四)版本控制

版本控制都提供了很多好处

 

例如HelloWorld

Code->shiyanlou_cs212->创建HelloWorld->创建并编辑HelloWorld.java

 

编译,运行,测试都没问题,提交代码

 

 

如图:先用git status查看一下代码状态,显示有未跟踪的代码,并用git add <file>...添加,使用git add HelloWorld.* 把要提交的文件的信息添加到索引库中。当我们使用git commit时,git将依据索引库中的内容来进行文件的提交。实验完成前使用git push保存文件

 

可以修改HelloWorld.java

 

(五)重构

    重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更

    重构中一个非常关键的前提就是“不改变软件外部行为”,它保证了我们在重构原有系统的同时,不会为原系统带来新的BUG,以确保重构的安全

    Eclipse菜单中的refactor体现出重构几乎是现代IDE的标配了

重构的第一项功能就是Rename,可以给类、包、方法、变量改名字

 

   这个类,类名,方法名和方法的参数名都有问题,没有注释的话是无法理解代码的。可以使用Eclipse中的重构功能来改名。用鼠标单击要改的名字-> Refactor->Rename...

   效果如下

 

   常犯问题是不会封装,该用类的地方都用了结构体。比如要定义一个类Student:

 

   Refactor->Encapsulate Field...

 

 

 

    上述代码还是有问题的,每次打印学生信息都这么写代码违反了DRY原则,造成代码重复,正常的重构可以使用Eclipse中的Extract Method...

     由于Java中所有的类都有个专门的toString方法,我们使用Eclipse中Source->Generate toString()... 给Student类产生一个toString方法并修改main的代码

 

(六)实践题目

游戏代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class BorderLayoutDemo extends JFrame {

JPanel p=new JPanel();
public int sum=0;

public BorderLayoutDemo(){

p.setLayout(new GridLayout(3,3,3,3)); //设置按钮(3行,3列,间隔3,间隔3)

//使用循环添加按钮,注意每次添加的按钮对象名称都是b

final int trapLocation=(int)((Math.random()*9)+1);

for(int i=1;i<10;i++){

final int checkTrap=i;

final JButton b=new JButton("Unknown");
p.add(b); //将按钮添加到面板中

//按钮产生效果
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){


if(checkTrap==trapLocation){
b.setText("Trap!");
EndGame(sum);
}

else{
int randomNum=0;
randomNum=(int)((Math.random()*10)+1);
b.setText(String.valueOf(randomNum));
sum=sum+randomNum;
}

}//ActionPerformed
}//ActionListener

);
}//结束for循环


getContentPane().add("Center",p); //将面板添加到中间位置

}

public void EndGame(int sum){

Component tip = null;
JOptionPane.showMessageDialog(tip, "A Trap Caugh! Your Score: "+sum);

//添加sum到文件
FileReader();

HighScores(sum);

Object[] options = {"Quit","Play Again"};
int response=JOptionPane.showOptionDialog ( null, "Quit or Play Again","Game Over",JOptionPane.YES_OPTION ,JOptionPane.PLAIN_MESSAGE,null, options, options[0] ) ;
if(response==0){
System.exit(0);
}else if(response==1){

this.dispose();
BorderLayoutDemo ls=new BorderLayoutDemo();
ls.setTitle("Treasure Hunter");
ls.setSize(400,400);
ls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ls.setLocationRelativeTo(null);
ls.setVisible(true);
}
}

public void FileReader(){
try {

String content = "This is the content to write into file";

File file = new File("highScores.txt");

// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

System.out.println("Done");

} catch (IOException e) {
e.printStackTrace();
}


}
public String FileSave(String filePath){
File file = new File(filePath);
StringBuffer sb = null;
if(file.exists()){
try {
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
sb = new StringBuffer();
String str =null;
while((str=br.readLine())!=null){
sb.append(str);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}
}
return sb.toString();//框架写好了~
}


public void HighScores(int sum){

ArrayList scores=new ArrayList();
scores.add(sum);
Collections.sort(scores);
Component highScore = null;
JOptionPane.showMessageDialog(highScore, "High Scores: "+scores.toString());
}


public static void main(String args[]) {

boolean playAgain=false;

JOptionPane.setDefaultLocale(Locale.ENGLISH);
JOptionPane.showMessageDialog(null, "Welcom to Treasure Hunter game! \nThe objective is to dig up as much treasure as possible before being caught in a trap. \nSimply click on a square to uncover it.", "Treasure Hunter",JOptionPane.INFORMATION_MESSAGE);


BorderLayoutDemo f = new BorderLayoutDemo();

f.setTitle("Treasure Hunter");

f.setSize(400,400); //让窗体大小

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null); //让窗体居中显示



}
}

结队编程

结对对象博客网址:http://www.cnblogs.com/cdcode/ 

实验楼git库:http://git.shiyanlou.com/cd88882

步骤

耗时

百分比

需求分析

20min 

 20%

设计

 50min

50% 

代码实现

10min 

10% 

测试

5min 

5% 

分析总结

10min 

 10%

 

posted @ 2015-06-05 13:49  20135305  阅读(350)  评论(0编辑  收藏  举报