Review of Terms and Concepts from Last Semester


Variables/Constants/Literals

Variable
A named space for holding values. Its name is referred to as its identifier.
Atomic Variable
A variable that can hold one datum (e.g., a number or a character).
Complex Variable
A variable that can hold multiple data (e.g., a class or an array).
Data Type
A designation that specifies a set of values (which may be infinite).
Primitive Data Type
A data type that is predefined in a programming language.
Constant
A named space for holding values. The value may not change once initialized.
Literal
A value used explicitly in a program, such as the number 1 or the String "abc".

Variables and constants must be predefined to be used in a Java program.

By convention, we declare variables and constants at the beginning of the method (or function) in which they are used.

By convention, declarations should be grouped based on data type.

Self documenting code suggests that variable names should make intuitive sense and should be used for only one purpose in any program.

For 239, if your variable names are CLEAR you don't need to document each one. You MUST use the @param for input arguments and each of these must be documented individually, one per line.

Java will not allow automatic narrowing conversions (loss of precision message). Use type-casting to explicitly change the type of a result.

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, pg 74 for further discussion of data types.

Operators

Operator
A symbol indicating that an operation is to be performed on one or more operands.
Operand
A variable, literal, constant, or an expression.
Binary Operator
An operator that takes two operands.
Unary Operator
An operator that takes a single operand.

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)

Memorize the truth tables - L&L page 205 and 206.

Operator precedence - See Appendix D, page 678

Automatic data conversion - Done as it is needed, operation by operation.

Type casting - Done at whatever point you want to change the type.
Casting is in the precedence rule list.

Examples: Operator.java

DeMorgan's Laws: ! (a && b) is equivalent to !a || !b

and ! (a || b) > is equivalent to !a && !b

Decisions

Two decision (or selection) statements in Java, if and switch.

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 simple statements.

Examples: Decisions.java

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