{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lect7_interface

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

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

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.

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

View Full Document
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)

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

View Full Document
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?

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

View Full Document
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?

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

View Full Document
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; } }

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern