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

Info icon This 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 specification 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 definitions), 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 definitions 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 isFull ( ) ; // 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
Image of page 1

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

View Full Document Right 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 definitions 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 efficient 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 final 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 ) ; }
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern