190 likes | 405 Views
A Review. Java Fundamentals. Common Programming Constructs. Software Development Cycle: design, implement, test, debug, document Large Problems are broken down into smaller modules to be solved using flowcharting or pseudo-code, then coded, & re-assembled to solve the complete problem.
E N D
A Review Java Fundamentals
Common Programming Constructs • Software Development Cycle: design, implement, test, debug, document • Large Problems are broken down into smaller modules to be solved using flowcharting or pseudo-code, then coded, & re-assembled to solve the complete problem. • High-level languages require a compiler to translate source code into machine code ( or byte code in the case of Java). • Byte code is interpreted by the Java Virtual Machine and run. a review of lessons learned so far… ( 2 steps forward - 1 step back)
Program command execution is linear, thus, programming statements must be written in order (linear) as well. • Programming languages contain keywords/reserved words that can only be used as their intended purpose as determined by the language. • Languages utilize a specific syntax & punctuation. • Primitive data types are “built in” to the language, and allow for data storage & retrieval. • Storage space must be allocated for data, before it can be used to store data. • Programming languages allow for user defined “identifiers” of storage space (variables & constants), methods, and classes (in object-oriented languages like Java). a review of lessons learned so far… ( 2 steps forward - 1 step back)
The API extends/expands the language with a library of classes/methods for common programming tasks. • Common Mathematical Operators are: • +, -, *, /, %, ++ (increment), -- (decrement) & - (unary minus) • = (The Assignment Operater, assigns the rval to the thelval ) • Mathematical Expressions are processed with operator precedence & associativity. • Programmers use comments to note explanation of coding methods & identifier purposes for future reference. • Good Programming Practice is to write “readable by humans” code (indent, ws, comments). a review of lessons learned so far… ( 2 steps forward - 1 step back)
Types of errors You mean there are more than one? Compile-Time: syntax errors caught by the compiler Run-Time: errors caused by exceptions resulting in the program terminating abnormally (ex. attempting to divide by zero (0) - undefined command - program crashes) Logic: program compiles & runs, but, produces inaccurate results (always test program with data to verify accurate results)
Common Coding Errors Compiler Gotcha: Mismatched braces, quotation marks, or parenthesis Possible Error Message: Errors.java:13: reached end of file while parsing public class Errors{ Enter them in pairs & label closing public static void main(String args[ ]) { //body of method } //closing main method } //closing class header
Common Coding Errors Compiler Gotcha: misspelling a keyword Error Message: Errors.java:6: cannot find symbol Type Accurately. Remember keywords are l.c.
Common Coding Errors Compiler Gotcha: Using a keyword as a variable name Errors.java:6: not a statementErrors.java:6: ';' expectedErrors.java:6: not a statement Sampling of most commonly accidently used keywords (and replacement choice): class (use group) final ( use endResult) return (use returnValue) false ( use negative) true (use affirmative) new (use newValue) null ( use emptyValue)
Common Coding Errors Compiler Gotcha: misspelling variable names & switching use of l.c. & u.c. Errors.java:7: cannot find symbol Be consistent. Follow convention when naming variables. List all variable declarations at the beginning of a method - FIRST! (then you can refer to them as a list when you need to use them, and… you will not improperly use or accidently reuse a variable name)
Common Coding Errors Compiler Gotcha: putting blank space in a multi-word variable name Error Messages: Errors.java:6: ';' expectedErrors.java:6: not a statement Just Don’t Do It! To reinforce this habit, never use blank spaces when you name any computer file.
Common Coding Errors Compiler Gotcha: forgetting to end the statement? Error Message: Errors.java:6: ';' expected Use it! ; (semi-colon)
Common Coding Errors Compiler Gotcha: mis-matched data types & literals Error Message: Errors.java:6: possible loss of precision Remember float literals are treated as double values. float total = 5.0; // wrong float total = 5.0F; // correct
Common Coding Errors Compiler Gotcha: using commas (,) or currency ($) symbols in numeric data types Error Message: Errors.java:6: ';' expected DON’T USE $ OR , IN DATA! float totalExpense = $5,000.00F; Correct: float totalExpense = 5000.00F;
Common Coding Errors unintentionally performing integer division (loss of remainder) Error Message: NONE! Watch out for this one! public class Errors { public static void main(String args[ ]) {int x =5;int y =2;int z; z= x/y;System.out.println(z); //output: 2 } //closing main method }
Common Coding Errors forgetting the rules for operator precedence Error Message: NONE! Watch out for this one! x = 2+5*4; // x = 22 x = (2+5)*4; // x = 28 y = 7-2+3; // y = 8; y = 7 – (2+3); // y = 2; When operators within an expression have the same precedence, sharing an operand, they work according to their associativity. When in doubt, use (parenthesis)!
Common Coding Errors Compiler Gotcha: placing a space between combined operators Error Message: Errors.java:9: illegal start of expression z + = y; //Don’t z += y; //Do
Common Coding Errors Compiler Gotcha: incompatible data types public class Errors { public static void main(String args[ ]) { float y =2.75F;intz =1; z+= y; //Don’t (z:3) } //closing main method } public class Errors { public static void main(String args[ ]) { float y =2.75F;float z =1.0F; //Do z+= y; //(z:3.75) } //closing main method }
Common Coding Errors Compiler Gotcha: forgetting to terminate a multi-line comment Error Message: Errors.java:3: unclosed comment /* start multi-line comment blah, blah, blah… */
Common Coding Errors You must import the Scanner Object! Compiler Gotcha: forgetting to use the import statement Errors.java:11: cannot find symbol import java.util.Scanner;public class Errors { public static void main(String args[ ]) { Scanner keyboard = new Scanner(System.in); } //closing main method } //closing class definition For this statemnt to work…