Week10 "Java Programming" job Summary 1. This week's study summary 1.1 summarizes anomalies related content in the way you like (mind map or other).
For::
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?
For:
- Frequently occurring anomalies are:
input type does not match numberformatexception, array is out of bounds arrayindexoutofboundsexception, null pointer nullpointerexception, Force type conversion classcastexception, illegal parameter input illegalargumentexception. These exceptions do not need to be captured, they are the exception that occurs when a problem occurs at run time, and it belongs to RuntimeException.
Most of the exceptions are exception and its subclasses. Except for RuntimeException, exception and its subclass exceptions need to be captured,
-
In the process of writing, should pay attention to clear-structured, careful and meticulous. When an error occurs, use Try-catch to catch the exception.
1.2 What kind of exception requires the user to be sure to use capture processing?
A: A checked exception type exception must be captured. Checked exception type exception is exception and its subclass exception except RuntimeException.
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?
For:
-
Experimental summary: In the possible error code into the try box, when there is input non-integer string, you should let catch catch exception and in Catch notice the index of the array minus one, or the system will be the location of the index default to 0.
try{int value = Integer.parseInt(line);arr[i] = value;}
-
Use try to catch exceptions, throw information about exceptions with catch, facilitate program debugging, and make the program more robust.
3. Throw and throws
Topic 7-3
Read Integer.parsetint source code
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?
For:
-
Intercept part of source code
public static int parseint (String s) throws NumberFormatException {return parseint ( s,10); public static int parseint (String s, int radix) throws numberformatexception{if (s = = null) {thro W 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");
-
Benefit: Here, the exception thrown is numberformatexception. An exception is generated for data that does not match the input format, and throws an exception message to alert the user that the input format is incorrect. Including the input S is null, the input number is less than 2, greater than 36, etc. to throw an exception.
3.2 in combination with its own program and 3.1, analysis of the method you write to throw an exception generally need to pass to the caller what information?
A: The caller is passed to the caller about the input range is correct, when the index starts Begin> index end end, throws the Begin>end exception, when the index starts begin<0, throws the begin<0 exception The end>arr.length exception is thrown when the end of the index is greater than the length of the array.
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: Using the way to throw an exception can let the program immediately stop running when the error, clearly tell the user program error where the number of rows, the cause of the error, from the intuitive can find the error point. Instead, use the return error value to alert the program that there is no way to throw an exception as intuitive and unambiguous as to the cause of the error, where the program will not stop because of the error. For example, when the code is in the stack, the fullstackexception exception is thrown directly if the stack is full.
public Integer push(Integer item) throws FullStackException { if(item==null) { return null; } if(top==capacity){ FullStackException e=new FullStackException(); throw e; } arrStack[top]=item; top++; return item; }
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: You can not use Throws,runtimeexception exception is unchecked Exception, not necessarily need to be captured, so you can not use. When a method may have an exception, but does not want to handle the exception in the current method, you can use the throws, throw keyword to throw an exception in the method. Using throws in one method, you can prevent some possible errors in advance, alerting the caller that the error exception that occurred is correct.
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: In the catch should be aware that the subclass exception must be placed before the parent class exception, otherwise the parent exception will be caught before the subclass exception, and the subclass exception catch block will never be executed, the program will be compiled error. For example first NumberFormatException again illegalargumentexception last 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?
A: Using the multiple capture syntax, you can make the code concise, but you must be aware of the exception inheritance, the exceptions listed in the catch brackets must not have an inheritance relationship, or a compilation error will occur.
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: 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!
Answer: The code is corrected and then run
When the input file name is incorrect:
After re-entering the file name
Post-Rewrite code:
Scanner sc=new Scanner (system.in); byte[] content = null; FileInputStream FIS =null; while (true) {String name=sc.next (); try{fis = new FileInputStream (name); int bytesavailabe = fis.available ();//Gets the number of bytes available for the file if (bytesavailabe>0) {content = new Byte[byt esavailabe];//creates an array fis.read (content) that accommodates the file size, or reads the contents of the file into the array} System.out.println (Arrays. ToString (content));//print array contents}catch (FileNotFoundException e) {System.out.println ("File not Found" +name+ "please re-lose Enter the file name "); Continue } catch (Exception E1) {System.out.println ("Open or read file failed"); } finally{if (fis!=null) {try{fis.close () ; SYSTEM.OUT.PRINTLN ("Close file ing"); }catch (Exception E2) {SyStem.out.println ("Failed to close file"); } } } }
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?
A: Release resources in finally to ensure that the resources that are occupied can be freed anyway, and execute some code that frees the resources, such as shutting down the database and shutting down the network connection. The code that will be executed will be put into the finally when it will be executed after the exception is thrown in the program. The code in the finally block is executed whenever it occurs. You should also include Try-catch in Finally, because an exception can also occur inside a finally, such as when a resource shutdown fails, execution Try-catch throws a shutdown failure exception.
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?
A: The implementation automatically closes the resource, placing the resource in the parentheses after the try.
while(true){ String name=sc.next(); try(FileInputStream fis = new FileInputStream(name)){ int bytesAvailabe = fis.available();//获得该文件可用的字节数 if(bytesAvailabe>0){ content = new byte[bytesAvailabe];//创建可容纳文件大小的数组 fis.read(content);//将文件内容读入数组 } System.out.println(Arrays.toString(content));//打印数组内容 }catch(FileNotFoundException e){ System.out.println("找不到文件"+name+"请重新输入文件名"); continue; } catch(Exception e1){ System.out.println("打开或读取文件失败"); } }
Benefit: Make sure that the resource is closed, rather than adding try-catch chunks to the finally statement, so that the code is simple and smooth and no longer bloated.
7. 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?
A: Librarians, students, faculty.
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: Main module:
Responsible for
7.3 Main class design and class diagram of the system (available)
For:
7.4 How you plan to store library information, address information, reader information, and more.
A: You can consider storing information with a set collection or file.
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
For:
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 |
0 |
0 |
0 |
0 |
| 4 |
0 |
0 |
0 |
0 |
| 5 |
739 |
739 |
16 |
16 |
| 6 |
1084 |
345 |
28 |
12 |
| 7 |
1180 |
96 |
30 |
2 |
| 8 |
1627 |
447 |
35 |
5 |
| 9 |
1986 |
359 |
44 |
9 |
| 10 |
2350 |
364 |
56 |
12 |
| 11 |
2534 |
184 |
61 |
5 |
Week10 "Java Programming" job summary