201621123006 Java programming 10th Week of study summary

Source: Internet
Author: User
Tags finally block

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 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?

    • Previously encountered array out-of-bounds exceptions, null pointers, input numeric types do not match the definition type.
    • None of these exceptions need to be captured. Because they all inherit from RuntimeException , and the RuntimeException class and its subclasses are run-time exceptions, they do not need to be captured.

      A run-time exception is characterized by the Java compiler not checking it, that is, when such an exception occurs in a program, the program can pass cheaply even if no try......catch statement is captured or thrown using the throws keyword declaration.

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

    • Compile-time exceptions require that the user be sure to use capture processing. That is, Exception in the class, except for RuntimeException the exception of the class and its subclasses, the others need to be captured.

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?

    • This problem requires an array of type int, and the user's input may be a non-integer string, so use the Try......catch statement to catch and handle the above program segments unexpectedly.
    • In the process of programming to read carefully, consider the problem to be comprehensive, coupled with the capture and handling of the exception of the statement can make the program more robust.

Throw and throws

Topic 7-3
Read Integer.parsetint source code

    • The source code is as follows:

public static int parseint (String s, int radix)
Throws NumberFormatException
{
/
Warning:this method may be invoked early during VM initialization
* Before Integercache is initialized. Care must is taken to don't use
* The ValueOf method.
/
if (s = = null) {
throw new NumberFormatException ("null");
}
if (Radix < Character.min_radix) {
throw new NumberFormatException ("radix" + Radix +
"Less than Character.min_radix");
}
if (Radix > Character.max_radix) {
throw new NumberFormatException ("radix" + Radix +
"Greater than Character.max_radix");
}
int result = 0;
Boolean negative = false;
int 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;
} else if (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;
}

3.1 Integer.parsetint There is a lot of code thrown out of the exception at the outset, what is the benefit of this practice?

    • The user may enter the format and the situation is very comprehensive, can be a variety of exceptions to capture and throw, let the user know why the exception.

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?

    • Exception type: illegalargumentexception
    • Cause of exception: Begin should be less than end, begin should be greater than 0, end should be greater than 0 and less than a.length

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?

    • We can directly know what is wrong. If you simply return the wrong value, the information is too small to judge where the error is.

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?

    • Can not be used, as RuntimeException belonging Uncheck Exception , can not be captured.
    • Advantage: With the throws keyword we can then write the Try......catch statement, so the program is more concise.

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 subclass exception should be caught after its parent class 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?

    • There are multiple catch statements after the try statement, such as 6-1 catch(NumberFormatException e) , catch(IllegalArgumentException e)
      catch(Exception e), and the order cannot be chaotic, the subclass exception must precede its parent class.

Add exception handling to the following code

byte[] content = null;
FileInputStream fis = new FileInputStream ("Testfis.txt");
int bytesavailabe = fis.available ();//Gets the number of bytes available for the file
if (bytesavailabe>0) {
Content = new byte[bytesavailabe];//Create an array that can hold the file size
Fis.read (content);//Read the contents of the file into an array
}
System.out.println (arrays.tostring (content));//print array contents

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: You need to add a 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!

    • The correction code is as follows:

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?

    • The action to close the file is placed in the finally block because the file needs to be closed regardless of whether an exception occurs.
    • Exceptions can also occur when you use finally to close resources, so the statements in the finally block are also required 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?

    • The advantage: After using the try-with-resources system will be called close() off to shut down the resources, we do not have to write this part of the code, of course, we do not need to catch the exception, so the code is more concise.

Object-oriented design job-Library management system (group completion, no more than 3 students per group)

Log in to lib.jmu.edu.cn to 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?

    • Students, faculty, and 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.

    • Student/faculty:

Login
Loss
Enquiry Form
View current Lending information
Borrowing books
Return books
Exit system

    • Administrator:

Search for book information
Add New Books
Delete the next shelf book

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, etc.

    • Library information and reader information are stored in documents.

3. Code Cloud and PTA

Topic Set: Exceptions
3.1. Code Cloud codes Submission record

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.


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