The Way of the Program
• Problem solving: The process of formulating a problem, finding a
solution, and expressing the solution.
• Program: A sequence of instructions that specifies how to perform tasks
on a computer.
• Programming: The application of problem solving to creating executable
computer programs.
• Computer science: The scientific and practical approach to computation
and its applications.
• Algorithm: A procedure or formula for solving a problem, with or without
a computer.
• Bug: An error in a program.
• Debugging: The process of finding and removing errors.
• High-level language: A programming language designed to be easy for
humans to read and write.
• Low-level language: A programming language designed to be easy for a
computer to run. Also called “machine language” or “assembly language”.
• Portable: The ability of a program to run on more than one kind of
computer.
• Interpret: To run a program in a high-level language by translating it one
line at a time and immediately executing the corresponding instructions.
• Compile: To translate a program in a high-level language into a low-level
language, all at once, in preparation for later execution.
• Source code: A program in a high-level language, before being compiled.
• Object code: The output of the compiler, after translating the program.
• Executable: Another name for object code that is ready to run on specific
hardware.
• Byte code: A special kind of object code used for Java programs. Byte
code is similar to a low-level language, but it is portable like a high-level
language.
• Statement: Part of a program that specifies one step of an algorithm.
• Print statement: A statement that causes output to be displayed on the
screen.
• Method: A named sequence of statements.
• Class: For now, a collection of related methods. (We will see later that
there is more to it.)
• Comment: A part of a program that contains information about the
program but has no effect when the program runs.
• String: A sequence of characters; the primary data type for text.
• Newline: A special character signifying the end of a line of text. Also
known as line ending, end of line (EOL), or line break.
• Escape sequence: A sequence of code that represents a special character
when used inside a string.
1
Variables & Operators
• Variable: A named storage location for values. All variables have a type,
which is declared when the variable is created.
• Value: A number, string, or other data that can be stored in a variable.
Every value belongs to a type (for example, int or String).
• Declaration: A statement that creates a new variable and specifies its
type.
• Type: Mathematically speaking, a set of values. The type of a variable
determines which values it can have.
• Syntax: The structure of a program; the arrangement of the words and
symbols it contains.
• Keyword: A reserved word used by the compiler to analyze programs.
You cannot use keywords (like public, class, and void) as variable names.
• Assignment: A statement that gives a value to a variable.
• Initialize: To assign a variable for the first time.
• State: The variables in a program and their current values.
• State diagram: A graphical representation of the state of a program at a
point in time.
• Operator: A symbol that represents a computation like addition, multi-
plication, or string concatenation.
• Operand: One of the values on which an operator operates. Most opera-
tors in Java require two operands.
• Expression: A combination of variables, operators, and values that
represents a single value. Expressions also have types, as determined by
their operators and operands.
• Floating-point: A data type that represents numbers with an integer part
and a fractional part. In Java, the default floating-point type is double.
• Rounding error: The difference between the number we want to represent
and the nearest floating-point number.
• Concatenate: To join two values, often strings, end-to-end.
• Order of operations: The rules that determine in what order operations
are evaluated.
• Composition: The ability to combine simple expressions and statements
into compound expressions and statements.
• Compile-time error: An error in the source code that makes it impossible
to compile. Also called a “syntax error”.
• Parse: To analyze the structure of a program; what the compiler does
first.
• Run-time error: An error in a program that makes it impossible to run
to completion. Also called an “exception”.
• Logic error: An error in a program that makes it do something other
than what the programmer intended.
2
Input & Output
• Package: A group of classes that are related to each other.
• Address: The location of a value in computer memory, often represented
as a hexadecimal integer.
• Library: A collection of packages and classes that are available for use in
other programs.
• Import statement: A statement that allows programs to use classes
defined in other packages.
• Token: A basic element of a program, such as a word, space, symbol, or
number.
• Literal: A value that appears in source code. For example, "Hello" is a
string literal and 74 is an integer literal.
• Magic number: A number that appears without explanation as part of
an expression. It should generally be replaced with a constant.
• Constant: A variable, declared final, whose value cannot be changed.
• Format string: A string passed to printf to specify the format of the
output.
• Format specifier: A special code that begins with a percent sign and
specifies the data type and format of the corresponding value.
• Type cast: An operation that explicitly converts one data type into
another. In Java, it appears as a type name in parentheses, like (int).
• Modulus: An operator that yields the remainder when one integer is
divided by another. In Java, it is denoted with a percent sign; for example,
( 5 % 2 = 1 ).
Void Methods
• Argument: A value that you provide when you invoke a method. This
value must have the same type as the corresponding parameter.
• Invoke: To cause a method to execute. Also known as “calling” a method.
• Parameter: A piece of information that a method requires before it can
run. Parameters are variables: they contain values and have types.
• Flow of execution: The order in which Java executes methods and
statements. It may not necessarily be from top to bottom, left to right.
• Parameter passing: The process of assigning an argument value to a
parameter variable.
• Local variable: A variable declared inside a method. Local variables
cannot be accessed from outside their method.
• Stack diagram: A graphical representation of the variables belonging to
each method. The method calls are “stacked” from top to bottom, in the
flow of execution.
• Frame: In a stack diagram, a representation of the variables and parame-
ters for a method, along with their current values.
• Signature: The first line of a method that defines its name, return type,
3
and parameters.
• Javadoc: A tool that reads Java source code and generates documentation
in HTML format.
• Documentation: Comments that describe the technical operation of a
class or method.
Conditionals & Logic
• Boolean: A data type with only two values, true and false.
• Relational operator: An operator that compares two values and produces
a boolean indicating the relationship between them.
• Logical operator: An operator that combines boolean values and pro-
duces a boolean value.
• Short circuit: A way of evaluating logical operators that only evaluates
the second operand if necessary.
• De Morgan’s laws: Mathematical rules that show how to negate a logical
expression.
• Conditional statement: A statement that uses a condition to determine
which statements to execute.
• Branch: One of the alternative sets of statements inside a conditional
statement.
• Chaining: A way of joining several conditional statements in sequence.
• Nesting: Putting a conditional statement inside one or both branches of
another conditional statement.
• Flag: A variable (usually boolean) that represents a condition or status.
• Recursion: The process of invoking (and restarting) the same method
that is currently executing.
• Recursive: A method that invokes itself, usually with different arguments.
• Base case: A condition that causes a recursive method not to make
another recursive call.
• Binary: A system that uses only zeros and ones to represent numbers.
Also known as “base 2”.
Value Methods
• Value method: A method that returns a value.
• Return type: The type of value a method returns.
• Return value: The value provided as the result of a method invocation.
• Temporary variable: A short-lived variable, often used for debugging.
• Dead code: Part of a program that can never be executed, often because
it appears after a return statement.
• Incremental development: A process for creating programs by writing
a few lines at a time, compiling, and testing.
• Stub: A placeholder for an incomplete method so that the class will
4
compile.
• Scaffolding: Code that is used during program development but is not
part of the final version.
• Functional decomposition: A process for breaking down a complex
computation into simple methods, then composing the methods to perform
the computation.
• Overload: To define more than one method with the same name but
different parameters.
• Tag: A label that begins with an at sign (@) and is used by Javadoc to
organize documentation into sections.
• Turing complete: A programming language that can implement any
theoretically possible algorithm.
• Factorial: The product of all the integers up to and including a given
integer.
• Leap of faith: A way to read recursive programs by assuming that the
recursive call works, rather than following the flow of execution.
Loops
• Iteration: Executing a sequence of statements repeatedly.
• Loop: A statement that executes a sequence of statements repeatedly.
• Loop body: The statements inside the loop.
• Infinite loop: A loop whose condition is always true.
• Program development: A process for writing programs. So far we have
seen “incremental development” and “encapsulation and generalization”.
• Encapsulate: To wrap a sequence of statements in a method.
• Generalize: To replace something unnecessarily specific (like a constant
value) with something appropriately general (like a variable or parameter).
• Loop variable: A variable that is initialized, tested, and updated in order
to control a loop.
• Increment: Increase the value of a variable.
• Decrement: Decrease the value of a variable.
• Pretest loop: A loop that tests the condition before each iteration.
• Posttest loop: A loop that tests the condition after each iteration.
Arrays
• Array: A collection of values, where all the values have the same type,
and each value is identified by an index.
• Element: One of the values in an array. The [] operator selects elements.
• Index: An integer variable or value used to indicate an element of an
array.
• Reference: A value that indicates another value, like an array. In a state
diagram, a reference appears as an arrow.
5
• Alias: A variable that refers to the same object as another variable.
• Traversal: Looping through the elements of an array (or other collection).
• Search: A traversal pattern used to find a particular element of an array.
• Reduce: A traversal pattern that combines the elements of an array into
a single value.
• Accumulator: A variable used to accumulate results during a traversal.
• Deterministic: A program that does the same thing every time it is
invoked.
• Nondeterministic: A program that always behaves differently, even when
run multiple times with the same input.
• Pseudorandom: A sequence of numbers that appear to be random, but
which are actually the product of a deterministic computation.
• Histogram: An array of integers where each integer counts the number
of values that fall into a certain range.
• Enhanced for loop: An alternative syntax for traversing the elements
(values) of an array.
Strings & Things
• Object: A collection of related data that comes with a set of methods
that operate on it.
• Primitive: A data type that stores a single value and provides no methods.
• Unicode: A standard for representing characters in most of the world’s
languages.
Objects
• Attribute: One of the named data items that make up an object.
• Dot notation: Use of the dot operator (.) to access an object’s attributes
or methods.
• Object-oriented: A way of organizing code and data into objects, rather
than independent methods.
• Garbage collection: The process of finding objects that have no references
and reclaiming their storage space.
• UML: Unified Modeling Language, a standard way to draw diagrams for
software engineering.
• Class diagram: An illustration of the attributes and methods for a class.
• Immutable: An object that, once created, cannot be modified. Strings
are immutable by design.
• Empty string: The string "", which contains no characters and has a
length of zero.
• Wrapper class: Classes in java.lang that provide constants and methods
for working with primitive types.
• Parse: To read a string and interpret or translate it.
6
• Empty array: An array with no elements and a length of zero.
Classes
• Class: Previously, we defined a class as a collection of related methods.
Now you know that a class is also a template for a new type of object.
• Instance: A member of a class. Every object is an instance of some class.
• Instantiate: Create a new instance of a class in the computer’s memory.
• Data encapsulation: A technique for bundling multiple named variables
into a single object.
• Instance variable: An attribute of an object; a non-static variable defined
at the class level.
• Information hiding: The practice of making instance variables private
to limit dependencies between classes.
• Constructor: A special method that initializes the instance variables of
a newly-constructed object.
• Shadowing: Defining a local variable or parameter with the same name
and type as an instance variable.
• Client: A class that uses objects defined in another class.
• Getter: A method that returns the value of an instance variable.
• Setter: A method that assigns a value to an instance variable.
• Override: Replacing a default implementation of a method, such as
toString.
• Instance method: A non-static method that has access to this and the
instance variables.
• Identical: Two values that are the same; in the case of objects, two
variables that refer to the same object.
• Equivalent: Two objects that are “equal” but not necessarily identical,
as defined by the equals method.
• Pure method: A static method that depends only on its parameters and
no other data.
• Modifier method: A method that changes the state (instance variables)
of an object.
Arrays of Objects
• Encode: To represent one set of values using another set of values, by
constructing a mapping between them.
• Class variable: A variable declared within a class as static. There is
only one copy of a class variable, no matter how many objects there are.
• Sequential search: An algorithm that searches array elements, one by
one, until a target value is found.
• Binary search: An algorithm that searches a sorted array by starting in
the middle, comparing an element to the target, and eliminating half of
7
the remaining elements.
Objects of Arrays
• Pseudocode: A way of designing programs by writing rough drafts in a
combination of English and Java.
• Helper method: Often a small method that does not do anything
enormously useful by itself, but which helps another, more complex method.
• Top-down development: Breaking down a problem into sub-problems,
and solving each sub-problem one at a time.
• Selection sort: A simple sorting algorithm that searches for the smallest
or largest element ( n ) times.
• Merge sort: A recursive sorting algorithm that divides an array into two
parts, sorts each part (using merge sort), and merges the results.
Objects of Objects
• Collection: An object that contains other objects, or more specifically,
one of the objects in the Java library, like ArrayList, that contains objects.
• Inheritance: The ability to define a new class that has the same instance
variables and methods of an existing class.
• Subclass: A class that inherits from, or extends, an existing class.
• Superclass: An existing class that is extended by another class.
• Wrapper method: A method that calls another method without doing
much additional work.
• Bottom-up development: A way of developing programs by identifying
simple pieces, implementing them, and then assembling them into more
complex algorithms.
• HAS-A: A relationship between two classes where one class “has” an
instance of another class as one of its attributes.
• IS-A: A relationship between two classes where one class extends another
class; the subclass “is” an instance of the superclass.
Developmental Tools
• IDE: An “integrated development environment” that includes tools for
editing, compiling, and debugging programs.
• JDK: The “Java Development Kit” that contains the compiler, Javadoc,
and other tools.
• JVM: The “Java Virtual Machine” that interprets the compiled byte code.
• Text editor: A program that edits plain text files, the format used by
most programming languages.
• JAR: A “Java Archive”, which is essentially a ZIP file containing classes
and other resources.
8
• Command-line interface: A means of interacting with the computer by
issuing commands in the form of successive lines of text.
• Redirection operator: A command-line feature that substitutes
System.in and/or System.out with a plain text file.
• Wildcard: A command-line feature that allows you to specify a pattern
of filenames using the * character.
• Debugger: A tool that allows you to run one statement at a time and see
the contents of variables.
• Breakpoint: A line of code where the debugger will pause a running
program.
• Call stack: The history of method calls and where to resume execution
after each method returns.
• Unit test: Code that exercises a single method of a program, testing for
correctness and/or efficiency.
Java 2D Graphics
• AWT: The “Abstract Window Toolkit”, a Java package for creating
graphical user interfaces.
• Coordinate: A value that specifies a location in a two-dimensional graph-
ical window.
• Pixel: The unit in which coordinates are measured.
• Bounding box: A common way to specify the coordinates of a rectangular
area.
• RGB: A color model based on adding red, green, and blue light.
Incremental Development
Beginners often make the mistake of writing a lot of code before they try to
compile and run it. Then they spend way too much time debugging. A better
approach is what we call incremental development. The key aspects of
incremental development are:
• Start with a working program and make small, incremental changes. At
any point, if there is an error, you will know where to look.
• Use variables to hold intermediate values so you can check them, either
with print statements or by using a debugger.
• Once the program is working, you can consolidate multiple statements into
compound expressions (but only if it does not make the program more
difficult to read).
As an example, suppose you want to find the distance between two points, given
by the coordinates ((x_1, y_1)) and ((x_2, y_2)). By the usual definition:
p
distance = (x2 − x1 )2 + (y2 − y1 )2
9
The first step is to consider what a distance method should look like in Java.
In other words, what are the inputs (parameters) and what is the output (return
value)? In this case, the two points are the parameters, and it is natural to
represent them using four double values. The return value is the distance, which
should also have type double.
Already we can write an outline for the method, which is sometimes called a
stub. The stub includes the method signature and a return statement:
public static double distance(double x1, double y1, double x2, double y2) {
return 0.0;
}
The return statement is a placeholder that is necessary for the program to
compile. At this stage, the program doesn’t do anything useful, but it is good
to compile it so we can find any syntax errors before we add more code.
It’s usually a good idea to think about testing before you develop new methods;
doing so can help you figure out how to implement them. To test the method,
we can invoke it from main using sample values:
double dist = distance(1.0, 2.0, 4.0, 6.0);
The next step is to find the differences ( x_2 - x_1 ) and ( y_2 - y_1 ). We store
those values in temporary variables named dx and dy. With these values, the
horizontal distance is 3.0 and the vertical distance is 4.0. So the result should
be 5.0, the hypotenuse of a 3-4-5 triangle. When you are testing a method, it is
helpful to know the right answer.
Once we have compiled the stub, we can start adding lines of code one at a time.
After each incremental change, we recompile and run the program. If there is an
error at any point, we have a good idea where to look: the last line we added.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
System.out.println("dx is " + dx);
System.out.println("dy is " + dy);
return 0.0;
}
The print statements allow us to check the intermediate values before proceeding.
They should be 3.0 and 4.0. We will remove the print statements when the
method is finished. Code like that is called scaffolding, because it is helpful for
building the program, but it is not part of the final product.
The next step is to square dx and dy. We could use the Math.pow method, but
it is simpler to multiply each term by itself.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
10
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
System.out.println("dsquared is " + dsquared);
return 0.0;
}
Again, you should compile and run the program at this stage and check the
intermediate value, which should be 25.0. Finally, we can use Math.sqrt to
compute and return the result.
public static double distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx * dx + dy * dy;
double result = Math.sqrt(dsquared);
return result;
}
As you gain more experience programming, you might write and debug more
than one line at a time. Nevertheless, incremental development can save you a
lot of time. “‘
11