Fatal Runtime Errors Nonfatal Runtime Errors KIS UML OOD OOP Ooad OMG IDE
Common programming errors include syntax errors like missing semicolons or braces, logic errors like using the wrong operator, and compilation errors like inconsistent method declarations. Good programming practices include commenting code, formatting for readability, and learning from compiler error messages.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views
Fatal Runtime Errors Nonfatal Runtime Errors KIS UML OOD OOP Ooad OMG IDE
Common programming errors include syntax errors like missing semicolons or braces, logic errors like using the wrong operator, and compilation errors like inconsistent method declarations. Good programming practices include commenting code, formatting for readability, and learning from compiler error messages.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
Cause programs to terminate immediately without having successfully
Fatal runtime errors
performed their jobs. Nonfatal runtime errors Allow programs to run to completion, often producing incorrect results. KIS Keep it Simple UML Unified Modelling Language OOD Object - Oriented Design OOP Object - Oriented Programming OOAD Object - Oriented Analysis and Design OMG Object Management Group IDE Integrated Development Environment
Common Programming Errors
1. Students who are new to programming (or a programming language) tend to make certain errors frequently. Focusing on these Common Programming Errors reduces the likelihood that students will make the same mistakes and shortens long lines outside instructors' offices during office hours! 2. Forgetting one of the delimiters of a traditional or Javadoc comment is a syntax error. The syntax of a programming language specifies the rules for creating a proper program in that language. A syntax error occurs when the compiler encounters code that violates Java's language rules (i.e., its syntax). In this case, the compiler does not produce a .class file. Instead, the compiler issues an error message to help the programmer identify and fix the incorrect code. Syntax errors are also called compiler errors, compile-time errors or compilation errors, because the compiler detects them during the compilation phase. You will be unable to execute your program until you correct all the syntax errors in it. 3. Java is case sensitive. Not using the proper uppercase and lowercase letters for an identifier normally causes a compilation error. 4. It is an error for a public class to have a file name that is not identical to the class name (plus the .java extension) in terms of both spelling and capitalization. 5. It is an error not to end a file name with the .java extension for a file containing a class declaration. If that extension is missing, the Java compiler will not be able to compile the class declaration. 6. It is a syntax error if braces do not occur in matching pairs. 7. Omitting the semicolon at the end of a statement is a syntax error. 8. Splitting a statement in the middle of an identifier or a string is a syntax error. 9. All import declarations must appear before the first class declaration in the file. Placing an import declaration inside a class declaration's body or after a class declaration is a syntax error. 10. Forgetting the left and/or right parentheses for the condition in an if statement is a syntax error the parentheses are required. 11. Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as "is equal to," and the assignment operator should be read as "gets" or "gets the value of." To avoid confusion, some people read the equality operator as "double equals" or "equals equals." 12. It is a syntax error if the operators ==, !=, >= and <= contain spaces between their symbols, as in = =, ! =, > = and < =, respectively. 13. Reversing the operators !=, >= and <=, as in =!, => and =<, is a syntax error. 14. Placing a semicolon immediately after the right parenthesis of the condition in an if statement is normally a logic error. 15. Declaring more than one public class in the same file is a compilation error. 16. A compilation error occurs if the number of arguments in a method call does not match the number of parameters in the method declaration. 17. A compilation error occurs if the types of the arguments in a method call are not consistent with the types of the corresponding parameters in the method declaration. 18. Using floating-point numbers in a manner that assumes they are represented precisely can lead to logic errors. 19. Error Prevention Tips 1. Always test your Java programs on all systems on which you intend to run them, to ensure that they will work correctly for their intended audiences. 2. When we first designed this "tip type," we thought we would use it strictly to tell people how to test and debug Java programs. In fact, many of the tips describe aspects of Java that reduce the likelihood of "bugs" and thus simplify the testing and debugging processes. 3. When learning how to program, sometimes it is helpful to "break" a working program so you can familiarize yourself with the compiler's syntax-error messages. These messages do not always state the exact problem in the code. When you encounter such syntax-error messages in the future, you will have an idea of what caused the error. Try removing a semicolon or brace from the program of Fig. 2.1, then recompile the program to see the error messages generated by the omission. 4. When the compiler reports a syntax error, the error may not be on the line number indicated by the error message. First, check the line for which the error was reported. If that line does not contain syntax errors, check several preceding lines. 5. When attempting to compile a program, if you receive a message such as "bad command or filename," "javac: command not found" or "'javac' is not recognized as an internal or external command, operable program or batch file," then your Java software installation was not completed properly. If you are using the J2SE Development Kit, this indicates that the system's PATH environment variable was not set properly. Please review the J2SE Development Kit installation instructions at java.sun.com/j2se/5.0/install.html carefully. On some systems, after correcting the PATH, you may need to reboot your computer or open a new command window for these settings to take effect. 6. The Java compiler generates syntax-error messages when the syntax of a program is incorrect. Each error message contains the file name and line number where the error occurred. For example, Welcome1.java:6 indicates that an error occurred in the file Welcome1.java at line 6. The remainder of the error message provides information about the syntax error. 7. The compiler error message "Public class ClassName must be defined in a file called ClassName.java" indicates that the file name does not exactly match the name of the public class in the file or that you typed the class name incorrectly when compiling the class. 8. When attempting to run a Java program, if you receive a message such as "Exception in thread "main" java.lang.NoClassDefFoundError: Welcome1," your CLASSPATH environment variable has not been set properly. Please review the J2SE Development Kit installation instructions carefully. On some systems, you may need to reboot your computer or open a new command window after configuring the CLASSPATH. 9. Forgetting to include an import declaration for a class used in your program typically results in a compilation error containing a message such as "cannot resolve symbol." When this occurs, check that you provided the proper import declarations and that the names in the import declarations are spelled correctly, including proper use of uppercase and lowercase letters. 10. Unless default initialization of your class's instance variables is acceptable, provide a constructor to ensure that your class's instance variables are properly initialized with meaningful values when each new object of your class is created. 11. Good Programming Practice 1. Good Programming Practices are tips for writing clear programs. These techniques help students produce programs that are more readable, self-documenting and easier to maintain. 2. Read the documentation for the version of Java you are using. Refer to it frequently to be sure you are aware of the rich collection of Java features and are using them correctly. 3. Your computer and compiler are good teachers. If, after carefully reading your Java documentation manual, you are not sure how a feature of Java works, experiment and see what happens. Study each error or warning message you get when you compile your programs (called compile-time errors or compilation errors), and correct the programs to eliminate these messages. 4. Every program should begin with a comment that explains the purpose of the program, the author and the date and time the program was last modified. (We are not showing the author, date and time in this book's programs because this information would be redundant.) 5. Use blank lines and space characters to enhance program readability. 6. By convention, always begin a class name's identifier with a capital letter and start each subsequent word in the identifier with a capital letter. Java programmers know that such identifiers normally represent Java classes, so naming your classes in this manner makes your programs more readable. 7. Whenever you type an opening left brace, {, in your program, immediately type the closing right brace, }, then reposition the cursor between the braces and indent to begin typing the body. This practice helps prevent errors due to missing braces. 8. Indent the entire body of each class declaration one "level" of indentation between the left brace, {, and the right brace, }, that delimit the body of the class. This format emphasizes the class declaration's structure and makes it easier to read. 9. Set a convention for the indent size you prefer, and then uniformly apply that convention. The Tab key may be used to create indents, but tab stops vary among text editors. We recommend using three spaces to form a level of indent. 10. Indent the entire body of each method declaration one "level" of indentation between the left brace, {, and the right brace, }, that define the body of the method. This format makes the structure of the method stand out and makes the method declaration easier to read. 11. Following the closing right brace (}) of a method body or class declaration with an end-of- line comment indicating the method or class declaration to which the brace belongs improves program readability. 12. Place a space after each comma (,) in an argument list to make programs more readable. 13. Declare each variable on a separate line. This format allows a descriptive comment to be easily inserted next to each declaration. 14. Choosing meaningful variable names helps a program to be self-documenting (i.e., one can understand the program simply by reading it rather than by reading manuals or viewing an excessive number of comments). 15. By convention, variable-name identifiers begin with a lowercase letter, and every word in the name after the first word begins with a capital letter. For example, variable-name identifier firstNumber has a capital N in its second word, Number. 16. Place spaces on either side of a binary operator to make it stand out and make the program more readable. 17. Using parentheses for complex arithmetic expressions, even when the parentheses are not necessary, can make the arithmetic expressions easier to read. 18. Indent an if statement's body to make it stand out and to enhance program readability. 19. Place only one statement per line in a program. This format enhances program readability. 20. A lengthy statement can be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense, such as after a comma in a comma- separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines until the end of the statement. 21. Refer to the operator precedence chart (see the complete chart in Appendix A) when writing expressions containing many operators. Confirm that the operations in the expression are performed in the order you expect. If you are uncertain about the order of evaluation in a complex expression, use parentheses to force the order, exactly as you would do in algebraic expressions. Observe that some operators, such as assignment, =, associate from right to left rather than from left to right. 22. Software Engineering Observation 1. The object-oriented programming paradigm requires a complete rethinking about the way we build software. Java is an effective language for performing good software engineering. The Software Engineering Observations highlight architectural and design issues that affect the construction of software systems, especially large-scale systems. Much of what the student learns here will be useful in upper-level courses and in industry as the student begins to work with large, complex, real-world systems. 2. Use a building-block approach to create programs. Avoid reinventing the wheel use existing pieces wherever possible. Called software reuse, this practice is central to object-oriented programming. 3. When programming in Java, you will typically use the following building blocks: Classes and methods from class libraries, classes and methods you create yourself and classes and methods that others create and make available to you. 4. Extensive class libraries of reusable software components are available over the Internet and the Web, many at no charge. 5. The J2SE Development Kit comes with the Java source code. Some programmers like to read the source code for the Java API classes to determine how the classes work and to learn additional programming techniques. 6. By default, package java.lang is imported in every Java program; thus, java.lang is the only package in the Java API that does not require an import declaration. 7. Normally, objects are created with new. One exception is a string literal that is contained in quotes, such as "hello". String literals are references to String objects that are implicitly created by Java. 8. The Java compiler does not require import declarations in a Java source code file if the fully qualified class name is specified every time a class name is used in the source code. But most Java programmers consider using fully qualified names to be cumbersome, and instead prefer to use import declarations. 9. A variable's declared type (e.g., int, double or GradeBook) indicates whether the variable is of a primitive or a reference type. If a variable's type is not one of the eight primitive types, then it is a reference type. For example, Account account1 indicates that account1 is a reference to an Account object). Look-and-Feel Observations 1. Look-and-Feel Observations highlight graphical user interface conventions. These observations help students design their own graphical user interfaces in conformance with industry norms. Performance Tips 1. Using Java API classes and methods instead of writing your own versions can improve program performance, because they are carefully written to perform efficiently. This technique also shortens program development time. 2. In our experience, teaching students to write clear and understandable programs is by far the most important goal for a first programming course. But students want to write the programs that run the fastest, use the least memory, require the smallest number of keystrokes, or dazzle in other nifty ways. Students really care about performance. They want to know what they can do to "turbo charge" their programs. So we highlight opportunities for improving program performance making programs run faster or minimizing the amount of memory that they occupy Portability Tips 1. Using classes and methods from the Java API instead of writing your own improves program portability, because they are included in every Java implementation. 2. Although it is easier to write portable programs in Java than in other programming languages, differences between compilers, JVMs and computers can make portability difficult to achieve. Simply writing programs in Java does not guarantee portability. 3. One of Java's "claims to fame" is "universal" portability, so some programmers assume that an application written in Java will automatically be "perfectly" portable across all Java platforms. Unfortunately, this is not always the case. We include Portability Tips to help students write portable code and to provide insights on how Java achieves its high degree of portability.
Object-Oriented Analysis and Design (OOAD)
Soon you will be writing programs in Java. How will you create the code for your programs? Perhaps, like many beginning programmers, you will simply turn on your computer and start typing. This approach may work for small programs (like the ones we present in the early chapters of the book), but what if you were asked to create a software system to control thousands of automated teller machines for a major bank? Or suppose you were asked to work on a team of 1,000 software developers building the next U.S. air traffic control system. For projects so large and complex, you could not sit down and simply start writing programs. To create the best solutions, you should follow a detailed process for analysing your project's requirements (i.e., determining what the system is supposed to do) and developing a design that satisfies them (i.e., deciding how the system should do it). Ideally, you would go through this process and carefully review the design (or have your design reviewed by other software professionals) before writing any code. If this process involves analysing and designing your system from an object-oriented point of view, it is called an object-oriented analysis and design (OOAD) process. Experienced programmers know that analysis and design can save many hours by helping them to avoid an ill-planned system-development approach that has to be abandoned part of the way through its implementation, possibly wasting considerable time, money and effort. OOAD is the generic term for the process of analysing a problem and developing an approach for solving it. Small problems like the ones discussed in these first few chapters do not require an exhaustive OOAD process. It may be sufficient to write pseudocode before we begin writing Java code pseudocode is an informal means of expressing program logic. It is not actually a programming language, but we can use it as a kind of outline to guide us as we write our code.
You Should Know…
To capture what a proposed system should do, developers often employ a technique known as use case modelling. In 1994, James Rumbaugh joined Grady Booch at Rational Software Corporation (now a division of IBM). RSC (Rational Software Corporation) is a division of IBM. The OMG (www.omg.org) is a non-profit organization that promotes the standardization of object-oriented technologies by issuing guidelines and specifications, such as the UML Collected Material From Different Books