Interfaces and Wrappers - Object Oriented Programming I | CMSC 131, Study Guides, Projects, Research of Computer Science

Material Type: Project; Professor: Cleaveland; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Fall 2006;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-elj
koofers-user-elj 🇺🇸

3

(1)

9 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10/25/2006 CMSC 131 Fall 2006
Rance Cleaveland
©2006 Univeristy of Maryland
Lecture 24:
Interfaces
Last time:
1. Design patterns: Model-View-Controller
2. Polymorphism
3. Interfaces
Today:
1. Project #5 is due 10/31 at 11 pm
2. Interfaces (cont.)
3. Wrappers
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Interfaces and Wrappers - Object Oriented Programming I | CMSC 131 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CMSC 131 Fall 2006Rance Cleaveland

Lecture 24: ©2006 Univeristy of Maryland

Interfaces

Last time: 1.^

Design patterns: Model-View-Controller

2.^

Polymorphism

3.^

Interfaces

Today: 1.^

Project #5 is due 10/31 at 11 pm

2.^

Interfaces (cont.)

3.^

Wrappers

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Project #5 Assigned! 

Project due Tuesday, 10/31 at 11 pm

Project is

closed

^

You must complete the project by yourself ^

Assistance can only be provided by teaching assistants(TAs) and instructors ^

You must not look at other students' code

Start now! ^

Read entire assignment from beginning to end beforestarting to code ^

Check out assignment now from CVS ^

Follow the instructions

exactly

, as much of grading is

automated

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

min / max Revisited ^

Recall motivating example for interfaces ^

Wanted polymorphic min / max routines

^

Needed

compareTo

method similar to

String

^

One argument

^

Return values ^

-^

if <

^

0

if ==

^

1

if >

^

Can interfaces help? Yes! ^

Java includes interface

Comparable

as part of

java.lang

^

Interface contains one abstract method: int

compareTo(Object

o)

^

Return type:

int

^

Argument type:

Object

(We’ll see about this in a minute)

^

We can use this interface to redefine min / max!See

PolyMinMax.java

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Adapting Date to ImplementComparable 

Must implement

compareTo

method:

int

compareTo(Object

o)

What is

Object

?

^

Type of all possible objects in any class ^

Shortcoming of (earlier) Java: no good way to say “sametype as

this

^

Instead: ^

Implementation must take any object ^

An error should be raised if argument is not of correct type

^

How to raise error? ^

We can use type casting ^

This is a topic for later in semester

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Using Polymorphic min / max 

See

Driver.java

for last class



Note that same min / max methods used on^ Date

,^

String

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

What about

int

?

char

?



Polymorphic

min

/

max

can be used on any

class implementing

Comparable



What about primitive types (

int

,^

char

,

double

, etc.)?

They are not classes

They do not implement

Comparable

Hence

min

/

max

cannot be used on them

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

The

Integer

Wrapper



The documntation is on-line at^ http://java.sun.com/j2se/1.5.0/docs/api/ 

Notes^ 

Constructors

Implements

Comparable

^

Documentation says “Comparable” ^

Comparable

in Java 5.0 is a

generic

interface

^

We’ll understand this more later

Has

equals

method, etc.

CMSC 131 Fall 2006Rance Cleaveland ©2006 University of Maryland

Wrappers Allow Use of Max /Min 

What is printed as result of following:

Integer i = new Integer (1);Integer j = new Integer (2);System.out.println (PolyMinMax.min(i,j));



Answer:

1

Integer implements

Comparable

, so

compareTo

exists

It also implements

toString

, so

System.out.println

prints integer value

correctly