Review for Test 2
No event handling in test 2!
Format of the test:
-
not multiple choice
-
using constructs from chapters
1 - 5 cannot be avoided
Instead:
-
Write a complete Java application
that ...
-
What is the output of the following
program ...
-
Provide test cases for (including
ones that produce a wrong result) ...
-
Write a section of code ...
-
Implement a class to work with
the following program ...
-
Extend a class (complete subclass
definition)
-
Which lines of the following
code produce errors?
Requires some (but very little)
memorization
public class MyApplication
{ public static
void main(String[] args)
{
}
}
public class MyClass
[extends MySuperclass implements MyInterface]
{ public MyClass(...)
// constructor
{
// usually initializes instance variables
}
public type
myMethod(...)
{
}
private
type myInstanceVariable; // instance variable
}
Details are the topic of
(chapter 3), chapter 7, and chapter 9.
for Loops
for (int i = 0; i < n;
i++)
{ ...
}
-
loop variable initialized (and
in this case defined), checked and incremented
-
standard format for standard
problems (otherwise use while)
-
should only be used if number
of iterations is known in advance
while loop
while (condition)
{ ...
}
-
all definitions, initializations,
and increments have to be done separately
-
useful if number of iterations
is not known in advance
-
e.g., determined by user input
Common problems
-
off-by-one errors
-
infinite loops
Random Numbers
Random generator = new Random();
int d = 1 + generator.nextInt(6);
-
create generator only once during
program execution!
-
nextInt(n) produces numbers
0..n-1
-
you might have to adjust the
range by adding / subtracting an offset
Method parameters
Can modifications of parameters
done within the method affect the calling program?
-
fundamental datatypes: no effect
-
objects:
-
if object is modified the calling
program is affected
-
many classes have methods that
don't modify objects but return a new object instead:
newObject = object.someMethod();
in that case even
object = object.someMethod();
will not modify object
in the calling program!
Accessor / Mutator methods
-
accessor methods return a value
or object
-
usually return information about
the object
-
should not modify the object
-
mutator methods modify the object
(return type void)
-
modify object
-
return type void
-
combining both is possible but
bad style (side effect!)
Keyword static
-
only one method / variable for
the whole class!
-
useful for constants (keyword
final)
-
or class properties (e.g., number
of objects created)
Scope of a variable
Recursion
-
a method may call itself
-
problem of avoiding infinite
recursions!
Testing
-
designing a test harness
-
program that calls method
-
designing test cases
-
think of problematic input (boundary
cases, e.g., 0 as a positive number)
-
check all branches
-
checking test cases
Debugging
-
without a debugger
-
with a debugger
-
breakpoints
-
step in / step over
-
inspect (evaluate) / data watches
-
Don't try to replace careful
programming by debugger use!
General strategies
-
reproduce error
-
locate error
-
devide and conquer
-
use program traces / breakpoints,
stepping
-
isolate error
-
comment out unrelated code
Inheritance
-
notation: myClass extends mySuperClass
-
myClass can instantly be used
in place of mySuperClass
-
new methods, instance variables,
and constructors can be supplied
-
you cannot access private instance
variables of the superclass (but you can access protected ones)
When to use it
-
if a class "is a"n other class:
use inheritance
-
if a class ''has a"n object
of other class: make an object of that class and instance variable
Overriding
-
if you implement a method that
exists in the super class (same signature!) you override it
-
you only override methods of
which you need a different implementation
-
calling a superclass method
when overriding:
-
calling a superclass constructor
Overloading
-
concept independent of inheritance
-
define a method with the same
name but different signature (combination of parameter types)
-
do not confuse with overriding!
Upcasting
-
object reference (object variable)
may have any superclass as type
-
MySuperClass superObject
= myObject;
-
useful expecially when parameter
of a method is of superclass type (results in an implicit assignment to
the local superclass variable in the method)
-
subclass methods may not be
used on superObject!
Downcasting
-
opposite of upcasting
-
may result in error (exception)
if object is not of appropriate type
Polymorphism
-
if overriding has been used
for a method, the actual method called depends on the object, not
on the object reference!
-
the compiler checks availability
of a method by checking the methods for the object reference
-
at runtime the executed method
depends on the object itself (but if the superclass implementation had
not existed the code would never have compiled!)
Interfaces
-
only definitions, no implementations
-
advantage: a class can only
extend one super class but may implement many interfaces
-
common use: a method may require
an object of "type" of the interface
-
can then rely on the availability
of methods
-
all methods that are promised
in the interface have to be implemented!
Superclass Object
-
all java classes inherit from
class Object
Events
3 classes involved
-
event source
-
generates and manages event
-
event
-
event listener
-
implements a specific listener
interface (to guarantee methods to be called by the event source)
-
must be registered with the
event source
Callback
-
when you register a listener
with an event source you tell the source that it can call the methods guaranteed
by the interface
Adapter
-
replaces listener interface
with a class
-
adapter implements interface
and provides methods with empty bodies
-
be careful to spell all methods
correctly!
Inner Classes
-
class within another class
Make sure to understand sample
code!