cs211sp08-03

cs211sp08-03 - Interfaces(cf"The Java Programming Language...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 211 Data Structures Interfaces. (cf “The Java Programming Language” book, ch 4.) These provide a very natural way to design a speciFcation for code to solve a problem, think of them as being contracts for classes. Effectively, cracking a problem can be broken down into designing the solution and then implementing it; the design stage being stipulated by listing ‘skeletal’ classes having methods (but no deFnitions), and the implementation stage being split between implementing the contracts (classes) and writing code which uses them to actually solve the problem. Hence a programming team could be divided into these three groups, allowing them to work largely in parallel once the design has been set. Imagine building a spec sheet for a queue . .. folk join at the back and are served (and leave) from the front. Being aggressive, we could insist that after joining a queue, the only way to leave is from the front. This might lead to . .. public interface Queue { // note that there aren’t even hints of method deFnitions below, save for the comments!!! void joinQ ( Person p ) ; // the person p gets added to the back of the queue Person leaveQ ( ) ; // to return the person at the front, and then dump them in favour of the next person in line int getLength ( ) ; // to return the number of people in the queue boolean is±ull ( ) ; // to return true if there’s no room left in the queue boolean isEmpty ( ) ; // to return true if there’s no-one in the queue } // end interface Queue 1 - interfaces everything in an interface is presumed public
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 211 Data Structures Note that promising to implement an interface forces (via the compiler) an implementation of every method listed in that interface (but doesn’t insist on the method defnitions being sensible!) ... public class Q1 implements Queue { void joinQ ( Person p ) { return ; } Person leaveQ ( ) { return new Person ( “anonymous” ) ; } int getLength ( ) { return Integer.MAX_VALUE ; } boolean isFull ( ) { return true ; } boolean isEmpty ( ) { return true ; } public Q1 ( int n ) { System.out.println ( “This queue is so ef±cient it takes up almost no space!!!” ) ; } // constructor public Q1 ( ) { this ( -47 ) ; } // equally silly default constructor } // end interface Queue Sadly, the compiler will be perfectly happy with this! Perhaps rather better might be . .. public class Q2 implements Queue { private Person [ ] storage ; private int length , front , back , size ; private ±nal int DEFAULT_SIZE = 100 ; void joinQ ( Person p ) { if ( ! isFull( ) ) { storage [ back++ ] = p ; length++ ; } else System.out.println ( “Sorry, full up” ) ; } // end joinQ method Person leaveQ ( ) { Person temp ; if ( ! isEmpty( ) ) { temp = storage [ front++ ] ; length-- ; return temp ; } else System.out.println ( “Sorry, queue is empty” ) ; } // end leaveQ method int getLength ( ) { return this.length ; } boolean isFull ( ) { return ( this.back == size ) ; } boolean isEmpty ( ) { return ( length == 0 ) ; }
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/04/2008 for the course CS 2110 taught by Professor Francis during the Spring '07 term at Cornell.

Page1 / 8

cs211sp08-03 - Interfaces(cf"The Java Programming Language...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online