1 / 26

Web Based Programming Section 6

Web Based Programming Section 6 . James King 12 August 2003. Key Skills. This section will teach how to: make your programs readable document you programs debug your programs. Making code Readable.

gayle
Download Presentation

Web Based Programming Section 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Based Programming Section 6 James King 12 August 2003

  2. Key Skills • This section will teach how to: • make your programs readable • document you programs • debug your programs

  3. Making code Readable • The key to readability is indentation it is difficult to see which bit of code is inside which if or loop if your presentation is messy • If you miss out a { or } it can be difficult to find the correct place to add one

  4. How not to Indent There is a } missing somewhere public void nestit() { for (int i=0; i<3; i=i+1){ System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); }

  5. One Way to indent (Pascal Method) • Each time you type a { all lines following are indented more • Each time you type a } all lines following are indented less • both { and } are on separate lines by themselves • it is easy to match up { and } pairs like this...

  6. One way to Indent (Pascal Method) public void nestit() { for (int i=0; i<3; i=i+1) { System.out.println("top of outer loop i "+i); for (int j=0; j<3; j=j+1) { System.out.println("inside inner loop i "+i+" j "+j); } System.out.println("bottom of outer loop i "+i); } There is a } missing !!

  7. Indentation Style • We don’t mind which way you indent as long as • you are consistent • it is easy to see nesting • it is easy to see where the blocks start and end

  8. Comments and automatic documentation Generation Key skills section

  9. Documentation - Comments • The simplest form of documentation for you code are comments. This allows you to add English descriptions to your code • These can be placed almost anywhere in your code • Comments can extend over multiple lines and are enclosed inside /* comment */ • Single line comments can be made with // comment

  10. Comments Example /* simple demonstration of the how the continue statement works inside a loop */ public void loopit() // takes no parameters { System.out.println("before loop"); for (int i=0; i<5; i=i+1) { System.out.println(" before if"+i); if (i==3) continue; System.out.println(" after if"+i); // bypassed if i is 3 // } oops I commented out the end of block marker by accident... System.out.println("after loop"); }

  11. Automatic Documentation Generation • Java compiler can generate HTML web pages from your comments if you place them in certain places in a certain style • comments go before the declarations /** * about this procedure **/ public void proc() • comments in the body of blocks are ignored

  12. Documentation Tags • In addition to describing the class inside the comment you can add a version and authors name using tags • @version number • @author name • You can document the parameters and return valves of a method • @param variable description • @returndescription • There are more tags...

  13. Documentation Example /** * description of the class * @author (your name) * @version (a version number or a date) */ public class doc { // description of the attribute private int x; /** * description of the constructor */ public doc() {} /** * description of the method * @param y description of the parameter * @return description of the return value */ public int sampleMethod(int y) {return x + y; } }

  14. HTML Documentation

  15. Debugging Key skills section

  16. Debugging Code • Most compilers come with a debugger which usually allows you to • Step though your program line by line • Examine the value of each attribute and variable in scope • Run the code at full speed and stop at certain points (breakpoint) • In addition BlueJ allows you to • Call a method and optionally provide values for the parameters it requires • Create a new instance of a class

  17. Debuggers are useful in several situations • You want to understand how someone else's code works and it is too complex to run in your head (reverse engineer) • You want to find out why your program is not doing what you expect it to and make it work correctly (debug) • You want to see how Java runs your programs and how the ifs and loops work

  18. Debugging • Debugging is a practical activity so you will learn how to do this in the practical sessions.

  19. Catching and Generating Runtime Errors Advanced Java Facilities

  20. Dealing with problems in a running program • The compiler tries to catch errors during byte code creation. However some errors only occur when the program is running int i=1; i=i-1; int b=3/i; 3 divided by 0 is infinity. Infinity can not be stored in a int The Java interpreter generates a ArithmeticException and stops running the program

  21. Catching Exceptions • To avoid the program stopping we can catch the exception try { int i=1; i=i-1; int b=3/i; } catch (Exceptione) { System.err.println(“CRASH”); } Any exception generated anywhere in this block of code will cause execution to jump to the catch block, skipping any remaining code. If there is no exception the catch block is skipped In either case execution eventually gets here

  22. Information about the Exception • The e in catch (Exception e) acts just like a parameter and is actually an object. It contains information about the exception e.printStackTrace(); displays on the console screen the class and line number the exception was generated at e.getMessage(); returns a string with details of why the exception occurred

  23. Catching different types of exceptions try { } Catch (ArithmeticException f) { } catch (Exception e) { } This will catch only ArithmeticException and any subclasses Since all Exceptions are subclasses of Exception this will pick up all other exceptions

  24. Cleaning up the mess • Sometimes regardless of if an exception is generated or not you want some code to be executed try { } catch (Exception e) { } finally { }

  25. Exceptions • Java built in methods and classes may generate exceptions if used incorrectly • The Java compiler expects you to either catch these possible exceptions in the method they could be created in or leave it to the method that called that method to handle them…

  26. Throws • Java expects each method to catch any exceptions it could generate • If you don’t want to do this you must add the uncaught exceptions to the method definition in a throws list void problem2() throws Exception { throw new Exception(); }

More Related