Third Blog Job

Source: Internet
Author: User

1, a website management system, the user registration, the phone number is optional input, the input format is: Area code-Telephone number-extension number, in the middle with "-" separated. The following is the design on the JSP page and does not have any control over the input.

Suppose the system now needs to take out the middle phone number part, the code is as follows:

/**     *      * This method takes the middle part of the phone number according to user input     * @param strphonenum  phone number, such as: "0591-83279988-002"     * @return  Returns the number part, such as: "83279988" */public    String getphonenumber (string strphonenum) {        if (strphonenum==null) | | ". Equals (Strphonenum)) {            return" ";        }        String[] Arrphone=strphonenum.split ("-");        return arrphone[1];    }

Use the techniques and methods you have learned to identify the problems in this approach, and analyze the cause of the problem, and give your solution. (Hint: there is more than one problem)

Requirements:

(1) Write the question

A: The length of the phone number is not limited, and there is no judge whether the phone number is a number

(2) Write down the reason why the problem arises

Answer: The input data is not defined in the code to control the corresponding processing

(3) Give the modified code

For:

/** * 3137102236
* @author Lam * 2016/4/30 * This method takes the middle phone number from the user input * @param strphonenum phone number, such as: "0591-83279988-002" * @retur N Returns the number part, for example: "83279988"*/ Publicstring Getphonenumber (String strphonenum) {//determine if the number of incoming phone numbers is made up of numbersBoolean result=strphonenum.matches ("[0-9]+"); if(Result = =false){ return "false"; }Else{ if((strphonenum==NULL) ||"". Equals (Strphonenum)) { return ""; } string[] Arrphone=strphonenum.split ("-"); //determine whether the length of the phone number is correct, the area code is not 4 digits, the phone number is not 8 digits, the extension number is not 3 digits will be an error. if(arrphone[0].length ()! =4){ return "false"; }Else if(arrphone[1].length ()! =8){ return "false"; }Else if(arrphone[2].length ()! =3){ return "false"; }Else{ returnarrphone[1]; } } }

2, write a paragraph for the integer array sorting code, explain your design ideas, and use the error-guessing method to give a possible error (at least 5), design the test case, and use JUnit to write unit tests to test. (assuming that the passed-in parameter has been determined to be an integer array)

Requirements:

(1) Write code, and conform to code specification (naming to specification, not directly written in the main method, need to have class comments, method comments, and appropriate line comments)

 Packagecom.lin.action;ImportJava.util.Scanner; Public classPaixu {/*** Bubble Sort * 2016/04/30 *@authorLin * Wang*/     Public voidMaopao () {/** Sorting implementation * Bubble Sort by manually entering numbers that need to be sorted*/System.out.print ("Please enter the number of digits you want to sort by M:"); Scanner SCA=NewScanner (system.in); intm=Sca.nextint (); inti,j,tmp; int[] A =New int[M]; System.out.println ("Please enter the number of M:"); //realization of the arrangement Order of integer array before sorting         for(i=0;i<a.length;i++) {A[i]=Sca.nextint (); } System.out.print ("Number before sorting:"); //The realization of the arrangement order of the integer array after being sorted         for(i=0;i<a.length;i++) {System.out.print (A[i]+"  ");         } System.out.println ();  for(i=0;i<m;i++)        {             for(j=m-1;j>i;j--)            {                if(a[j-1]>A[j]) {tmp=A[j]; A[J]=a[j-1]; A[j-1]=tmp; }}} System.out.print ("Number after sorting:");  for(i=0;i<a.length;i++) System.out.print (A[i]+"  ");    System.out.println (); }    //call to the Maopao () method     Public Static voidmain (String [] args) {Paixu Paixu=NewPaixu (); Paixu.     Maopao (); }}

(2) Explain your design ideas

A: 1. For bubble sort, first get the array, so you can freely define the number of numbers in the array to be arranged according to the scanner method.

2. A[i]=sca.nextint () The order in which the arrays are sorted before bubbling by entering the number sequence

3. The bubble sort is coded to get the order of the numbers in the bubble sorted array (the specific bubble sort method means that the first two numbers are compared to the larger numbers to the back, the smaller numbers go to the front, and then the numbers are compared until the maximum number is moved to the last face.) And then the second round of comparison to get the second-largest number to move to the next two positions, and so on, and then finally get the numbers from small to large order, bubble sort even completed. )

4. Calling the above method in main will enable the bubbling ordering of the array to be sorted.

(3) Write a possible error (at least five kinds)

Answer: 1. The array to be sorted is an empty array

2. The number in the array to be sorted is the same.

3. The number in the sorted array is only one

4. The numbers in the sorted array are arranged in order from small to large

5. There are some numbers in the array of sorts that are the same size

6. The numbers in the sorted array are arranged in order from large to small

7. The number of array inputs to sort is greater than the number of arrays you have defined

(4) Write JUnit unit tests to put the test data that was previously given in the possible error into the unit test for testing

Answer: 1. The array to be sorted is an empty array

2. The number in the array to be sorted is the same.

3. The number in the sorted array is only one

4. The numbers in the sorted array are arranged in order from small to large

5. There are some numbers in the array of sorts that are the same size

6. The numbers in the sorted array are arranged in order from large to small

7. The number of array inputs to sort is greater than the number of arrays you have defined

(The test finds only the number of qualified digits previously entered to bubble sort)

Third Blog Job

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.