201621123080 Java programming 10th Week of study Summary 1. This week's study summary 1.1 summarizes anomalies related content in the way you like (mind map or other).
2. Written work
This PTA job problem set anomaly
1. Common exceptions
Combined Topic 7-1 Answer
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?
Often encountered before ArrayIndexOutOfBoundsException
NullPointerException
ClassCastException
NumberFormatException
, as long as the inheritance RuntimeException
does not need to capture. Instead, it needs to try-catch
be captured.
1.2 What kind of exception requires the user to be sure to use capture processing?
Belong Checked Exception
to must capture
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?
The subject is essentially a string conversion int where an exception occurs, and the code that might appear to be the exception is try-catch
captured.
3. Throw and throws
Topic 7-3
Read the Integer.parsetInt
source code
3.1
Integer.parsetInt
What is the benefit of having a large number of code throwing exceptions at the outset?
Avoid the program from exiting because of Integer.parsetInt
an exception; there are many places where problems occur, and by throwing an exception you can also let the user know where an exception occurred and make it easier to modify.
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?
Tells the caller which side of the exception was thrown, for example begin>=end
, that begin<0
throws an end>arr.length
exception.
4. Improvement with exception
ArrayIntegerStack
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?
Avoid the direct exit of the program, throw an exception can prompt the user error information, and the cause of the error, in favor of modification, and simply return the error value can only know the program run error.
4.2 If a method inside the inner code throws is
RuntimeException
Type of exception, then the method declaration should use the
throws
keyword, if you use the
throws
Keyword declares the exception thrown by this method, what benefits can it bring to us?
Can be used or can not be used; throwing exceptions gives us more detailed information that is helpful for changing and finding errors.
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?
Capture the order of the catch
smallest subclasses first, and then step catch
to the parent class.
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?
The catch class cannot have an inheritance relationship;
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 and add the following functions. When the file cannot be found, you need to prompt the user not to find the file xxx, retype the file name, and then try to open it again. If it is another exception, prompt to open or read the file failed!.
Note 1: There are several methods inside that can throw an exception.
Feature 2: Need to add finally
close file. Regardless of whether the above code produces an exception, always be prompted to close the file ing. If closing the file fails, the prompt to close the file failed!
6.2 Combining the title set 6-2 code, what kind of action to put in the finally block? Why? What do I need to be aware of when closing resources using finally?
Close file operation in finnnally;
Program execution finally need to close the file;
Closing a file operation also needs to be executed with a try-catch
statement.
6.3 Use Java7 try-with-resources
to overwrite the above code to automatically close the resource. What are the benefits of this approach?
That is, parentheses are appended to the try to open the file try(FileInputStream fis = new FileInputStream("testfis.txt"))
, which automatically closes the file resource regardless of whether the exception is performed. Equivalent to finnaly
addingfinally{if(br!=null)br.close();}
7. Object-oriented design job-Library management system (group completion, no more than 3 students per group)
Log in to lib.jmu.edu.cn
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?
There are mainly users, administrators;
7.2 Main function modules (not too many) and the owner of each module. Next week everyone is going to submit their own module code and run the video.
function |
Object |
principal |
Registered |
User |
Liu Sihuan |
Login |
Users, administrators |
Liu Sihuan |
Borrowing and returning |
User |
Chenjianbo |
To add a book of omission |
Administrator |
Chenjianbo |
Search for books |
Users, administrators |
Zhu |
User borrowing information |
Users, administrators |
Zhu |
7.3 Main class design and class diagram of the system (available)
7.4 How you plan to store library information, address information, reader information, and more.
Save in a file with a dynamic array
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.
Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
1 |
0 |
0 |
0 |
0 |
2 |
0 |
0 |
0 |
0 |
3 |
472 |
472 |
11 |
11 |
4 |
472 |
0 |
11 |
0 |
5 |
468 |
-4 |
12 |
1 |
6 |
1043 |
575 |
30 |
18 |
7 |
2417 |
1374 |
51 |
21st |
8 |
5129 |
2712 |
83 |
32 |
9 |
5284 |
155 |
89 |
6 |
10 |
5714 |
430 |
98 |
9 |
11 |
5995 |
281 |
105 |
7 |
201621123080 Java programming 10th Week of study summary