Data Structures and Java

Download Report

Transcript Data Structures and Java

Data Structures and Java
CS 105
Review of Java Topics
The following Java features play an
important role when implementing data
structures in Java



4/4/2016
Interfaces
Exceptions
The Java class hierarchy and the Object class
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 2
Interfaces



4/4/2016
An interface indicates the method
signatures for the operations of a data
structure
An implementation of the data structure
is a Java class that implements this
interface to enforce the definition of all
methods
There can be multiple implementations of
the same interface/data structure
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 3
Example: Dictionary
Dictionary
SimpleDictionary
4/4/2016
BetterDictionary
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 4
public interface Dictionary
{
public void addWord( String word, String definition );
public void getDefinition( String word );
}
public class SimpleDictionary implements Dictionary
{
// define addWord and getDefinition
}
public class BetterDictionary implements Dictionary
{
// another implementation
// define addWord and getDefinition
}
4/4/2016
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 5
Exceptions


Some operations of data structures may
be invalid in certain situations
One option: handle the error within that
method by printing an error message


4/4/2016
Can be annoying since the user of the
method may get the message interspersed
with other output
Better alternative: throw exceptions so
that the user of the method can decide
how to deal with the error
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 6
Exceptions in Java



Exceptions are handled using a try-catch
statement
Exceptions are thrown from the method that
could cause the exception
What needs to be done



4/4/2016
Define a class that extends Exception
(the class may be empty)
In the method declaration, include a throws clause
In the method body, include a throw statement
where the exception occurs
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 7
Example
public class DuplicateWordException extends Exception
{ // this class could be empty
}
public class SimpleDictionary implements Dictionary
{
//…
public void addWord( String word, String definition )
throws DuplicateWordException
{
if ( getDefinition( word ) != null )
throw new DuplicateWordException();
// code to add dictionary entry here…
}
// …
}
4/4/2016
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 8
Example
Dictionary d = new SimpleDictionary();
try
{
d.addWord( “bat”, “mammal with wings” );
d.addWord( “cat”, “animal with whiskers” );
d.addWord( “bat”, “equipment used in baseball” );
d.addWord( “elephant”, “a large mammal” );
An exception
will be
thrown on
this call
}
catch( DuplicateWordException e )
{
System.out.println( “Duplicate Word Error” );
}
4/4/2016
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 9
More on Exceptions


Different kinds of exceptions can be
handled using a try-catch chain
Can have a more elaborate exception
class by defining exception/error details
inside the class; for example:


4/4/2016
error message
additional data about the error
(in the example, the word that causes the
duplicate to occur can be stored in the
DuplicateWordException class)
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 10
RuntimeException



4/4/2016
Make the exception class extend
RuntimeException instead of Exception
whenever you do not want to require
that the exception be caught
The user of the method may or may not
use a try-catch statement (try-catch is
required for Exceptions)
If not within a try-catch, the program
aborts
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 11
Interfaces and Exceptions


4/4/2016
In general, when a class implements an
interface, the throws clause should be
present in both the interface and the
class that implements it
However, an implementing class can
throw additional exceptions as long as
they are runtime exceptions
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 12
Inheritance hierarchy



The extends keyword/feature in Java
creates an inheritance hierarchy
If a class does not extend another class,
it implicitly extends Object, a built-in
class in Java
This means all classes are subclasses of
Object

4/4/2016
Variables of type Object can refer to an
instance of any class
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 13
The Object class
and data structures


When establishing interfaces for data
structures, it might be better to use the Object
class instead of particular types
Example for the Queue interface:
public void enqueue( Object o );
public Object dequeue();
instead of
Supports
Strings and
other types of
objects
public void enqueue( String s );
public String dequeue();

Will need to cast when retrieving an object
from the data structure:
String s = ( String ) q.dequeue();
4/4/2016
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 14
Summary



4/4/2016
Interfaces allow us to standardize
method signatures and to have multiple
implementations in a uniform manner
Exceptions allow us to elegantly handle
errors/unexpected situations
The Object class allows our data
structures to contain instances of any
type/class
Copyright 2005, by the authors of these slides, and Ateneo
de Manila University. All rights reserved.
L6: Abstract Class
Slide 15