CS 211 Review Guide: Prof. Snyder's Sections Test #1 Review Guide - Snyder
CS 211 Review Guide: Prof. Snyder's Sections Test #1 Review Guide - Snyder
Prof.
Snyder's
Sections
Test
#1
Review
Guide
-
Snyder
The
first
test
covers
everything
we
have
discussed
about
basic
Java
programming,
through
control
structures,
arrays,
and
methods.
We
continued
into
classes
and
objects,
and
learned
how
to
create
and
use
both,
including
making
constructors
and
other
methods.
We
further
discussed
visibility
(public/private),
which
is
the
last
topic
on
our
test.
In
preparing
for
your
test,
you
should
be
aware
that
anything
in
the
required
readings,
anything
presented
in
lecture,
and
anything
covered
in
the
labs
and
assignments,
is
valid
testing
material.
I
will
try
to
create
a
laundry
list
of
topics
here,
but
it
is
not
a
guarantee
that
things
won't
show
up
on
the
exam.
Since
I'm
the
one
making
the
study
guide
and
our
test,
it's
a
pretty
good
bet
that
it
will
indeed
be
the
same
listing
of
things
I
feel
is
important;
just
be
sure
you
study
everything
at
your
disposal,
even
if
this
review
guide
serves
to
focus
your
time
on
specific
topics.
This
class,
and
especially
this
first
test,
has
a
significant
focus
on
being
able
to
write
programs
in
the
Java
language.
This
course
also
has
a
significant
focus
on
being
able
to
describe
the
object-oriented
concepts
that
we
have
learned,
so
as
a
transition
from
CS
112,
there
will
likely
be
an
increased
(but
not
extreme)
focus
on
more
theoretical
questions,
such
as
"why
is
X
a
good
idea",
or
"how
does
Y
make
Z
exhibit
more
encapsulation?".
We
will
still
have
questions
that
test
your
ability
to
program
in
Java,
just
as
CS
112
would
test
your
ability
to
write
Python
code.
But
now
we
will
also
have
a
slightly
shifted
focus
towards
the
concepts
of
OO.
Sometimes
the
theoretical
part
doesn't
occupy
quite
as
much
a
portion
of
our
presentation
time,
because
the
details
of
writing
Java
code
dwarf
the
theory;
but
that
doesn't
mean
the
theory
was
less
important.
This
guide
does
not
attempt
to
go
into
complete
detail,
especially
when
that
detail
is
available
already
in
our
slides
and
in
the
book.
Throughout
the
slides,
there
are
many
"Practice
Problems"
slides;
they
are
an
excellent
starting
point
for
the
sorts
of
sample
problems
you
might
expect
to
see
on
the
exam.
Other
examples
(not
explicitly
listed
as
practice
problems)
in
the
slides
are
also
quite
instructive
of
what
to
expect
on
the
test.
Java Basics
The
Java
compiler
translates
from
source
code
to
bytecode,
which
runs
on
the
"Java
Virtual
Machine".
Many
different
systems
have
interpreters
that
translate
bytecode
to
machine
instructions
for
their
particular
CPU's.
syntax:
the
way
to
write
something
in
a
language.
semantics:
the
meaning
of
a
language
feature
or
specific
piece
of
code.
errors:
o compile-time
errors.
E.g.,
syntax
errors
(can't
be
interpreted
as
valid
code
in
the
language);
type
errors
(misuse
of
language
features
so
that
it
is
not
valid
code).
o run-time
errors.
Code
compiled,
but
attempted
something
illegal/impossible
(e.g.,
divide
by
zero,
out-of-bounds
array
index,
using
null
like
an
actual
object
value).
o semantic
errors.
The
code
compiles
and
runs
(doing
what
it
implied),
but
we
have
written
code
that
doesn't
do
quite
what
we
want
to
occur
(such
as
dividing
instead
of
multiplying,
or
not-quite-finding
the
maximum
in
an
array).
Edit-Compile-Run
cycle:
When
writing
code,
we
first
edit
a
document;
then
we
compile
it
(to
translate
to
Java
bytecode,
in
this
class),
which
generates
the
.class
file;
last,
we
run
it.
No
matter
what
development
environment,
or
language,
this
three-phase
cycle
is
occurring,
even
when
the
steps
are
blurred
by
DrJava
or
Eclipse
or
some
other
IDE.
HelloWorld:
be
able
to
write
out
the
"hello,
world"
program.
Although
we
haven't
learned
what
quite
all
the
parts
of
it
mean,
we
are
making
great
progress
already.
Also,
running
any
program
amounts
to
this
basic
piece
of
code.
o on
the
test,
you
won't
always
have
to
write
code
to
make
a
full
program;
read
carefully
if
a
question
just
asks
for
a
method,
or
lines
of
code
that
would
do
something
when
run.
We're
usually
just
looking
for
the
couple
lines
or
so
that
relate
to
a
specific
task.
Whitespace:
any
whitespace
allowed
between
any
identifiers,
operators,
etc.
(as
long
as
they
can
be
distinguished,
we
don't
even
need
whitespace:
x+=1*foo(a,b);
versus
x
+=
1
*
foo
(
a
,
b
)
;
).
Only
exception:
can't
have
newlines
within
a
string
literal
(between
matching
""'s).
Thus
whitespace
doesn't
have
the
indentation
meaning
that
we
saw
in
Python,
even
though
we
strongly
encourage
indentations
again.
Comments:
/*
multi
line
*/
and
//to-end-of-line
styles.
identifiers:
names
for
things
in
our
code.
Consist
of
letters,
numbers,
underscores,
and
$'s,
but
don't
start
with
numbers.
(Please
don't
use
$'s:
they
are
for
code
that
writes
code).
keywords:
identifiers
that
have
built-in
meaning
in
the
language,
such
as:
for
while
if
case
switch
public
private
primitive
types
versus
reference
types:
o primitive
types:
8
basic
types
where
the
values
are
atomic
(no
sub-portions)
and
of
a
known,
small,
fixed
size.
They
are:
boolean,
char,
byte,
short,
int,
long,
float,
double.
! literals:
know
how
to
create
literals
in
all
primitive
types.
Recall
hexadecimal
input,
F
suffix
for
float,
L
suffix
for
longs.
o reference
types:
types
created
through
a
class
definition
or
array
definition
(whether
we
write
the
class
or
we
get
it
from
a
library
of
code,
such
as
java.util
or
java.lang).
casting:
a
conversion
from
a
value
of
one
type
to
a
value
of
another
type.
o implicit
casting
conversions:
when
no
information
is
lost,
Java
will
convert
between
types
for
us.
E.g.,
from
int
to
double;
from
short
to
int;
from
anything
to
String
(for
printing
purposes,
usually;
uses
the
toString()
method
for
objects).
o explicit
casting
conversions:
programmer-specified
conversions.
Usually
required
when
the
conversion
loses
precision
or
other
information;
tells
Java
that
it
is
'okay'
to
perform
the
lossy
action.
E.g.:
from
double
to
int;
from
long
to
short.
o Be
able
to
identify
when
Java
performs
implicit
casts.
2
o Be
able
to
use
explicit
casts
to
get
data
into
the
right
format
(type).
Creating
Variables
o declaration:
a
type
and
an
identifier.
Tells
Java
that
there
should
be
a
named
storage
location
that
can
contain
one
value
of
the
given
type,
and
will
be
accessed
using
the
given
identifier.
Only
declared
variables
can
be
used,
ever.
Expressions
vs.
Statements
o Expression:
a
representation
of
a
calculation
that
can
be
evaluated
to
result
in
a
single
value;
no
indication
what
to
do
with
the
value.
! Be
comfortable
identifying
expressions.
! Understand
the
ternary
?:
expression.
o Statement:
a
command/instruction
for
the
computer
to
perform
some
action;
often,
statements
contain
expressions.
! Be
comfortable
identifying
statements
versus
expressions.
Strings
o concatenation:
+
operator.
o implicit
conversion
of
everything
to
String
values
when
added
to
a
String,
or
when
a
String
value
is
needed.
o escape
sequences:
\t,
\n,
\",
\\,
etc.
o checking
for
string
equality:
can't
just
use
==.
Use
the
equals
method:
e.g.,
s1.equals(s2).
o Strings
are
immutable
(can't
change).
printing:
via
System.out.println(),
System.out.print(),
System.out.printf().
Scanner:
class
providing
convenient
methods
to
read
text
input
from
a
source
(such
as
keyboard/System.in).
o Be
able
to
use
basic
scanner
methods:
nextInt(),
nextDouble(),
next(),
nextLine(),
etc.
Constant:
a
'variable'
whose
value
will
never
change
from
its
initial
value.
Indicated
with
the
final
keyword.
o naming
convention:
all
caps.
ex:
MIN_HEIGHT,
NUM_PONIES.
increment/decrement:
e.g.,
x++,
++x,
x--,
--x.
"syntactic
sugar"
for
an
increment
on
the
previous
or
following
line.
See
our
slide
examples
for
their
semantics.
Be
ready
to
examine
code
using
them
and
report
what
happens.
We
ought
not
have
more
than
one
of
these
per
variable
per
statement
Control Flow
break,
continue:
ways
to
modify
loop/switch
control
flow.
Try
not
to
use
them
too
often,
but
sometimes
they
really
do
make
a
block
of
code
simpler.
Arrays
arrays
are
fixed-length
sequences
of
values,
all
of
a
single
type.
e.g.,
an
array
of
integers
(where
each
slot
holds
one
integer,
like
an
integer
variable).
representing
an
array
type:
place
[]'s
after
a
type
to
indicate
an
array
of
that
type.
String
String[].
int
int[].
Defines
an
array
type.
o can
add
multiple
dimensions:
int[][],
float[][][],
etc.
Declaration:
as
always,
give
a
type
and
an
identifier;
now
the
type
is
an
array
type.
ex:
int[
]
xs
;
Instantiation:
must
indicate
exact
length.
o {vals,like,this}
//allowed
at
declaration
time
only.
o new
int[]{vals,like,this}
//
allowed
anywhere
o new
int[10].
(Fills
with
default
values:
0,
false,
null,
as
appropriate
for
the
type).
accessing/update:
using
xs[index].
index
must
be
an
expression
yielding
an
integer
value.
o No
slicing
like
Python
allowed.
usage
with
loops.
A
lot
of
code
examples
used
loops
and
arrays;
be
comfortable
doing
these
sorts
of
things.
Class:
a
class
is
a
type.
It
is
like
a
blueprint
for
making
objects.
It
defines
the
state
that
each
object
will
have,
and
what
behaviors
(methods)
are
available
for
those
objects.
Object:
an
object
is
a
value.
(A
value
of
a
particular
class
type).
It
is
an
instance
of
its
class
(one
distinct
value
of
that
type).
It
has
its
own
copy
of
each
instance
variable
and
method.
Terminology:
a
type
defined
through
a
class
is
called
a
"reference
type",
because
we
don't
have
the
direct
value
as
with
ints;
instead,
we
deal
with
references
to
the
object
value
that
resides
in
memory.
A
variable
is
a
named
container
(on
the
stack).
A
reference
is
just
a
(type,address)
pair
of
an
address
of
an
object
in
memory
that
also
knows
what
type
it
points
to.
Some
variables
only
contain
primitive
values
(no
reference),
while
other
variables
only
contain
references
(the
value
is
elsewhere).
An
object
is
an
instance
of
a
class
(it's
a
value),
and
it
resides
in
the
heap
memory.
The
object
contains
its
own
copy
of
each
instance
variable
and
method.
many
references
can
have
the
address
of
the
same
object;
they
are
called
aliases.
Since
there's
only
one
object
involved,
updates
via
one
reference
are
visible
through
the
other
references.
instance
variable:
a
(non-static)
variable
declared
inside
a
class,
indicating
that
each
object
of
the
class
will
have
its
own
maintained
copy
of
this
variable.
E.g.,
the
Square
class
has
a
side
instance
variable.
A
Coordinate
class
could
have
both
x
and
y
instance
variables.
constructor:
special
method
that
is
used
when
creating
a
new
object.
o no
return
type
listed
(returns
reference
to
object
of
the
class's
type)
o method
name
must
be
the
class
name
exactly.
o parameters:
entirely
at
programmer's
discretion.
(Often
one
per
instance
variable).
o default
constructor:
If
a
class
definition
does
not
explicitly
list
a
constructor
definition,
then
a
default
implementation
is
available:
no
parameters,
and
all
instance
variables
receive
default
values:
0,
false,
and
null
(for
reference
types).
Methods
Static
modifier
that
indicates
there
should
only
be
one
definition
for
the
entire
class.
Note:
static
doesn't
mean
"unchanging"!
That's
what
the
final
keyword
is
for.
static
variable:
a
"class
variable".
One
copy
overall,
regardless
how
many
objects
are
made
(zero
to
many,
it
doesn't
matter).
Access
as
Classname.varname, or, as varname inside the class.
static
method:
like
a
regular
method,
but
cannot
use
instance
variables.
Therefore,
accessible
without
an
object,
directly
through
class
(since
no
object
is
used
to
call
it).
Access
as
Classname.method(..), or inside the class definition as just method(..)
Usage
example:
main
is
static
(because
we
don't
make
an
object
of
the
class
to
run
main).
If
we
want
'helper'
methods
for
main,
they
must
also
be
static:
o static
things
can't
use
non-static
things!
(Local
variables
in
a
static
method
seem
like
an
exception,
but
it's
still
inside
a
static
location).
References
the
result
of
a
constructor
call
is
not
the
object
itself
it
is
a
reference
to
that
object.
A
reference
is
simply
an
address
of
an
object.
only
values
are
ever
sent
across
from
actual
parameters
of
a
method
call
to
formal
parameters
during
execution
of
method
body.
primitive
types:
a
copy
of
the
primitive
value
is
sent.
No
effect
on
the
original
primitive
value
is
possible
(or
wherever
it
may
have
been
stored).
reference
types:
a
copy
of
the
reference
value
is
sent
(creating
an
alias).
No
effect
on
the
original
reference
is
possible;
however,
these
aliases
both
point
to
the
same
object
in
memory
and
thus
can
witness
each
others'
updates
to
that
object.
6
We
had
examples
of
each
of
these,
to
understand
what
effect
on
memory
was
possible
when
passing
primitive/reference
types
for
parameters.
Garbage
Collection
When
an
object
is
no
longer
reachable
by
your
program
(no
existing
chain
of
references
can
reach
it),
Java
identifies
it
as
'garbage',
and
in
the
next
'garbage
collection'
sweep,
Java
will
delete
this
object
(reclaim
its
memory
for
later
use).
garbage
collection
is
automatic
in
Java
very
convenient!
(But
there
are
programming
tasks
where
garbage
collection
may
not
be
desirable/allowed/available).
Object
class
ancestor
of
all
classes.
Provides
the
toString()
and
equals(Object
o)
methods,
among
others.
o we
can
redefine
toString()
to
aid
in
printing
(Java
uses
this
toString
method
to
String-ify
any
object,
quite
often
with
printing
in
mind).
Packages
Random Class
pseudorandom
number:
one
that
seems
random
(despite
being
based
quite
exactly
upon
some
seed
state
and
previous
values).
Scale/offset
the
nextFoo
values
to
get
the
range
you
want.
Wrapper Classes
Scope
scope
of
data
is
the
area
in
a
program
where
that
data
can
be
referenced
(used).
7
data
declared
at
the
class
level
can
be
referenced
by
all
methods
in
that
class.
data
declared
within
a
method
is
called
"local
data",
and
can
only
be
used
in
that
method.
Terminology
Visibility
class
members
(and
classes)
can
be
given
a
visibility.
For
now,
just
consider
public
and
private.
public:
anyone
with
access
to
this
object
is
allowed
to
use
this
public
portion
of
it
(whether
it's
reading/writing
a
public
variable,
or
calling
a
public
method).
private:
access
to
this
member
is
restricted
to
other
members
inside
the
class:
meaning
that
an
object
can
use
this
private
thing
while
performing
its
own
calculations,
but
the
outside
world
can't
use
it.
o Good
for
enforcing
encapsulation:
presentation
to
outside
world
is
the
public
stuff,
internal-only
representations
and
methods
are
private
stuff.
Based
on
current
view
of
an
object:
internal
vs.
external.
(internal:
everything
available;
external:
only
public
stuff
available).
public
methods:
"service
methods".
private
methods:
"support
methods".
accessor/mutator
methods
(getters
and
setters):
ways
to
individually
restore
read/write
privileges
to
users
of
private
variables.
public
stuff
defines
the
"interface"
(we
called
it
the
API/application
programming
interface
in
Python)
to
the
object.
CS
211
Test
#2
Review
Guide
-
Snyder
The
second
test
broadly
covers
inheritance
(including
overriding,
abstract
classes
and
methods,
constructors,
etc),
interfaces,
enumerations,
exceptions,
numeric
systems
(converting
between
bases),
and
the
concepts
of
unit
testing
/
debugging
/
javadoc.
It
will
not
include
searching/sorting
(that
is
the
breaking
point).
In
preparing
for
your
test,
you
should
be
aware
that
anything
in
the
required
readings,
anything
presented
in
lecture,
and
anything
covered
in
the
labs
and
assignments,
is
valid
testing
material.
I
will
try
to
create
a
laundry
list
of
topics
here,
but
omission
here
is
not
a
guarantee
that
things
won't
show
up
on
the
exam.
Since
I'm
the
one
making
the
study
guide
and
the
test,
it's
a
pretty
good
bet
that
it
will
indeed
be
the
same
listing
of
things
I
feel
is
important;
just
be
sure
you
study
everything
at
your
disposal,
even
if
this
review
guide
serves
to
focus
your
time
on
specific
topics.
This
guide
does
not
attempt
to
go
into
great
detail
(such
as
exhaustive
examples),
especially
when
that
detail
is
available
already
in
our
slides,
lab
tutorials,
and
in
the
book.
As
is
often
the
case,
if
you
are
not
running
Java
code
while
you
study,
you
will
likely
not
do
well
on
the
test.
Don't
restrict
yourself
to
just
catching
up
on
reading;
be
sure
you
actually
practice
writing
code!
Throughout
the
slides,
there
are
those
"Practice
Problems"
slides;
they
are
an
excellent
starting
point
for
the
sorts
of
sample
problems
you
might
expect
to
see
on
the
exam.
The
quizzes
are
another
excellent
resource
for
sample
question
styles.
Other
examples
(not
explicitly
listed
as
practice
problems)
in
the
slides
are
also
quite
instructive
of
what
to
expect
on
the
test.
We've
posted
a
lot
of
code
samples
from
class;
review
that
code
as
a
way
to
see
the
ways
we
introduced
topics,
and
think
back
to
how
we
discussed
why
we
would
do
things,
followed
by
how
we
would
do
them.
Lastly,
if
you
haven't
completed
the
lab
tutorials
yet,
that's
a
great
source
of
practice!
Don't
just
skim
over
examples
because
you
don't
see
any
difficult
situations
arising;
code
it
up,
make
sure
it
compiles,
make
sure
it
works.
I
have
a
hunch
people
are
omitting
the
lab
chapter
exercises,
and
that
might
be
a
telling
factor
in
who
does
well
on
the
test
or
not.
Format
will
again
likely
be
part
multiple
choice,
part
short
answer,
part
longer
responses.
Topics
Inheritance
Allows
a
new
sort
of
code
reuse:
similar
state
(fields)
and
behavior
(methods)
can
be
"inherited"
from
one
class
to
another.
o Establishes
a
"parent-child"
relationship.
Also
called
"superclass/subclass",
"base
class/derived
class".
o indicated
with
extends
keyword:
class Car extends Vehicle
Perhaps
most
importantly,
we
now
have
a
supertype/subtype
relationship.
All
the
child
classes'
objects
can
be
used
where
the
parent
class's
objects
were
expected.
Inheriting
things:
any
fields
or
methods
of
the
parent
class
are
automatically
a
part
of
the
child
class.
They
can
never
be
removed.
Methods
may
be
re-implemented
(with
the
parent
class's
9
Object
class
o it
is
always
an
ancestor
(parent,
grandparent,
..)
of
every
class.
o provides
a
few
useful
methods:
equals(..),
toString(),
others.
o we
prefer
always
overriding
these
methods.
Abstract
class:
o a
class
that
cannot
(yet)
be
instantiated
(we
can't
make
objects
of
this
exact
type).
o The
opposite
of
an
"abstract"
class
is
a
"concrete"
class.
! concrete
classes
may
be
instantiated
o abstract
class:
achieved
by
adding
the
abstract
keyword
to
class
declaration.
public abstract class Shape { .. }
o abstract
classes
also
may
optionally
include
abstract
methods:
methods
that
are
declared
(with
abstract
modifier),
but
have
no
body
(a
;
instead
of
{..}).
! just
like
all
other
members,
abstract
methods
will
be
inherited.
o abstract
classes
may
still
have
child
classes.
If
the
child
class
overrides
every
abstract
method
that
was
inherited,
then
the
child
may
be
concrete.
o Just
like
other
classes,
an
abstract
class
is
a
type.
The
collection
of
fields
and
methods
we
introduced
are
all
guaranteed
to
exist
(and
be
concrete)
for
all
actual
instances
from
child
classes.
This
is
the
whole
point
of
introducing
an
abstract
class
into
our
class
hierarchy:
to
have
a
formal
way
to
group
related
child
classes
and
use
them
uniformly.
final
classes,
f inal
methods:
o if
a
class
is
declared
to
be
final,
it
may
not
be
extended
(no
child
classes
are
allowed).
o if
a
method
is
declared
to
be
final,
it
may
not
be
overridden
(no
child
class
may
ever
change
its
implementation
of
this
method).
Interfaces
Enumerations
basically,
a
finite
set
of
values
that
are
grouped
into
a
new
type.
simple
examples:
o enum TrafficLight {RED, AMBER, GREEN}
o enum Day {MON, TUES, WED, THURS, FRI, SAT, SUN}
internal
implementation
(good
mental
model):
as
a
class,
except
that
constructors
must
be
private
(so
no
extra
values
are
created
anywhere
else),
and
the
finite
list
of
values
are
included
at
the
beginning
of
the
class
definition
once
and
for
all
(as
static
fields).
Otherwise,
we
can
still
add
methods,
fields,
all
the
usual
class
stuff.
Simple
Usage:
o in
general,
access
with EnumName.EnumValue (e.g.,
Grade.A).
o in
switch:
access
with
EnumValue.
(Java
already
knows
EnumName
b/c
of
the
switch
expression)
o in
foreach
loop
(utilizing
the
array
returned
by
values()):
for (Grade g : Grade.values() ) { ... }
Advanced
features:
o add
three
things
at
once:
! fields
(any
visibility)
! private/package-default
constructors
(not
public:
can't
be
accessed
outside
package
ever,
and
no
protected:
enums
can't
be
extended)
! "constructor
calls"
to
the
enumerated
values
to
give
state
to
the
values
(see
slides)
o add
methods
to
the
entire
enumeration.
(static
or
non-static;
different
visibilities
ok
except
protected)
Number Systems
Exceptions
Idea:
Java's
representation
of
"something
went
wrong
that
can't
be
handled
right
here".
o when
thrown,
an
exception
causes
normal
control
flow
to
be
abandoned,
like
a
rogue
return
statement.
o There
are
many
Exception
classes,
related
in
a
class
hierarchy
(using
inheritance).
We
can
extend
these
classes
with
our
own
child
classes,
letting
us
hook
into
Java's
exception
and
exception
handling
effort.
o Exception
classes
to
know
about
(know
of
an
example
if
starred):
!
Throwable
! Exception
RuntimeException
o NullPointerException
o ArrayIndexOutOfBoundsException
o ClassCastException
o ArithmeticException
IOException
o FileNotFoundException
Causing
exceptions:
o call
a
method
that
throws
an
exception
(e.g.
,
file
not
found)
o expression
that's
nonsensical
(e.g.,
5/0
or
xs[-3])
o manually
throw
one
(e.g.,
throw new MyException(""");)
Handling
exceptions:
o wrap
the
suspicious
code
in
a
try
block.
o immediately-following
catch
blocks
specify
how
to
handle
exceptions
of
the
particular
type
listed
in
the
catch
block.
o we
can
have
multiple
catch
blocks
for
one
try
block,
where
each
catch
block
handles
a
different
exception.
! order
catch
blocks
from
most
specific
(most
childly)
to
most
general
(most
ancestral,
such
as
Exception
itself).
o we
also
may
have
a
finally
block
after
all
the
catch
blocks.
This
block
always
runs
whether
no
exceptions
occur,
or
an
exception
occurs
and
is
caught,
or
an
exception
occurs
and
won't
be
caught
(the
finally
block
still
gets
to
run
before
the
propagation
continues,
and
if
the
finally
block
throws
its
own
exception
then
the
original
one
is
lost).
Propagating
exceptions:
o means
we
allow
it
to
crash
this
part
of
the
program,
if
the
exception
actually
occurs.
o when
a
try
block
has
no
corresponding
catch
block
(or
we
didn't
even
have
a
try).
o Checked
vs.
Unchecked
exceptions:
! checked
exceptions
that
are
propagated
must
be
admitted
in
the
signature:
public int foo (int a) throws IOException {..}
! unchecked
exceptions
that
are
propagated
don't
have
to
be
admitted,
but
may
be.
! unchecked
exceptions
are
the
R untimeException
class,
E rror
class,
and
their
children.
They
tend
to
represent
program
bugs
and
hardware
failure
respectively,
so
should
be
solved/fixed
(for
bugs)
or
dealt
with
gracefully.
! checked
exceptions
represent
things
beyond
programmer's
control
that
still
must
be
dealt
with.
e.g.:
FileNotFoundException
when
trying
to
read
a
file
InputMismatchException
when
Scanner
token
is
wrong
13
javadoc:
a
tool
for
generating
API
documentation
based
on
Java
packages
(and
classes).
o generates
a
lot
of
HTML
(good
to
put
in
separate
directory.
Then,
open
index.html)
o always
"scrapes"
source
code
for
things
like
inheritance
hierarchy,
implemented
interfaces,
listing
out
fields/methods.
o can
write
special
/**
javadoc
*/
comments
that
are
also
inspected
by
javadoc.
! use
@tags
to
label
more
info:
@param,
@return,
@exception,
@author,
etc.
o can
limit
what
levels
of
visibility
are
included
o can
generate
API
documentation
for
many
packages/sub-packages
all
together.
o might
add
package-info.java
files
to
document
entire
packages.
debugger:
a
tool
for
interactively
inspecting
values
of
a
running
program.
o breakpoints:
when
these
statements
are
reached,
execution
pauses
(allows
user
to
take
control
and
inspect/change
things).
o stepping:
advancing
one
statement
at
a
time.
! step
into:
allow
steps
to
travel
with
method
calls
and
step
there
too.
(follow
calls)
! step
over:
allow
steps
of
method
calls
to
call-return
in
one
atomic
step.
(stay
local)
! step
out:
keep
stepping
until
a
return
statement
is
reached
(let's
leave
now)
o watches
/
watch
lists:
a
set
of
variable
names
that
the
debugger
will
show
values
for
at
each
pause
(of
course,
can't
display
anything
when
nothing
of
that
name
is
in
scope).
Just
variables/fields
available.
(DrJava
now
allows
tracking
of
expressions,
cool!)
o modifications:
! user
can
directly
change
a
variable's
memory
to
see
effects
(but
doesn't
change
what
original
program
will
do
when
run
again)
! user
can
run
expressions/statements
interactively
(to
inspect
more
than
just
variables).
! direct
edits
to
source
code
generally
will
not
affect
debugger's
behavior
until
next
compilation/debug-run.
14
Testing
o gaining
assurance
that
code
works
as
expected
o catching
bugs
in
earlier
phases
saves
orders
of
magnitude
of
effort!
o test
case:
specific
inputs
and
expected
outputs
for
some
part
of
the
program.
o regression
testing:
after
making
any
changes,
we
re-run
old
tests
that
had
passed,
to
double
check
that
nothing
was
broken.
o Testing
styles:
! black
box
testing:
test
cases
that
focus
on
the
meaning
of
the
code:
with
no
view
inside
the
"black
box",
what
behavior
is
expected?
! white
box
testing:
test
cases
that
are
aware
of
the
particular
implementation,
and
attempt
to
get
code
coverage.
Each
part
of
the
code
(loop,
if-else,
etc)
gets
test
cases
to
try
and
use
that
piece
of
code.
In
general,
test
cases
that
focus
on
the
implementation
more
than
the
general
meaning
of
the
code
could
be
considered
white
box
testing.
o passing
all
tests
doesn't
mean
program
is
necessarily
correct.
(Can't
usually
test
all
cases
anyways).
Defining
correctness
is
very
hard!
o unit
testing:
writing
tests
for
the
smallest
parts
of
a
program.
o test-driven
development:
writing
tests
before
(or
at
least
alongside)
implementation
JUnit:
a
unit
testing
framework
for
Java.
o java
package
that
can
be
imported
(and
used
to
run
test
cases/report
on
results)
o offers
annotations:
@Test
methods
are
test
cases.
Also,
@Before,
@After.
o require
behavior
by
using
assertWhatever
methods:
assertEqual(..),
assertTrue(..),
assertNull(..).
Report
failures
with
fail().
Command-Line
Arguments
Since
running
a
Java
program
means
running
the
main
method,
we
can
pass
in
an
array
of
String
arguments
on
the
command-line
when
we
execute
our
Java
program:
demo$ java MyProgram args here 123 "spacey ones in quotes" too
Only
strings
available:
main
method
signature
has
(String[] args)
parameter
list.
values
are
separated
by
spaces.
matching
single-
or
double-quotes
may
be
used
to
create
a
single
String
that
contains
a
space
(e.g.,
"spacey arg").
getting
numbers:
use
Integer.parseInt(s)
or
Double.parseDouble(s),
based
on
any
String s.
(e.g.,
Integer.parseInt(args[0])
).
15
CS 211
Final
Review
Guide
This
review
guide
covers
the
last
third
of
the
course
(everything
after
our
second
test's
materials).
The
final
exam
itself
will
be
cumulative:
you
should
anticipate
roughly
half
the
exam
to
cover
the
materials
listed
here,
and
the
other
half
should
cover
the
materials
of
the
first
two
tests.
The
labs
go
into
far
better
details
than
will
be
shown
here;
you
should
consider
the
lab
readings,
lab
exercises,
quizzes,
and
slides
as
other
good
sources
for
ideas
on
what
is
important,
as
well
as
what
sorts
of
questions
might
be
asked.
Good
luck!
Searching:
given
some
collection
of
data,
and
some
key
that
will
identify
the
piece
of
data
we
seek,
search
through
that
collection
and
return
the
data.
o some
possible
returns:
a
reference
to
the
object;
the
index
within
the
collection
that
can
be
used
to
quickly
re-find
the
data.
Sorting:
given
some
collection
of
data,
and
the
notion
of
some
sequential
traversal,
relocate
values
within
the
collection
so
that
the
traversal
will
visit
values
in
some
ordered
fashion.
o examples:
sorting
an
array
of
Person
objects
by
name,
or
alternatively
sorting
by
age.
Sorting
numbers
ascending.
You
should
be
capable
of
writing
a
search
algorithm
and
of
writing
a
sort
algorithm
of
your
choosing.
Searching
16
Sorting
Search
and
Sort
Summary
There
are
many
other
ways
to
search
or
sort
data,
we
simply
looked
at
a
simplistic
and
then
more
involved
version
of
each.
Different issues of efficiency, both in time taken and space needed during the calculation, arise.
Regular
Expressions
o |
:
the
vertical
bar
indicates
"or",
allowing
the
pattern
on
its
left
or
right
to
be
chosen.
Can
be
used
(with|many|choices|still|choosing|one|of|many).
o character
classes:
! used
to
define
how
to
match
a
single
acceptable
character.
Just
list
them
inside
[
]'s:
[aeiou]
! [a-z]
:
the
dash(-)
indicates
a
range
on
the
ASCII
chart;
must
be
ascending.
! [^abcde]
:
the
caret
(^),
only
when
it
is
the
first
symbol
in
a
character
class,
indicates
"not
the
characters
in
this
character
class".
! [nested[classes]]
:
union.
(any
single
character
from
either
can
be
selected).
! [nested&&[classes]]
:
intersection.
Only
a
character
from
both
classes
may
be
matched.
o pre-defined
groups:
many
common
character
classes
have
shortcut
names.
! \d
:
[0-9]
(digits)
! \s
:
[\t\n
\f\r]
(whitespace)
! \w
:
[a-zA-Z0-9_]
(identifier
characters)
! \D,
\S,
\W
:
[^0-9],
[^\t\n
\f\r],
[^a-zA-Z0-9_]
(non-whatever
versions)
! other
special
pre-defined
groups
also
exist.
(see
Java
Pattern
class
API
if
you're
interested).
o anchors:
represent
some
property
other
than
a
specific
character.
! ^
:
the
beginning
of
line.
! $
:
the
end
of
line.
! \b
:
a
word-boundary,
the
point
between
a
word-character
\w
and
a
non-word
character
\W.
! \B
:
a
non-word-boundary.
(the
point
between
2
word-characters
or
2
non-word
characters).
o embedded
flags:
ways
to
affect
the
overall
meaning
of
a
regular
expression.
! (?i)
:
case
insensitive.
! (?d)
:
unix
newlines
(only
\n
means
newline)
! (?m)
:
let
^,
$
match
on
each
line
in
the
string,
not
just
actual
begin/end
of
entire
string.
! there
are
more
of
these
we
just
want
to
be
comfortable
with
the
basic
idea.
18
!
!
19
Capture
Groups
o each
parenthesized
portion
of
a
regular
expression
is
a
capture
group.
They
are
numbered
1
and
up,
found
by
scanning
through
the
regex
from
left
to
right
and
numbering
each
opening
parenthesis
found.
(The
entire
regex
is
considered
group
0).
Pattern
and
Matcher
classes
o In
order
to
speed
up
loop
bodies
or
use
further
functionality,
we
can
use
the
Pattern
and
Matcher
classes.
o Pattern:
allows
us
to
represent
a
"compiled"
regular
expression
as
an
object
with
useful
methods,
rather
than
just
hiding
it
in
a
String.
!
!
o Matcher:
Provides
functionality
related
to
checking
for
matches
or
finding
the
next
match
within
a
Pattern.
Also
provides
the
group
method
for
extracting
capture
groups'
values.
!
checks
for
a
complete
match
between
the
Pattern
used
to
create
this
Matcher,
and
the
candidate
String
supplied
when
the
Matcher
was
created.
If
successful,
calls
to
group
with
existing
group
#'s
will
succeed,
returning
the
last
match
of
that
capture
group.
checks
for
the
first
match
within
the
candidate
String.
If
successful,
calls
to
group
with
existing
group
#'s
will
succeed,
returning
the
last
match
of
that
capture
group.
if
a
successful
match
or
find
has
been
completed,
and
the
regular
expression
that
matched
had
a
capture
group
of
the
given
number
input,
then
the
String
that
corresponded
to
this
group
is
returned.
Generics
o the type parameters must be instantiated at each constructor call, locking in the correct type
21
Method-definition
generics
o we
may
introduce
a
list
of
type
parameters
in
a
method
declaration.
! example:
public <U> U choose (U left, U right, boolean selector) {
return b ? first : second;
}
! Not
connected
to
class
type
parameters:
these
are
only
instantiated/used
inside
the
method,
and
possibly
at
different
types
for
each
call
of
the
method.
! Offers
a
sort
of
"infinite
overloading",
but
lets
us
constrain
different
types
via
equality
of
the
same
type
parameter,
though
it
will
become
some
specific
type
whenever
the
method
is
called.
Our
Examples:
o we
looked
at
a
simplistic
view
of
the
ArrayList
class
as
an
example
of
generics.
We
also
had
extensive
examples
of
Pairs
and
Boxes
in
the
lab.
Going
further:
o There
are
more
advanced
notions
of
generics,
involving
notions
of
subtyping,
extending
multiple
types
(both
from
class
inheritance
and
interfaces,
despite
frugal
re-usage
of
the
extends
keyword),
and
even
wildcards.
Just
be
aware
that
we
can
get
more
than
this
basic
parametric
polymorphism.
Example
usage:
ArrayList.
o Supply
a
type
in
<>'s
to
create
the
actual
type,
which
then
is
used
to
declare
variables
or
call
constructors:
(all
in
one
line
here:)
Java
Collections
ArrayList<E>
part
of
the
Java
Collections
Framework,
ArrayList<E>
represents
a
List<E>
structure
(and
has
an
underlying
array
implementation,
hence
the
name
"ArrayList").
As
a
class,
all
list
operations
are
through
methods:
e.g.,
xs.get(i),
xs.set(i,val)
instead
of
xs[i].
as
a
list,
it
has
more
functionality
than
just
an
array:
we
can
add/remove
elements
(changing
the
length
of
the
structure),
and
we
can
do
so
at
any
index
(not
just
the
end).
Feels
like
Python
lists
(because
it
is
a
list,
not
an
array!).
All
these
behaviors
come
from
implementing
List<E>.
extra
methods
available
due
to
the
array
implementation:
ensureCapacity,
trimToSize,
more
constrs.
Uses
generics
to
allow
it
to
contain
any
specific
type
of
element.
(see
generics
for
more
info).
22
Recursion
24