Docsity

Structural Recursion - Lecture Slides | CMPU 101, Study notes of Computer Science

Toggle sidebar
bg1
Structural Recursion
CMPU-101: Problem-Solving and Abstraction
Jennifer Walter
Vassar College
Fall 2008
(slides originally written by Marc Smith)
Sub- and Superclasses
You already know that you can write classes that extend
existing classes. In effect, when a class extends an existing
class, the extending class has multiple types: its own type
and the type of its superclass.
For example, any class you've written that extends
ConsoleProgram is defining a data type that is a subclass of
the superclass ConsoleProgram and is of type
ConsoleProgram as well.
Another type of superclass is called an interface.
Interfaces
Declaring a program called an interface is useful when you
want to define the same instance methods in different
classes and you want to refer to those different classes by
the same typename.
Classes that implement the same interface are all members
of the interface type. The interface represents a union of
classes with similar functionality.
Interface definition line:
public interface InterfaceName { }
Subclasses of interfaces use the following class definition:
public class ClassName implements InterfaceName { }
Interfaces
Interfaces define common behaviors (methods) for
subclasses. But they do not provide implementations for any
of those methods. The method definitions in an interface
have no curly braces (ie, no bodies), they have only a
method definition line followed by a semicolon.
Any classes that have the "implements" keyword in the
class definition must provide implementations for each
method definition in the interface.
Why do we care about interfaces? Because they allow us
to write structural recursive programs and to use
polymorphic references.
Containment
Book
Book
--------------
--------------
String title
String title
Author author
Author author
Author
Author
------------
------------
String name
String name
int dob
int dob
String
String
--------------
--------------
char[] letters
char[] letters
“contains”
“contains”
“contains”
UML diagrams:
Class Name
instance variables
constructors
instance methods
Self-containment
•Objects can contain instance variables of their
own type
•A set of classes that implement the same
interface is called a union of classes
•In Java, classes can contain fields whose type is
the same as any of the interfaces they
implement
pf3
pf4

Partial preview of the text

Download Structural Recursion - Lecture Slides | CMPU 101 and more Study notes Computer Science in PDF only on Docsity!

Structural Recursion

CMPU-101: Problem-Solving and Abstraction

Jennifer Walter

Vassar College

Fall 2008

(slides originally written by Marc Smith)

Sub- and Superclasses

You already know that you can write classes that extend

existing classes. In effect, when a class extends an existing

class, the extending class has multiple types: its own type

and the type of its superclass.

For example, any class you've written that extends

ConsoleProgram is defining a data type that is a subclass of

the superclass ConsoleProgram and is of type

ConsoleProgram as well.

Another type of superclass is called an interface.

Interfaces

Declaring a program called an interface is useful when you

want to define the same instance methods in different

classes and you want to refer to those different classes by

the same typename.

Classes that implement the same interface are all members

of the interface type. The interface represents a union of

classes with similar functionality.

Interface definition line:

public interface InterfaceName { }

Subclasses of interfaces use the following class definition:

public class ClassName implements InterfaceName { }

Interfaces

Interfaces define common behaviors (methods) for

subclasses. But they do not provide implementations for any

of those methods. The method definitions in an interface

have no curly braces (ie, no bodies), they have only a

method definition line followed by a semicolon.

Any classes that have the "implements" keyword in the

class definition must provide implementations for each

method definition in the interface.

Why do we care about interfaces? Because they allow us

to write structural recursive programs and to use

polymorphic references.

Containment

BookBook

StringString titletitle

AuthorAuthor authorauthor

AuthorAuthor

StringString name name

intint dobdob

StringString

char[]char[] letters letters

“contains”

“contains”

UML diagrams: “contains”

Class Name

instance variables

constructors

instance methods

Self-containment

• Objects can contain instance variables of their

own type

• A set of classes that implement the same

interface is called a union of classes

• In Java, classes can contain fields whose type is

the same as any of the interfaces they implement

Example: Recursive

definition of a list

• A List is either:

• empty

• a construction consisting of:

• the first element

• the rest of the List

i.e., an object with no fields i.e., an object with two fields We can translate this definition into a class hierarchy...

Example: Recursive

definition of a list

• A List is either:

  • empty (class MTList)

(the base case)

  • a construction (class

ConsList) consisting of:

  • the first element
  • the rest of the List

• Interfaces never contain

constructors or instance

variables because they

cannot be instantiated.

IList

ConsList

int first

IList rest

MTList

IList union:

implements “contains”

Example: Recursive definition of a list

We can translate this

hierarchy into Java code...

IList

ConsList

int first

IList rest

MTList

IList union:

implements “contains” public interface IList {} pulic class MTList implements Ilist { public MTList() {} } public class ConsList implements Ilist { private int first; private IList rest; public ConsList(int first, IList rest) { this.first = first; this.rest = rest; } }

A view of object containment: (boxes in boxes)

new ConsList(42, new ConsList(43, new ConsList(44, new ConsList(45, new MTList() ) ) ) ) (42, (43, (44, (45, () ) ) ) ) List Creation statement Resulting List Structure To add a method to a union of classes:

  1. Add a "blank" method to the interface IList if the method is not defined in the Object class. Interfaces hold only method signatures with no braces or body code.
  2. Add a non-recursive method to the MTList class that does what an empty list should do in each particular situation.
  3. Add a recursive method to the ConsList class containing the statements to do what the method is supposed to do for a non-empty list.

Compute the length of a list

• If we wish to compute the length of lists of

type IList, we must add a definition line for a length method to interface IList

• Once we’ve defined length in the interface,

we must define the length method in all classes that implement IList: MTList and ConsList

• Where to begin? Let’s start with the

interface, and work our way down...

Does list contain a particular value? /**

  • empty list contains no elements / class MTList: not recursive... (base case) /*
  • constructed list either has x as
  • first element or x may be in
  • rest of list / class ConsList: recursive... (what type of object is this.rest? ) interface IList { public boolean contains(int x); } interface IList: (^) Questions about lists ! What is the length of the list? How many occurrences of some value are there? ! Does the list contain a particular value? How many even numbers are in the list? Does the list contain all odd values? How many times does a value occur in list? /*
  • empty list contains no elements / class MTList: not recursive... (base case) /*
  • constructed list either has x as
  • first element or x may be in rest
  • of list */ class ConsList: recursive... interface IList { public int countX(int x); } interface IList: