Using interfaces 3.0. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.

Slides:



Advertisements
Similar presentations
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Computer Science 209 Software Development Equality and Comparisons.
Fields, Constructors, Methods
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
Further abstraction techniques Abstract classes and interfaces 5.0.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Grouping Objects 3 Iterators, collections and the while loop.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Objects First with Java A Practical Introduction using BlueJ
String Concatenation (operator overloading) 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Object interaction Creating cooperating objects 3.0.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Object Interaction 1 Creating cooperating objects.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More about inheritance Exploring polymorphism 5.0.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
Further abstraction techniques Abstract classes and interfaces 3.0.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Programming in C# Comparison (Sorting)
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Collections and iterators
Objects First with Java Creating cooperating objects
Objects First with Java Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Further abstraction techniques
Improving structure with inheritance
Presentation transcript:

Using interfaces 3.0

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum element in a collection? Could this code be generalised? How would you sort a collection? Could this code be generalised? What type would such a method have?

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using the Collections Class The collections class provides methods that can be used on any collection: Lists and Sets Can be used for –finding max –sorting –searching –lots of other things as well

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using max The signature of the max method is public static > T max(Collection coll) In other words, T must implement the Comparable interface

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Interfaces An interface is a list of method signatures It can be thought of as a “completely abstract type”: no implementation detail Can use interface names as types Good for separate compilation

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Comparable Interface public interface Comparable { int compareTo(T o); } Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Writing compareTo Write compareTo methods for the following classes Player with an int field goalScored Club with an int field points and an int field goalDifference

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using max etc Having defined the compareTo method adapt the class declaration public class Player implements Comparable{ }

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public class Club{ ArrayList squad; //… public Player topScorer(){ return Collections.max (squad); }

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Different sorting criteria You may need to sort things in different ways E.g. different people would have a favourite present Can use an alternative max (and sort etc) with the Comparator interface

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public interface Comparator { int compare(T o1,T o2); boolean equals(Object obj); } Could always use equals from Object: nothing to do

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Pass the comparator object into a Collections method to use it e.g. public static void sort (List list, Comparator c) N.B. List is also an interface, implemented by ArrayList