Outline

One of the goals of this course

Dr. Arch Harris's Programming Style Guidelines

Review of Terms and Concepts from Last Semester


Variables/Constants/Literals

Identifier
Any name in Java.  Classes, variables, constants, methods all have identifiers.
Variable
A named space for holding values. Its name is called its identifier.  The values of a variable can change during program execution.
Constant
A named space for holding values. The value may not change once initialized. (By convention, initialize all constants at declaration.)
Literal
A value used explicitly in a program, such as the number 1 or the String "abc".
Data Type
A designation that specifies a set of values (which may be infinite).

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

Operator
A symbol indicating that an operation is to be performed on one or more operands.  
Operand
The value(s) that an operator works on.  An operand may be a literal, constant, variable, or an expression.
Binary Operator
An operator that takes two operands.
Unary Operator
An operator that takes a single operand.
Tertiary Operator 
The conditional operator takes 3 operands.  See chapter 3.8 if you are interested.

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).

Decisions

There are two decision (or selection) statements in Java, if and switch and one conditional operator.

if (condition)
statement (or statement block)
else

statement (or statement block)
The else portion is optional.

Alternate format:

if (condition) statement;
else statement;

Use this form only for very simple statements.

Examples: Decisions.java

No cascading if's unless you are dealing with entirely different conditions.  

Loops

Four items to look for in a loop structure:

Types of loops

Terms

Termination/Exit Condition:
The condition which when false will cause the loop to terminate.
Sentinel or Sentinel Value:
A specific value which signals that the loop should terminate. Not a normally processed value.
Infinite Loop:
A loop whose termination condition is never reached. The loop will not naturally end.
Off by one Error:
A loop that executes one too many or one too few times.
Indefinite Loop:
A loop that terminates when a termination condition is reached where the number of iterations is not known ahead of time.

Loop usage

Loop equivalency - A loop may be rewritten (sometimes awkwardly) as any other kind of loop.

See example: Loops.java


JMU CS 159 reference for: Jan 13, 2013 JMU CS 159