Lect7_interface - ITI 1121. Introduction to Computing II *...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of February 1, 2010 Abstract • Interface • Abstract data types * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Interface 1 In the general context of object-oriented programming (including but not restricted to Java), the interface designates the list of public methods and variables of a class. For example, consider a class Time having three public methods, getHours , getMinutes and getSeconds , and no other public methods or variables, then, the interface of the class Time is getHours , getMinutes and getSeconds . Problem 1 Through a series of problems, we will discover the need for a new concept, called interface . Problem 1: write a (polymorphic) method that sorts an array of objects. There is a variety of sort algorithms, including bubble sort , selection sort and quick sort . What do all these algorithms have in common? (do not answer now) To make this example concise, the size of the input array will be two (2). Therefore, sorting the array will be simple: if the value of the first element is smaller than the value of the second element, there is nothing to do, otherwise exchange the content of the two cells. An algorithm to solve the general case will also be presented. Specifically, here is an implementation that sorts an array of two integers. public static void sort2( int a ) { if ( a[ 0 ] > a[ 1 ] ) { int tmp = a[ 0 ]; a[ 0 ] = a[ 1 ]; a[ 1 ] = tmp; } } ⇒ What are the necessary changes so that the method can be used to sort an array of Time objects? 1) Changing the type of the variables (parameter and local variable) as well as 2) replacing the comparison operator by a method call. public static void sort2( Time a ) { if ( a[ 0 ].after( a[ 1 ] ) ) { Time tmp = a[ 0 ]; a[ 0 ] = a[ 1 ]; a[ 1 ] = tmp; } } What would be the necessary changes so that the method can be used to sort an array of Shapes? 1) Changing the type of the variables (parameter and local variable) and replacing the method that is used to compare two values. public static void sort2( Shape a ) { if ( a[ 0 ].compareTo( a[ 1 ] ) > 0 ) { Shape tmp = a[ 0 ]; a[ 0 ] = a[ 1 ]; a[ 1 ] = tmp; } } What do these algorithms have in common? To sort an array of objects, one simply needs a way to compare two objects. Sorting an array of objects is a task that is likely to occur in a variety of contexts, an array of student objects, bank accounts, transactions, etc. therefore a general, polymorphic, method would be useful. What are the requirements/types/operations? static void sort2( __________ as) { if ( as[ 0 ]._________( as[ 1 ] ) > 0 ) { __________ tmp; tmp = as[ 0 ]; as[ 0 ] = as[ 1 ]; as[ 1 ] = tmp; } } 1. Needs a method for comparing two objects; 2. The name of the method must be the same for all the classes who need to use the sort method; 3. The particular implementation depends on the type of the objects that are stored in the array....
View Full Document

This note was uploaded on 01/06/2011 for the course ITI 1121 taught by Professor Samaan during the Spring '10 term at University of Ottawa.

Page1 / 76

Lect7_interface - ITI 1121. Introduction to Computing II *...

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

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