201621123030 Java programming 10th Week of study summary

Source: Internet
Author: User

1. Summary of this week's study

1.1 Summarize abnormal content in the way you like (mind map or other).

2. Written work

This PTA work problem set异常

1. Common exceptions

7-1 answer with topic set

1.1 What exceptions do you often have in code that you have written before, and what do you need to capture (why)? What should be avoided?

Nullpointerexceptin is a common anomaly that will only go wrong when the program is running, it belongs to unchecked Exception, it is not captured. When programming with the idea of NULL, using an IF statement can reduce such problems

1.2 What kind of exception requires the user to be sure to use capture processing?

Except RuntimeException exception must use capture processing, is checked Exception

2. Handle exceptions to make your program more robust

Topic 7-2

2.1 Experimental summary. And answer: How can you make your program more robust?

Experimental Summary: to consider entering non-numeric array subscript position

Integer.parseint (String) allows the try-catch program to be more robust by allowing input diversity and using exception handling input mechanisms.

3. Throw and throws

Topic 7-3
Read the Integer.parsetInt source code

3.1 Integer.parsetInt There was a lot of code thrown out of the exception at the outset, what's the benefit of this practice?

Source

 PublicStaticIntparseint(String s)Throws NumberFormatException {Return parseint (S,10); }PublicStaticIntparseint(String S,int radix)Throws NumberFormatException {if (s = =NULL) {ThrowNew NumberFormatException ("null"); }if (Radix < Character.min_radix) {ThrowNew NumberFormatException ("Radix" + Radix +"Less than Character.min_radix"); }if (Radix > Character.max_radix) {ThrowNew NumberFormatException ("Radix" + Radix +"Greater than Character.max_radix"); }int result =0;Boolean negative =Falseint i =0, Len = s.length ();int limit =-integer.max_value;int multmin;int digit;if (Len >0) {Char Firstchar = S.charat (0);if (Firstchar <' 0 ') {Possible leading "+" or "-"if (Firstchar = ='-') {negative =True Limit = Integer.min_value; }Elseif (Firstchar! =' + ')Throw numberformatexception.forinputstring (s);if (len = =1)Cannot has lone "+" or "-"Throw numberformatexception.forinputstring (s); i++; } multmin = Limit/radix;While (i < len) { //accumulating negatively avoids surprises near max_value digit = Character.digit (S.charat (i + +), radix); if (digit < 0) { throw numberformatexception.forinputstring (s);} if (Result < Multmin) { throw numberformatexception.forinputstring (s);} result *= radix; if (Result < limit + digit) { throw numberformatexception.forinputstring (s);} result = digit;}} else { throw numberformatexception.forinputstring (s);} return negative? Result:-result;}           

The Advantage: The NumberFormatException exception is thrown, the user can know the cause of the exception, such as

    1. The first character is not a number or ' + ' or '-'
    2. Exceeding Extremum

3.2 What information do you usually need to pass to the caller when you write your own program with 3.1 and analyze your own method of throwing an exception?

The problem is the illegalargumentexception exception, the reason to give the caller information is that the begin is less than end and not less than 0

4. Improve Arrayintegerstack with exception

Topic 6-3

4.1 In combination with 6-3 code, how does it benefit to use an exception-throwing method to represent a program run-time error? What is the advantage of returning an error value more than a simple one?

Throw an exception when the way to run if an error throws an exception, the program stops, easy to fix the code, if you just return the error value, there is no way to directly find the point of error, so the method of throwing an exception can be more convenient to modify the code

4.2 When should I use throw keywords?

Use throw when you have a definite point to throw the anomaly and a type.

4.3 If the inner code of a method throws an exception of type RuntimeException, is the method declaration supposed to use the keyword throws , and if the throws exception that is thrown by the method is declared with the keyword, what is the benefit to us?

Using the throws keyword to declare exceptions thrown in this way can stop the program from running directly

5. Function questions-capture of multiple exceptions

Topic 6-1

5.1 Combined with 6-1 code, answer: If you can throw multiple exceptions in a try block, and there may be an inheritance relationship between exceptions, what do you need to be aware of when capturing?

Note that the subclass exception for exception cannot be caught after exception

5.2 If you can throw multiple exceptions in a try block, what do you need to be aware of when using Java8 's multiple exception capture syntax?

Notice that the subclass exception must catch before any of its parent classes, or there is no way to catch the child class

6. Add exception handling to the following code
byte[] content = null;FileInputStream fis = new FileInputStream("testfis.txt");int bytesAvailabe = fis.available();//获得该文件可用的字节数if(bytesAvailabe>0){content = new byte[bytesAvailabe];//创建可容纳文件大小的数组fis.read(content);//将文件内容读入数组}System.out.println(Arrays.toString(content));//打印数组内容

6.1 Correct the code so that it can function properly. Note 1: There are several methods inside that can throw an exception. NOTE 2: to use finally to close the resource.

Correction Code:

              

6.2 Ending handling exceptions and using finally close resources what do you need to be aware of?

Finally is whether the pipe is captured will execute, finally the execution of the statement will also be an exception, so finally internal also try-catch

6.3 Use try-with-resources in Java7 to rewrite the above code to automatically close the resource. What are the benefits of this approach?

7. Object-oriented design job (group completion, no more than 3 students per group)

Log in to lib.jmu.edu.cnto search for books. Then log in to the library information system to view my library . If you want to implement a book borrowing system, try using object-oriented modeling.

7.1 Who is the user of this system?

Student, teacher, administrator

7.2 Main function modules (not too many)

Student/Faculty

    1. Login
    2. Find
    3. Borrowing
    4. Return
    5. Check the status of books
    6. Log out

Administrator

    1. Inquire
    2. Manage book Status
    3. Adjust book location

7.3 Main class design and class diagram of the system (available)

Not finished yet, only a small part of it.

7.4 How you plan to store library information, address information, reader information, etc.

Files should be used to store information about various books and user information.

3. Code Cloud and PTA

Topic Set: Exceptions

3.1. Code Cloud codes Submission record
    • In the Code cloud Project, select statistics-commits history-set time period, and then search for and

3.2 PTA Problem set complete situation diagram

Two graphs are required (1. Ranking chart. 2.PTA Submission list diagram)

3.3 Count the amount of code completed this week

The weekly code statistics need to be fused into a single table.

weeks Total code amount new Code volume Total Files number of new files added
2 + 5 5
3 652 322 all 6
4 1047 442 5
6 1978 425 5
7 2643 506 6
8 3341 798 all 8
9 4003 662 to
ten 4556 553 ten one

201621123030 Java programming 10th Week of study summary

Related Article

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.