Dr. Arch Harris's Programming Style Guidelines
Variables and constants must be predefined to be used in a Java program. By convention, we declare all variables and constants at the beginning of the class or method in which they are used.Exception is a for loop when the variable is strictly used as a loop control variable.
Self documenting code suggests that variable names should make intuitive sense and should be used for only one purpose in any program.
You MUST use the @param for each input argument and they must be documented individually, one per line.
Java will not allow automatic narrowing conversions (loss of precision message) (except when ????) Use type-casting to explicitly change the type of an expression.
Examples: Variable.java
Note: All data is stored in binary on the computer. That limits the range of values for each of the primitive types. See book, Chapter 2 for further discussion of data types.
Operators
Assignment | = | Place the value on the right into the variable on the left. |
Arithmetic | + - |
Addition Subtraction |
* / |
Multiplication Division |
|
Integer Arithmetic | / % |
Division: whole quotient Modulo or Remainder: remainder of division operation |
Increment/Decrement
careful! see example |
++ | Add 1 (increment by 1) |
-- | Subtract 1 (decrement by 1) | |
Relational | == != |
equals not equals |
> < |
greater than less than |
|
>= <= |
greater than or equal to
less than or equal to |
|
Logical | && | AND |
|| | OR | |
! | NOT (not is unary) | |
^ | XOR (Exclusive OR) (one or the other but not both) | |
Combined | += -= *= /= |
plus equals minus equals multiply equals divide equals |
Memorize the truth tables
Operator precedence - Always use parentheses to resolve precedence.
Automatic data conversion - Done as it is needed, operation by operation. Java will automatically perform widening conversions, but not narrowing conversions**. Wrapper class - "autoboxing".
** - See combined operators
Type
casting - Done at whatever
point you want to
change
the type of the value. Does not change the type of the variable.
Examples: Operator.java
DeMorgan's Laws: ! (a && b) is equivalent to !a || !b
and ! (a || b) > is equivalent to !a && !b
For 159, you may use mixed operators (+=) as long as you know exactly what they can do (and a nasty side effect if you are not careful).
There are two decision (or selection) statements in Java, if and switch and one conditional operator.
if (condition)The else portion is optional.
statement (or statement block)
else
statement (or statement block)
Alternate format:
if (condition) statement;Use this form only for very simple statements.
else statement;
Examples: Decisions.java
No cascading if's unless you are dealing with entirely different conditions.Four items to look for in a loop structure:
Types of loops
Terms
Loop usage
Loop equivalency - A loop may be rewritten (sometimes awkwardly) as any other kind of loop.
See example: Loops.java