201621123010 Java programming 10th Week of study summary

Source: Internet
Author: User

1. Study summary 1.1 This week summarize anomalies in the way you like (mind mapping 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?

A: 7-1 The main thing in this topic is to catch the exception generated by the runtime and the user's unsolicited exception;
Previously written code is prone to the following issues:

    1. ArrayIndexOutOfBoundsException
    2. NullPointerException
    3. ClassCastException
    4. NumberFormatException
      None of these exceptions need to be captured, because these exceptions are directly inherited from the Exception class and RunTimeException must be captured, such as directly inheriting from Exception the class.
      The important process to avoid these anomalies is to have a rigorous review of each condition when writing code, especially boundary values, to write code filtering on the types of parameters that may be entered, discard incoming data that does not meet the requirements, and inform the user of the type and format of the arguments passed in.
1.2 What kind of exception requires the user to be sure to use capture processing?

A: For exceptions that inherit directly from Exception the class, you must capture (for example) the code when you write RuntimeException it, or the compilation will not allow it to pass.

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?

A: The main thing to do is to use the exception handling input mechanism, so you want to pass data into the statement block of the array to use the try-catch statement, the possible incoming non-conforming data generated by the exception to capture.
In order to make the program more robust, it is necessary to catch the exception of the statements in the program with the statement try-catch of the exceptions, and also note that the incoming data of the statement block is also easy to pass the non-conforming data, and therefore often need to use the exception handling input mechanism.

3. Throw and throws

Topic 7-3
Read the Integer.parsetInt source code

3.1 Integer.parsetIntWhat is the benefit of having a large number of code throwing exceptions at the outset?


Source code, such as the first throw a large number of exception code, the object is Null thrown to the exception, and then the binary is not the minimum 二进制 and maximum input 三十六进制 data object thrown exception, the advantage is that the total non-conforming objects can be rejected first, Further screening of the remaining data is then made.

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?

A: Throwing an exception should let the user know exactly what the exception is, and the specific cause and location of the exception, rather than just telling the user that there is an exception, but not giving the specific problem.
So when writing a method, the module that throws the exception needs to indicate what kind of exception is generated and the specific cause and location of the exception.

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?

A: By using exception handling mechanism to throw an exception, you can let the user more clearly know the specific type of operation failure, cause and location information;
The simple return of NULL or error value is tantamount to telling the user that "you failed the operation, but I do not tell you the specific reason for failure", so that the program is relatively weak.

4.2 If the inner code of a method throws an exception of type RuntimeException, does the method declaration use the throws keyword, and if the exception that is thrown by the method is declared using the throws keyword, what benefit can it bring us?

A: If the inner code throws an exception of type RuntimeException, the method declaration does not use the throws keyword. Because Exception the exception that inherits the class directly must be captured.
If RuntimeException so, then the virtual opportunity to process after throwing an exception, without the programmer writing the try-catch statement, rather than RuntimeException , you need to capture and handle it at the time of writing.

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?

A: When a try block can throw multiple exceptions, and there may be an inheritance relationship between exceptions, if the parent class's capture statement before the subclass, because the code execution is executed sequentially, the parent class's capture statement is executed, and the subclass's exception inherits from the parent class, so when the parent class is captured, The exception is no longer able to continue to be captured by the quilt class, at which point the subclass capture statement is lost.
Thus, when capturing, it is important to note that the exception capture statement for the subclass is placed before the exception capture statement of 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?

A: If you can throw multiple exceptions in a try block, using Java8 's multiple exception capture syntax requires attention to adding a block of statements for each exception after the try block catch , and if there is an inheritance relationship between the exceptions, the exception captures for the subclasses need to be placed before the exception captures of the parent 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 and add the following functions. When the file is not found, you need to prompt the user 找不到文件xxx,请重新输入文件名, and then try to turn it back on. Prompt if it is a different exception 打开或读取文件失败!

Note 1: There are several methods inside that can throw an exception.
feature 2: Need to add finally close file. Always prompt, regardless of whether the above code produces an exception 关闭文件ing . If closing the file fails, prompt 关闭文件失败 !
when the file does not exist :

when the file exists :

6.2 Combined Problem Sets 6-2Code, what do you want to put in the finally block? Why? What do I need to be aware of when closing resources using finally?

A: try-catch after the statement block, if there are any statements that need to be executed anyway, you need to put in the finally statement, such as the previous step to close the file is to do anyway, in addition to other types of resource release usually also need to put into the finally statement block.
When you use finally to close a resource, you also need to use a statement because the statement that closes the resource may also produce an exception try-catch .

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

when the file does not exist :

when the file exists :

The syntax for initializing an object is called Try-with-resources after the try, and the Close () function of the resource is called automatically, so the programmer does not have to write the resource's closing release statement at the time of writing.

7. Object-oriented design job-Library management system (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?

A: The main should be two categories, one is the ordinary users , the second is the librarian system.

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.

Answer :

    • Registration and Login module , you need to distinguish the user type by the login module's account name.
    • For ordinary users are generally looking for books , borrowing books ;
    • For Administrators , the feature is up/down books .
7.3 Main class design and class diagram of the system (available)

A: For the use of objects, you can design a user class, attributes are common user and Administrator properties (account password, etc.), and the ordinary user class and the administrator class can inherit the user class, You then extend other properties, and you need a book class to record information about the book object .

    • The common user class should include the method of finding and borrowing ;
    • The administrator class should include the shelves/Bottom shelf book method;
    • The library should contain the information record method of the book ;
    • There are also login/registration methods
7.4 How you plan to store library information, address information, reader information, and more.

A: For simpler information, it is easier to use dynamic arrays to store information.
If the information is more complex, you need to use files to store the information of each module, of course, the operation of the file will be relatively complex.
Will try to use the file to solve, if the implementation is difficult to consider the choice of dynamic array to handle.

3. Code Cloud and PTA

Topic Set:异常

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.

2
Week Time Total code Amount New Code Volume total number of files number of new files
1 all 5 5
322 232 one 6
3 652 5
4 946 294 + 5
5 1347 401 5
6 1591 244 2
7 2118 527 3
8 2627 509 8
9 2912 285 3
Ten 3171 259 / 8

201621123010 Java programming 10th Week of study summary

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.