Content of Test 3
Format of the test:
-
not multiple choice
-
focus on chapters 10, 11, 13,
15
-
using constructs from previous
chapters cannot be avoided
Event Handling:
-
You have to understand the example
programs discussed in class and in the textbook.
-
You have to know the concepts
-
related to event handling (such
as event source, event, event listener)
-
commonly used in the context
of event handling (such as interfaces, adapters, inner classes).
(Frame Windows will not be asked)
Arrays / Vectors
You have to be able to write
simple methods that are commonly used on Arrays / Vectors
-
how many values are larger /
smaller than ...
-
what is the largest / smallest
value
-
what is the sum / average of
the values
-
which element(s) have the value
...
-
add / remove an element to an
array
Exceptions
You have to know
-
how to catch exceptions
-
how to throw exceptions
-
how to delegate exceptions to
the calling method
-
the difference between checked
and unchecked exceptions
-
that IOExceptions are checked
(not programmer's fault)
-
and most other common ones unchecked
(programmer's fault)
File I/O
-
difference between text files
and binary files
-
difference between sequential
and random access
-
you won't have to write code
to open / close files or read from / write to files
Algorithms
You might have to
-
suggest an algorithm
-
determine the order O(...) for
an algorithm
-
implement an algorithm
Review
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
Interfaces
-
only definitions, no implementations
-
advantage: a class can only
extend one super class but may implement many interfaces
-
all methods that are promised
in the interface have to be implemented!
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!
Arrays
-
all elements of equal type (fundamental
datatype or class)
-
extend class Object
-
object references are passed
to methods
-
syntax is different from other
Objects
-
double[] data = new double[10];
-
BankAccount[] accountArray
= new BankAccount[50];
-
this does not create any BankAccount
objects!!!
-
fixed length
-
int len = data.length;
-
length is a public instance
variable of the array!
-
you commonly access data using
an index
-
data[3] = 123.4;
-
value = data[7];
-
if you don't know the number
of data items in advance
-
create a larger array
-
keep track of actual number
of data items with a separate variable
-
check if array is full when
adding elements
-
use arrayCopy otherwise
-
adding to / deleting from unsorted
arrays
-
add at the end
-
deleting: replace element by
the last one in the array
-
adding to / deleting from sorted
arrays
-
elements have to be moved
-
2 or more dimensional arrays:
Vectors
-
properly object oriented
-
Vector points = new Vector();
-
Point2D.Double p = new Point2D.Double(100,200)
-
points.add(p); //
adds at the end
-
variable length
-
int noOfElements = points.size();
-
all elements of type "Object"
=> any combination of objects possible
-
special methods for retrieval
of data
methods return objects of
class Object => downcasting necessary
Point2D.Double pOut
= (Point2D.Double) points.get(23);
Exceptions
-
may be caught
-
program stops otherwise
Catching Exceptions
try
{...
} catch (Exception e)
{...
}
-
try block can contain more than
one statement
-
you can either catch an Exception
exactly of the class thrown or any superclass of it.
-
catching IOException catches
also EOFException, etc.
-
catching the generic "Exception"
is not recommended
-
you can add more than one catch
block
Delegating Exceptions
-
Add "throws IOException" (or
whichever exception applies) to the definition of the method
public void
readFromFile() throws IOException
-
used if the method that calls
your method can better handle the exception (e.g., if you don't know if
your method will be called by an application or applet)
-
do not use try / catch blocks
in that case!
-
not recommended for main
Throwing exceptions
-
E.g. you expect to read 20 lines
but the file only contains 10
String input = in.readLine();
if (input == null)
throw new
EOFException("File not long enough");
Checked vs. unchecked exceptions
-
Checked exceptions have to be
caught
-
usually not due to programming
mistake
-
e.g., IOException
-
Unchecked exceptions don't have
to be caught
-
usually due to programming mistake
-
e.g., NullPointerExceptions
(and all others you have so far seen in labs)
-
you can catch them but you are
not encouraged to
-
sometimes you should check "unchecked
exceptions": e.g. Integer.parseInt(String) if input is provided by the
user
Random Access
-
method of accessing any file
(other than keyboard input or screen output)
-
file pointer can be moved to
specific location
-
usually record lengths should
be constant
-
you can use random access for
text, or any other data type
-
(methods for random access of
text files in java use Unicode, which means that you cannot open other
text files for random access)
Binary <=> text files
-
binary format corresponds to
the way data types / objects are stored internally
-
text files contain humanly readable
text
-
numbers are converted to strings
-
potential of wasting a lot of
space
Algorithms
-
can be written in any language
(e.g., natural languages)
-
unambiguous, executable, terminating
Analysing Performance
-
scientists like power laws and
orders of magnitude!
-
don't waste effort on prefactors
-
find some basic unit: data visits
O(...)
-
O(...) describes how the fastest
growing term depends on the relevant number(s)
Searching algorithms
-
linear search for unsorted data
-
binary search for sorted data
Sorting
-
selection sort requires two
loops through data => O(n^2)
-
merge sort works on the basis
of merging two sorted sets