Experimental content 1. XP Foundation 2. XP Core Practice 3. Related tools
Experimental steps (i) Agile Development and XP
Software engineering covers the following areas: Software requirements analysis, software design, software construction, software testing and software maintenance.
The name of the identifier should be intuitive and can be spelled, it is expected to understand the text, do not have to "decode", the general use of English words or their combination, easy to remember and read, avoid the use of Hanyu Pinyin to name, use the word to be accurate for example "current value" should be named currentValue
, written nowValue
is not accurate, but also dqz
make it (Dang Qian Zhi first letter) is a joke.
The principle of “min-length && max-information”
The length of the identifier, such as: maxVal
better than maxValueUntilOverflow
, can be by going to the vowel method to shorten the variable name, such as returnValue
rtnVal
, and, message
msg
general global variables with a descriptive name, Local variables with short names: single-character's name, common as i,j,k, etc. used as local variables.
(ii) Coding standards
An important understanding of writing code is that "most of the time the program is for people to see", the programming standard makes the code easier to read and understand, and can even guarantee fewer errors. Programming standards include: Descriptive names, clear expressions, straightforward control flow, readable code and annotations, and the importance of consistently using certain rules and idioms when pursuing these content.
The principle of “min-length && max-information”
The length of the identifier, such as: maxVal
better than maxValueUntilOverflow
, can be by going to the vowel method to shorten the variable name, such as returnValue
rtnVal
, and, message
msg
general global variables with a descriptive name, Local variables with short names: single-character's name, common as i,j,k, etc. used as local variables.
The general naming conventions in Java are:
- To embody their own meanings.
- Terms for packages, classes, variables
- Method name with movable bin
- Package name all lowercase, such as: IO,AWT
- The first letter of the class name should be capitalized, such as: Helloworldapp
- The first letter of a variable name is lowercase, such as: UserName
- The first letter of the method name is lowercase: setName
(iii) Pair programming
Voussy Yao Work: Add comment, test code, add picture
Zheng Wei: Write the main function, perfect each module
Pairs of students: 20135332
http://www.cnblogs.com/wuxiyao/
Puzzle Games
* Java Games-Jigsaw puzzles I made the first little game * The cell class is an Inherited button class, and with the corresponding graphics, forming a grid *mycanvas is a panel that loads the cell class object (grid), which is the core of these three classes *
*/
Import Java.awt.BorderLayout; Import Java.awt.Button; Import Java.awt.Choice; Import Java.awt.Color; Import Java.awt.Container; Import java.awt.event.ActionEvent; Import Java.awt.event.ActionListener;
Import Javax.swing.Icon; Import Javax.swing.ImageIcon; Import Javax.swing.JFrame; Import Javax.swing.JLabel; Import Javax.swing.JOptionPane; Import Javax.swing.JPanel;
public class Mymainframe extends JFrame implements ActionListener {MyCanvas MyCanvas; JPanel panelnorth,panelpreview;//define the top panel, and preview the desired panel button start,preview,set;//definition start, preview, set button Container container;//container, Get Content Panel
Public Mymainframe () {//initial container=this.getcontentpane (); Start=new button ("Start"); Start.addactionlistener (this); Preview=new button ("Preview"); Preview.addactionlistener (this); Set = New Button ("Set"); Set.addactionlistener (this); Panelpreview=new JPanel (); Panelpreview.setlayout (NULL); Icon icon=new ImageIcon ("Pictrue/pic_" +mycanvas.pictureid+ ". jpg"); JLabel label=new JLabel (icon); Label.setbounds (0,0,300,300); Panelpreview.add (label);
Panelnorth=new JPanel (); Panelnorth.setbackground (color.red); Panelnorth.add (start); Panelnorth.add (preview); Panelnorth.add (set); Mycanvas=new MyCanvas (); Container.add (Mycanvas,borderlayout.center); Container.add (Panelnorth,borderlayout.north); This.settitle ("Puzzle games-ming"); This.setlocation (300,200); This.setsize (308,365); This.setresizable (FALSE); This.setvisible (TRUE);
This.setdefaultcloseoperation (3); } public static void Main (string[] args) {//TODO auto-Generate Method Stub new Mymainframe ();
} public void actionperformed (ActionEvent arg0) {//For three button event handling//TODO Auto Generate Method Stub button button= (Button) Arg0.getsource (); if (Button==start) {Mycanvas.start ();
}else if (Button==preview) {if (Button.getlabel () = = "Preview") {container.remove (MyCanvas); Container.add (Panelpreview); Panelpreview.updateui (); Container.repaint ();
Button.setlabel ("return"); }else{Container.remove (Panelpreview); Container.add (MyCanvas); Container.repaint (); Button.setlabel ("Preview");}} else if (button==set) {//modifies the selected picture Choice pic = new Choice (); Pic.add ("Kitten"); Pic.add ("Piggy"); Pic.add ("The Cloud"); Pic.add ("QQ"); pic.a DD ("cartoon"); Pic.add ("Flower");
int I=joptionpane.showconfirmdialog (this,pic, "select Picture", joptionpane.ok_cancel_option); if (i==joptionpane.yes_option) {mycanvas.pictureid=pic.getselectedindex () +1; Mycanvas.reloadpictrue (); Icon icon=new ImageIcon ("Pictrue/pic_" +mycanvas.pictureid+ ". jpg"); JLabel label=new JLabel (icon); Label.setbounds (0,0,300,300); Panelpreview.removeall (); Panelpreview.add (label); Panelpreview.repaint (); } } }
}
--------------------------------------------------
Import Java.awt.Rectangle; Import java.awt.event.MouseEvent; Import Java.awt.event.MouseListener;
Import Javax.swing.Icon; Import Javax.swing.ImageIcon; Import Javax.swing.JOptionPane; Import Javax.swing.JPanel;
public class MyCanvas extends JPanel implements MouseListener {Boolean hasaddactionlistener=false;//sets the flag bit of the action listener for the square, True for the action event has been added, false is not yet added action event Cell cell[];//definition grid Rectangle cellnull;//define empty square area public static int pictureid=1;// The currently selected picture designator public MyCanvas () {this.setlayout (null); This.setsize (400,400); cellnull=new Rectangle (200,200,100,100);// The empty square area in the third row every three columns cell=new cell[9]; icon icon; for (int i = 0; i < 3; i++) {//For 9 squares load the picture, and initially make the coordinates, forming three rows three columns for (int j=0;j<3;j++) {icon=new ImageIcon ("Pictrue/pic_" +pictu Reid+ "_" + (i*3+j+1) + ". jpg"); Cell[i*3+j]=new Cell (icon); Cell[i*3+j].setlocation (j*100,i*100); This.add (Cell[i*3+j]); }} this.remove (Cell[8]);//Remove the last extra square}
public void Reloadpictrue () {//When selecting another graphic for the puzzle, reload the new Picture icon icon; for (int i = 0; i < 3; i++) {for (int j=0;j<3;j++) {i Con=new ImageIcon ("Pictrue/pic_" +pictureid+ "_" + (i*3+j+1) + ". jpg"); Cell[i*3+j].seticon (icon); }}} public boolean isfinish () {//Determines whether flattening succeeds for (int i=0;i<8;i++) {int x=cell[i].getbounds (). x; int Y=cell[i].getbounds ( ). Y; if (y/100*3+x/100!=i) return false; } return true; }
public void Start () {//re-arranges squares, disrupts order
while (Cell[0].getbounds (). X<=100&&cell[0].getbounds (). y<=100) {//when the first square is closer to the upper-left corner of the int x= Cellnull.getbounds (). x; int Y=cellnull.getbounds (). Y; int direction= (int) (Math.random ());//Generate 0-4, corresponding to the empty square up and down move if (direction==0) {//empty squares to move left, and the left square to swap positions, left square right to move x-=100; Test (x, y)) {for (int j=0;j<8;j++) {if (Cell[j].getbounds (). x==x) && (Cell[j].getbounds (). y==y)) {// Look for the left button Cell[j].move ("Right", 100); Cellnull.setlocation (x, y); break;//is found after jumping out for loop}}}}else if (direction==1) {//right x+=100; if (Test (x, y)) {for (int j=0;j<8;j++) {if (Cell[j]. GetBounds (). x==x) && (Cell[j].getbounds (). y==y) {Cell[j].move ("left"); Cellnull.setlocation (x, y); Break }}}}else if (direction==2) {//up y-=100; if (Test (x, y)) {for (int j=0;j<8;j++) {if (Cell[j].getbounds (). X==x) & & (Cell[j].getbounds () y==y) {cell[j].move ("down"); Cellnull.setlocation (x, y); break;}} }}else{//down y+=100, if (Test (x, y)) {for (int j=0;j<8;j++) {if (Cell[j].getbounds (). x==x) && (Cell[j]. GetboundS (). y==y) {cell[j].move ("up"), Cellnull.setlocation (x, y); } }
}
if (!hasaddactionlistener)//If an action event has not been added, add for (int i=0;i<8;i++)//Add an action event for the first square, so that clicking the button moves cell[i]. Addmouselistener (this); Hasaddactionlistener=true; } private Boolean test (int x,int y) {if (x>=0&&x<=200) | | | (y>=0&&y<=200)) return true; else return false; }//public void Paint (Graphics g) {////for (int i=0;i<=300;i+=100)//g.drawline (0, I, a, i);//for (int i=0;i<=300; i+=100)//g.drawline (i, 0, I, 300); for (int i=0;i<8;i++)//cell[i].repaint (); //}
public void mouseclicked (MouseEvent arg0) {} public void mouseentered (MouseEvent arg0) {} public void mouseexited (Mouse Event arg0) {} public void mousereleased (MouseEvent arg0) {} public void mousepressed (MouseEvent arg0) {//Grid mouse event because MYC is used Anvas Some methods in the cell class, so the mouse event is not handled in cell button= (cell) Arg0.getsource (); int X1=button.getbounds (). x;//gets the coordinates of the clicked Square int y1=button.getbounds (). Y;
int x2=cellnull.getbounds (). x;//gets the coordinates of the empty squares int y2=cellnull.getbounds (). Y;
if (x1==x2&&y1-y2==100)//Compare if the condition is met Button.move ("up", +); else if (x1==x2&&y1-y2==-100) Button.move ("Down", 100); else if (x1-x2==100&y1==y2) Button.move ("left", 100); else if (x1-x2==-100&&y1==y2) button.move ("right", 100); Else return;//does not perform any processing on the
Cellnull.setlocation (x1,y1); This.repaint (); if (This.isfinish ()) {//whether the completion of the decision Joptionpane.showmessagedialog (This, "Congratulations on completing the puzzle, come on!" "); for (int. i=0;i<8;i++) Cell[i].removemouselistener (this);//If you have finished, undo mouse events, mouse click squares are not in effect hasaddactionlistener=false; } }
}
------------------------------------------------------------------------------------------
Import Javax.swing.Icon; Import Javax.swing.JButton;
public class Cell extends JButton {cell (icon icon) {//is actually icon Super (icon); this.setsize (100,100);}
public void Move (String direction,int sleep) {////Square Move if (direction== "up") {This.setlocation (This.getbounds (). x, This.getbounds (). y-100); }else if (direction== "down") {this.setlocation (This.getbounds (). X,this.getbounds (). y+100);} else if (direction== "left") {This.setlocation (This.getbounds (). X-100,this.getbounds (). y);} else{this.setlocation (This.getbounds (). X+100,this.getbounds (). y);}}
}
(iv) version control
Note that the code submitted to the code base must compile, run, test no problem of code, we have no problem with the test code above, we can submit: we can first look at the git status
code status, display has not tracked code, and suggested with the git add <file>...
addition, we use git add HelloWorld.*
Add the information for the file to be submitted to the index library. When we use it git commit
, git submits the file according to the contents of the index library. This is only in the local operation, close the experimental environment, will delete the code, if you want to save the code to a remote hosting server, need to use git push
, before the experiment git push
is completed, do not forget to use , otherwise it is equivalent to you in Word editing a half-day file and finally did not save. We can modify HelloWorld.java
it as shown in the following:
Compile, run, and test without problems after committing, here is used git commit -a
:,
We can submit a record by looking at the git log
code:
(v) Reconstruction
Let's look at the concept of refactoring first:
Refactoring (refactor) is to change the structure within the software without changing the external behavior of the software, making it easier to read, easy to maintain, and easy to change.
One of the key prerequisites in refactoring is to "not change the external behavior of the software", which guarantees that we will not bring new bugs to the original system while reconstructing the original system, so as to ensure the security of the reconstruction. How to ensure that the software external behavior is not changed ? The reconstructed code should be able to pass the unit test . How can you make it easier to read, easier to maintain, and easy to change ? The design pattern gives the goal of refactoring.
Does refactoring matter? You can see from the Eclipse menu that there refactor
is a menu, and refactoring is almost standard for modern Ides:
We 编码标准
say that "naming identifiers" is an important skill for programmers, and there is no such consciousness before, what do you do now? No problem, the first function in refactoring is to Rename
change names to classes, packages, methods, and variables. For example this has a ABC
class:
This class, the class name, the method name, and the parameter name of the method are problematic, and the code cannot be understood without comments. We can use the refactoring feature in Eclipse to rename it. To modify the method, click the name you want to change with your mouse, and select the menu in Eclipse Refactor
Rename...
:
The effect of refactoring is as follows:
function unchanged, code level immediately on a grade, experience the name of the power it?
In the Eclipse menu Refactor
Encapsulate Field...
, such as:
Note the code changes before and after refactoring: You can also encapsulate id
and age
two member variables, and the results are as follows:
The 34th, 35 line is still problematic, every time the student information printed code violates the dry principle, resulting in code duplication, normal refactoring can be used in Eclipse Extract Method...
, such as:
Because all classes in Java have a dedicated tostring method, we use the eclipse-to- Source
Generate toString()...
class to Student
produce a toString
method, such as:
Modify the code of main and the result is as follows:
Let's think about it, what are the benefits of such a refactoring? What's wrong with refactoring?
We want to modify the software, original aim, is nothing more than four kinds of motives:
- Add new features;
- The original function has a bug;
- Improving the structure of the original procedures;
- Optimize the performance of legacy systems.
- The simplest
Duplicated Code
is that [the two methods within the same class contain the same expression]. All you need to do is use the Extract Method
extracted code, and then let the two locations invoke the extracted piece of code.
- Another common scenario is that [two siblings (sibling) subclasses contain the same expression]. To avoid this, use only two classes
Extract Method
, and then use the extracted code to Pull Up Method
push it into the superclass.
- If the code is just similar, not exactly the same, then you have to
Extract Method
split the similarity and Difference sections to form a single method. Then you may find that you might be able to apply Form Template Method
a Template Method
design pattern.
- If there are methods that do the same thing with different algorithms, you can choose one that is clearer and
Substitute Algorithm
replace the algorithm with other methods.
- If two unrelated classes are present
Duplicaded Code
, you should consider using one Extract Class
, refining the duplicate code into a separate class, and then using the new class within another class. However, the method of repeating the code may indeed belong to only one class, the other class can only invoke it, or the method may belong to the third class, and the other two classes should refer to the third class. You have to decide where this method fits best and make sure that it is placed and will not appear anywhere else.
Other Bad Smell
and corresponding refactoring techniques are shown in the following table:
The Refactor
time to apply refactoring techniques in the Eclipse menu is as follows:
A more complete approach can be found in the blog of Martin Fowler, the author of refactoring. The use of basic techniques in eclipse can be referred to by anyone who can refactor to learn and practice.
A complete refactoring process consists of:
- Check out code from the version control system library
- Read code (including test code)
- Find Bad smell
- Refactoring
- Run all the unit Tests
- Check in code to codebase
Let's combine git to give a more complete example.
(vi) Practice Item 1. A pair of programming to write a software, blog to give the knot to the classmate's blog URL, you can take pictures to show the pairing programming situation, you can refer to other school's work
Pairs of students: 20135322
http://www.cnblogs.com/zhengwei0712/
2. Record the TDD and refactoring process and test the code less than the business code, in eclipse
refactor
No less than 5 refactoring skills under the menu
3. Team code to use git in the lab building hosting, to use the pair of students in a classmate's account hosting. 4. The program should have GUI interface, reference user interface and user Experience 5. program function Choose a Java implementation from the Pea pod game, note:
team projects can not be duplicated, the class represents the coordination。
Java Third Experiment