Java Lab report (experiment three)

Source: Internet
Author: User
Tags coding standards delete key event listener getcolor gety version control system

Beijing Institute of Electronic Technology

Real Inspection report

Course: Mobile Platform Application development Practice class: 201592 Name: Souzenan No.: 20159207

Score: Instructor: Lou Jia Peng Experimental Date: 2015.10.15

Experiment level: Preview degree: Experiment time:

Instrument Group: Compulsory/Elective: Elective experiment number: 3

Experiment Name: Agile Development and XP practice

Experimental content

1. XP Basics

2. XP Core Practice

3. Related Tools

Experimental steps

(i) Agile Development and XP

Software engineering is the process of applying the systematic, orderly and quantifiable methods to the development, operation and maintenance of the softwares. Software engineering covers the following areas: Software requirements analysis, software design, software construction, software testing and software maintenance. People have many technologies, practices, habits and ideologies in the process of developing, operating, and maintaining software. Software engineering unifies these related technologies and processes into one system, called the "development process". The purpose of the software development process is to improve the efficiency of software development, operation and maintenance, and improve the quality of software, customer satisfaction, reliability and maintainability of software. It is not enough to have ideas of all kinds of processes, and we have a series of tools to ensure that these ideas can operate efficiently in practice. The important point of software development is not to see how many theories you can make, but also how you apply them to related tools, such as the combination of unit tests in Java and JUnit applications, and modeling to be combined with Umbrello or staruml applications. Programming learning is a process that is learned. A common formula is: Software engineering = development process + Tools Xin Zou Two formulas given by the teacher: SW = program + Software engineering and SW Enterprise = Software + business model development process you can refer to the software team and the development process of learning Xin Zou teacher. The common development processes are:

    • RUP (Rational Unified Process)
    • PSP (Personal software Process)
    • TSP (Team software Process)
    • Agile Process
    • ......

Agile Development (development) is a human-centric, iterative, and progressive approach to development. "Agile Processes" are a collection of values and methodologies. Since 2001, some experts in the software industry have started advocating "agile" values and processes that affirm the value of popular practices, but emphasize that agile practices can bring value.
Among them, extreme programming (EXtreme PROGRAMMING,XP) is
is a new and fast method of software development. The XP team uses on-site customers, special planning methodologies, and ongoing testing to provide rapid feedback and comprehensive communication:

    • XP is a methodology developed to target software that meets customer needs
    • XP is a practice-based software engineering process and thought
    • XP believes that code quality is more important than people generally think.
    • XP is ideal for small, responsible, self-motivated teams to develop software that requires uncertainty or rapid change

XP software development is what is expressed through the XP guidelines:

    • Communication: XP believes that communication between project members is key to the success of the project and regards communication as the main driver of coordination and cooperation in the project.
    • Simple: XP assumes that the future cannot be reliably predicted, and it is economically unwise to consider it now, so you should not think too much about the future but should focus on the urgent needs.
    • Feedback: XP considers the system itself and its code to be a reliable basis for reporting on the progress and status of the system development. The feedback of system development status can be used as a means to determine the progress of system development and determine the next development direction of the system.
    • Courage: It represents XP's view that people are one of the most important aspects of software development. In the development of a software product human participation throughout its entire life cycle, is the courage to eliminate the dilemma, let the team to the local optimal left behind, to achieve more significant goals. Demonstrates XP's basic trust in "people make projects successful".

The basis for successful use of a practice in an XP environment is presented through the rules of XP, including: Quick feedback, hypothesis simplicity, incremental changes, advocating for change, and quality of work.

XP software development is the cornerstone of XP activities, including: coding, testing, listening, design.

(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 layout in the coding standard is a good example, although the layout does not affect the function of the program, but it can affect the readability. The process of the layout of the pursuit of clear, beautiful, is a procedural style important factors.

III) pair programming

Pairing programming is an important practice in XP. In pair programming mode, a pair of programmers work side-by-shoulder, equally, and in complementary way. They sit in front of a computer, facing the same monitor, working with the same keyboard and the same mouse. They analyze together, design together, write test cases together, encode together, do unit tests together, do integration testing together, write documents together, and so on. There are two roles in pairing programming:

    • The driver (Driver) is the person who controls the keyboard input.
    • The Navigator (Navigator) plays the role of pilotage and reminding.

How to pair programming, why to pair programming, we refer to pair programming and two people cooperation, the focus is:

    • Drivers: Write design documents, conduct coding and unit tests, and other XP development processes.
    • Navigator: Review the driver's documentation, the driver's implementation of the development process such as coding, consider the coverage of unit tests, think about the need for and how to refactor, and help drivers solve specific technical problems.
    • Drivers and pilots constantly rotate their roles and do not work for more than an hour, and take 15 minutes off each hour of work. The navigator has to control the time.
    • Active participation. Any task is the responsibility of the two people, and the responsibility of all. There is no "My Code", "Your Code" or "his/her code", only "our code".
    • There are only horizontal gaps, and there is no level difference. They are pairs, and although they may have different levels of seniority, both sides have equal decision-making rights in terms of analysis, design, or coding.

Teamwork is a spirit that is emphasized in many places, and the smallest team is a one-to-two team, and the team spirit starts with pairing programming. The most important thing for people to get along with in social life is honesty, empathy and mutual benefit. There will be differences in pairing programming, how to cooperate more effectively to do things wrong, to master these can be a lifelong benefit.

(iv) version control

The collective ownership of XP means that everyone is responsible for all the code, which in turn means that everyone can change any part of the code. Pair programming contributes a lot to this practice: by working in different pairs, all programmers can see the full code. One of the main advantages of collective ownership is to increase the speed of the development process, because any programmer can fix it once there is an error in the code. This means that the code is placed in a place where everyone can easily get it, and we call it a code repository. This leads to another topic called versioning (version control).

Versioning provides a number of benefits for both teams and individuals.

    • Versioning provides item-level undo (undo) functionality: Nothing is a finalized version, and any error must be easily rolled back. Let's say you're using the world's most complex word processing system. It has all the features you can think of, that is, the DELETE key is not supported. Imagine how cautious and slow you are when typing, especially when a large document is approaching the end of the day, a careless will start again (imagine you select all the text, accidentally pressed the DELETE key, because there is no undo function, had to re-enter). Edit text and versioning are the same, and anytime you need to roll back, whether it's one hours, a day, or a week, it makes your team work free and fast, and is confident about correcting bugs.
    • Versioning allows multiple people to work on the same code, as long as they follow certain control principles. There is no longer a case where one person overwrites another person's edited code, causing the person's modification to be invalid.
    • The version control system preserves the history of changes made in the past. If you encounter some surprising code, the version control system can easily find out who did it, what was changed, how long it was modified, and if so, why.
    • The version control system also supports the release of multiple software versions while developing on the mainline. There is no need for the entire team to stop working when the software is released, and there is no need to freeze the code.
    • Version control is also a project-level time machine, you can choose any time to accurately view the project at the time of the situation. This is useful for research and is the basis for reproducing a previously problematic release

(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.

(vi) The experimental process:

Source->format in the 1,eclipse menu or ctrl+shift+f with shortcut keys can be indented as specified by Eclipse specification

2. Refactoring

A concrete example of refactoring:

3. Pair programming

Game Name: Play Turn Gobang

Teammate: 20159215 Tian Rengui

Blog: http://www.cnblogs.com/20159215trg/

My main tasks are: Writing pseudocode, perfecting Code, adding annotations, TDD testing

/*
Gobang main frame, program start
*/
public class Startchessjframe extends JFrame {

Private chessboard chessboard;
Private JPanel toolbar;
Private JButton Startbutton, Backbutton, Exitbutton;

Private JMenuBar MenuBar;
Private JMenu Sysmenu;
Private JMenuItem Startmenuitem, Exitmenuitem, Backmenuitem;

Re-start, exit, and Undo menu items
Public Startchessjframe () {
Settitle ("Stand-alone version Gobang");//Set Caption
Chessboard = new chessboard ();
Container ContentPane = Getcontentpane ();
Contentpane.add (chessboard);
Chessboard.setopaque (TRUE);

//Create and add menu
MenuBar = new JMenuBar ();//Initialize menu bar
Sysmenu = new JMenu ("system");//Initialize menu
//Initialize menu item
Startmenuitem = New JMenuItem ("re-start");
Exitmenuitem = new JMenuItem ("Exit");
Backmenuitem = new JMenuItem ("undo");
//Add three menu items to the Menus
Sysmenu.add (startmenuitem);
Sysmenu.add (Exitmenuitem);
Sysmenu.add (Backmenuitem);
//Initialize button event listener inner class
Myitemlistener lis = new Myitemlistener ();
Register three menus on the event listener
This.startMenuItem.addActionListener (LIS);
Backmenuitem.addactionlistener (LIS);
Exitmenuitem.addactionlistener (LIS);
Menubar.add (Sysmenu);//Add the System menu to the menu bar
Setjmenubar (menuBar);//Set MenuBar as the menu bar

toolbar = new JPanel ();//Tool panel instantiation
Three button initialization
Startbutton = new JButton ("re-start");
Exitbutton = new JButton ("Exit");
Backbutton = new JButton ("undo");
Layout the tool panel buttons with FlowLayout
Toolbar.setlayout (New FlowLayout (Flowlayout.left));
Add three buttons to a tool panel
Toolbar.add (Startbutton);
Toolbar.add (Exitbutton);
Toolbar.add (Backbutton);
Registering three buttons to listen for an event
Startbutton.addactionlistener (LIS);
Exitbutton.addactionlistener (LIS);
Backbutton.addactionlistener (LIS);
Layout The tool panel below the interface
Add (toolbar, Borderlayout.south);
Add (chessboard);//Adds a Panel object to the form
Setting the interface Shutdown event
Setdefaultcloseoperation (Jframe.exit_on_close);
SetSize (800,800);
Pack ();//Adaptive size

}

Private class Myitemlistener implements ActionListener {
public void actionperformed (ActionEvent e) {
Object obj = E.getsource ();//Get Event Source
if (obj = = StartChessJFrame.this.startMenuItem
|| obj = = Startbutton) {
Start again
Jfiveframe.this internal classes referencing external classes
System.out.println ("re-start");
Chessboard.restartgame ();
} else if (obj = = Exitmenuitem | | obj = = Exitbutton)
System.exit (0);
else if (obj = = Backmenuitem | | obj = = Backbutton) {
System.out.println ("Undo ...");
Chessboard.goback ();
}
}
}

public static void Main (string[] args) {
Startchessjframe f = new Startchessjframe ();//Create main frame
F.setvisible (TRUE);//Display main frame

}
}

/**
* Pawn Class
*/
public class Point {
private int x;//x index in the checkerboard
The y index in the private int y;//checkerboard
Private color color;//Color
public static final int DIAMETER = 30;//diameter

public point (int x, int y, color color) {
this.x = x;
This.y = y;
This.color = color;
}

public int GetX () {//Get the index of X in the board
return x;
}

public int GetY () {
return y;
}

Public color GetColor () {//Get the color of the pawn
return color;
}
}

/**
* Gobang--board class
*/

public class Chessboard extends JPanel implements MouseListener {
public static final int margin = 30;//margin
public static final int grid_span = 35;//grid spacing
public static final int rows = 15;//Checkerboard Row number
public static final int COLS = 15;//number of checkerboard columns

point[] chesslist = new point[(ROWS + 1) * (COLS + 1)];//initial each array element is NULL
Boolean isblack = true;//The default start is black chess first
Boolean Gameover = false;//whether the game is over
int chesscount;//The current number of checkers
int Xindex, yindex;//the current index of the pawn

Image img;
Image Shadows;
Color colortemp;

Public chessboard () {

SetBackground (Color.orange);//Set the background color to orange
img = Toolkit.getdefaulttoolkit (). GetImage ("board.jpg");
Shadows = Toolkit.getdefaulttoolkit (). GetImage ("shadows.jpg");
Addmouselistener (this);
Addmousemotionlistener (New Mousemotionlistener () {
public void mousedragged (MouseEvent e) {

}

public void mousemoved (MouseEvent e) {
Margin grid spacing
int x1 = (E.getx ()-MARGIN + GRID_SPAN/2)/Grid_span;

Turn the coordinate position of the mouse click into the grid index
int y1 = (e.gety ()-MARGIN + GRID_SPAN/2)/Grid_span;

The game is over.
You can't fall outside the chessboard.
X, y position already has a pawn exists, cannot under
if (X1 < 0 | | x1 > ROWS | | Y1 < 0 | | y1 > COLS | | gameover
|| Findchess (x1, y1))
SetCursor (New Cursor (cursor.default_cursor));
Set as default state
Else
SetCursor (New Cursor (cursor.hand_cursor));

}
});
}

Draw
public void Paintcomponent (Graphics g) {

Super.paintcomponent (g);//Draw Board

int imgwidth = Img.getwidth (this);
int imgheight = Img.getheight (this);//Get the width and height of the picture
int fwidth = GetWidth ();
int fheight = GetHeight ();//Get the width and height of the window
int x = (fwidth-imgwidth)/2;
int y = (fheight-imgheight)/2;
G.drawimage (IMG, x, y, null);

for (int i = 0; I <= ROWS; i++) {//Draw horizontal lines
G.drawline (margin, margin + i * grid_span, margin + COLS
* Grid_span, MARGIN + i * grid_span);
}
for (int i = 0; I <= COLS; i++) {//Draw vertical Line
G.drawline (margin + i * grid_span, margin, margin + i * grid_span,
MARGIN + ROWS * grid_span);

}

Draw Pieces
for (int i = 0; i < Chesscount; i++) {
Mesh intersection point x, y coordinates
int xPos = chesslist[i].gety () * Grid_span + MARGIN;
int yPos = CHESSLIST[I].GETX () * Grid_span + MARGIN;
G.setcolor (Chesslist[i].getcolor ());//Set color
G.filloval (XPOS-POINT.DIAMETER/2, YPOS-POINT.DIAMETER/2,
Point.diameter, Point.diameter);
G.drawimage (Shadows, XPOS-POINT.DIAMETER/2,
YPOS-POINT.DIAMETER/2, Point.diameter, point.diameter, NULL);
Colortemp = Chesslist[i].getcolor ();
if (colortemp = = Color.Black) {
Radialgradientpaint paint = new Radialgradientpaint (xPos
-POINT.DIAMETER/2 +, YPOS-POINT.DIAMETER/2
+ Ten, new float[] {0f, 1f}, new color[] {
Color.White, color.black});
((GRAPHICS2D) g). Setpaint (paint);
((GRAPHICS2D) g). Setrenderinghint (
Renderinghints.key_antialiasing,
RENDERINGHINTS.VALUE_ANTIALIAS_ON);
((GRAPHICS2D) g). Setrenderinghint (
Renderinghints.key_alpha_interpolation,
Renderinghints.value_alpha_interpolation_default);

} else if (colortemp = = Color.White) {
Radialgradientpaint paint = new Radialgradientpaint (xPos
-POINT.DIAMETER/2 +, YPOS-POINT.DIAMETER/2
+, +, new float[] {0f, 1f}, new color[] {
Color.White, color.black});
((GRAPHICS2D) g). Setpaint (paint);
((GRAPHICS2D) g). Setrenderinghint (
Renderinghints.key_antialiasing,
RENDERINGHINTS.VALUE_ANTIALIAS_ON);
((GRAPHICS2D) g). Setrenderinghint (
Renderinghints.key_alpha_interpolation,
Renderinghints.value_alpha_interpolation_default);

}

Ellipse2d e = new Ellipse2d.float (XPOS-POINT.DIAMETER/2, YPos
-POINT.DIAMETER/2, 34, 35);
((GRAPHICS2D) g). Fill (e);
A red rectangular box that marks the last piece

if (i = = chessCount-1) {//If it is the last pawn
G.setcolor (color.red);
G.drawrect (XPOS-POINT.DIAMETER/2,
YPOS-POINT.DIAMETER/2, 34, 35);
}
}
}

public void mousepressed (MouseEvent e) {//mouse is called when pressed on component

At the end of the game, you can no longer
if (Gameover)
Return

String colorname = isblack? "Black Chess": "White Chess";

Converts the coordinate position of the mouse click into a grid index
Xindex = (E.getx ()-MARGIN + GRID_SPAN/2)/Grid_span;
Yindex = (e.gety ()-MARGIN + GRID_SPAN/2)/Grid_span;

You can't fall outside the chessboard.
if (Xindex < 0 | | xindex > ROWS | | Yindex < 0 | | yindex > COLS)
Return

If the X, Y position already has a pawn, it cannot be
if (Findchess (Xindex, Yindex))
Return

Can be processed at the time
Point ch = new Point (Xindex, Yindex, Isblack?). Color.Black
: Color.White);
chesslist[chesscount++] = ch;
Repaint ();//Notification system redraw

If the winner gives a message and cannot continue playing chess

if (Iswin ()) {
String msg = String.Format ("Congratulations,%s won!") ", colorname);
Joptionpane.showmessagedialog (this, msg);
Gameover = true;
}
Isblack =!isblack;
}

Methods of covering MouseListener
public void mouseclicked (MouseEvent e) {
Called when the mouse button is clicked on the component
}

public void mouseentered (MouseEvent e) {
Called when the mouse enters the component
}

public void mouseexited (MouseEvent e) {
Called when the mouse leaves the component
}

public void mousereleased (MouseEvent e) {
Called when the mouse button is disposed on the component
}

Find if there is a piece of x, y index in the pawn array
Private Boolean findchess (int x, int y) {
for (point C:chesslist) {
if (c! = null && c.getx () = = x && c.gety () = = y)
return true;
}
return false;
}

Private Boolean Iswin () {
int continuecount = number of 1;//consecutive pieces

Horizontal Westward Search
for (int x = xIndex-1; x >= 0; x--) {
Color C = isblack? Color.black:Color.white;
if (getchess (x, Yindex, c)! = null) {
continuecount++;
} else
Break
}
Looking east
for (int x = Xindex + 1; x <= COLS; × x + +) {
Color C = isblack? Color.black:Color.white;
if (getchess (x, Yindex, c)! = null) {
continuecount++;
} else
Break
}
if (Continuecount >= 5) {
return true;
} else
Continuecount = 1;

Continue another search portrait
Search up
for (int y = yIndex-1; y >= 0; y--) {
Color C = isblack? Color.black:Color.white;
if (getchess (Xindex, y, c)! = null) {
continuecount++;
} else
Break
}
Portrait Down Search
for (int y = yindex + 1; y <= ROWS; y++) {
Color C = isblack? Color.black:Color.white;
if (getchess (Xindex, y, c)! = NULL)
continuecount++;
Else
Break

}
if (Continuecount >= 5)
return true;
else
Continuecount = 1;
To continue the search for another situation: oblique
//Tohoku Search
for (int x = Xindex + 1, y = yIndex-1; y >= 0 && x <= COLS, x + +, y--) {
Color C = isblack? Color.black:Color.white;
if (getchess (x, y, c)! = null) {
continuecount++;
} else
break;
}
//Southwest Search
for (int x = xIndex-1, y = yindex + 1; x >= 0 && y <= ROWS; x--, y++) {
Color c = Isblac K? Color.black:Color.white;
if (getchess (x, y, c)! = null) {
continuecount++;
} else
break;
}
if (continuecount >= 5)
return true;
else
Continuecount = 1;

To continue a search for another situation: oblique
Northwest Search
for (int x = xIndex-1, y = yIndex-1; x >= 0 && y >= 0; x--, y--) {
Color C = isblack? Color.black:Color.white;
if (getchess (x, y, c)! = NULL)
continuecount++;
Else
Break
}
Southeast Search
for (int x = Xindex + 1, y = yindex + 1; x <= COLS && y <= ROWS; x + +, y++) {
Color C = isblack? Color.black:Color.white;
if (getchess (x, y, c)! = NULL)
continuecount++;
Else
Break
}
if (Continuecount >= 5)
return true;
Else
Continuecount = 1;

return false;
}

Private point getchess (int xindex, int yindex, color color) {
for (point P:chesslist) {
if (P! = null && p.getx () = = Xindex && p.gety () = = Yindex
&& P.getcolor () = = color)
return p;
}
return null;
}

public void Restartgame () {
Clear pieces
for (int i = 0; i < chesslist.length; i++) {
Chesslist[i] = null;
}
Restore game-related variable values
Isblack = true;
Gameover = false; Whether the game is over
Chesscount = 0; Current number of Checkers
Repaint ();
}

Undo
public void GoBack () {
if (Chesscount = = 0)
Return
CHESSLIST[CHESSCOUNT-1] = null;
chesscount--;
if (Chesscount > 0) {
Xindex = Chesslist[chesscount-1].getx ();
Yindex = Chesslist[chesscount-1].gety ();
}
Isblack =!isblack;
Repaint ();
}

Rectangular dimension

Public Dimension getPreferredSize () {
return new Dimension (margin * 2 + grid_span * COLS, margin * 2
+ Grid_span * ROWS);
}

}

Experimental time

Steps

Take

Percentage

Demand analysis

50min

27%

Design

40min

21.6%

Code implementation

50min

27%

Test

45min

24.3%

Analysis Summary

40min

21.6%

Experimental summary

Through this experiment I found that my lack of programming ability, mainly the basic knowledge is weak, in the writing Studenttest class, do not use the ToString () method, I seriously study the ToString () method, or not resolved, Finally changed a way of thinking the code again after the discovery of the original is a "}" problem, make static display error, my ability to check the wrong to improve, many errors are very simple but difficult to find, in the future study to focus on improving the ability.

Java Lab report (experiment three)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.