Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.

Slides:



Advertisements
Similar presentations
Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Advertisements

Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
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.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
Comparable and Comparator Nuts and Bolts. 2 Nuts and bolts Four methods underlie many of Java’s important Collection types: equals, compare and compareTo,
Comparable and Comparator. Outline of the Student class import java.util.*; public class Student implements Comparable { public Student(String name, int.
1 Lecture 08(API)Lecture 11 Java API and Interfaces l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Unit 301 Java Collections Framework: Classes I JCF Class Hierarchy The ArrayList Class The Collections Class.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
1 Search & Sorting Using Java API Searching and Sorting Using Standard Classes  Searching data other than primitive type.  Comparable interface.  Implementing.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Introduction to Searching and Sorting
Comparable and Comparator. 2 Comparing our own objects The Object class provides public boolean equals(Object obj) and public int hashCode() methods –For.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 10 Sorting and Searching.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Chapter 9 Searching and Sorting
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 18 Java Collections Framework
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Comparison-Based Sorting & Analysis Smt Genap
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Interfaces and Inner Classes
Searching and Sorting 14ACEHRPT Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Searching.
Chapter 7 User-Defined Methods.
An Introduction to Java – Part I
Introduction to Searching and Sorting
Comparable and Comparator
null, true, and false are also reserved.
CMSC 202 Interfaces.
Comparable and Comparator
Sorting Chapter 8.
Searching.
Creating and Modifying Text part 3
CMSC 202 Interfaces.
Presentation transcript:

Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises

Unit 262 The Comparable Interface The Comparable interface of java.lang public interface Comparable{ public abstract int compareTo(Object object); } This interface may be used to order objects of a class that have a natural ordering. Several core Java classes implement Comparable. A user defined class that implements Comparable should implement the compareTo method such that : object1.compareTo(object2) is: 0if object1 “is equal to” object2 > 0if object1 “is greater than” object2 < 0if object1 “is less than” object2

Unit 263 The Comparable Interface (cont’d) It is also preferable for object1.compareTo(object2) to return 0 if and only if object1.equals(object2) is true. The compareTo method throws a ClassCastException if the type of this object and the type of the object passed as parameter are not compatible for comparison. Example: 1. public class BankAccount implements Comparable{ 2. private int accountNumber; 3. // public int compareTo(Object object){ 5. BankAccount account = (BankAccount) object; 6. if(accountNumber < account.accountNumber) 7. return –; 8. else if(accountNumber == account.accountNumber) 9. return 0; 10. else 11. return 1; 12. } 13. }

Unit 264 The Comparable Interface (cont’d) Example: Assuming that account1 and account2 are BankAccount objects, a typical call to the compareTo method is: 1. int comparisonResult = account1.compareTo(account2); 2. if(comparisonResult == 0) 3. System.out.println(“Same account”); 4. else 5. System.out.println(“Different accounts”); To define an ordering for objects that do not implement Comparable, or to define an ordering other than the natural ordering defined by Comparable, the java.util.Comparator interface should be used.

Unit 265 The Comparator Interface The Java collection framework is a set of important utility classes and interfaces in the java.util package for working with collections. A collection is a group of objects. The Comparator interface is one of the collections framework interfaces. Comparator defines how collection objects are compared. public interface Comparator{ public abstract int compare(Object object1, Object object2); public abstract boolean equals(Object object); } The Comparator interface is used when: the collection objects to be ordered do not have a natural ordering defined by the Comparable interface the objects are to be ordered by not using their natural ordering.

Unit 266 The Comparator Interface (cont’d) A class that implements Comparator should implement the compare method such that its return value is: 0if object1 “is equal to” object2 > 0if object1 “is greater than” object2 < 0if object1 “is less than” object2 It is also preferable for the compare method to return 0 if and only if object1.equals(object2) is true. The compare method throws a ClassCastException if the type of object1 and that of object2 are not compatible for comparison. The equals method returns true if its parameter is a Comparator object and if it uses the same ordering as the invoking Comparator object; otherwise it returns false.

Unit 267 The Comparator Interface (cont’d) Note: Since each class inherits the equals method from the Object class, it is not necessary for a class that implements the Comparator interface to implement the equals method. Example: The reverse Comparator for strings 1. import java.util.*; 2. public class StringReverseComparator implements Comparator{ 3. public int compare(Object object1, Object object2){ 4. String string1 = object1.toString(); 5. String string2 = object2.toString(); 6. // Reverse the comparison 7. return string2.compareTo(string1); 8. } 9. }

Unit 268 The Comparator Interface (cont’d) import java.util.*; class BankAccount implements Comparable{ private int accountNumber; protected String name; private double balance; //... public int compareTo(Object object){ BankAccount account = (BankAccount) object; if(accountNumber < account.accountNumber) return -1; else if(accountNumber == account.accountNumber) return 0; else return 1; } public String toString(){ return "Account#: " + accountNumber + ", Name: " + name + ", Balance: " + balance + " SR"; }

Unit 269 Example 1: The Comparator Interface 1 import java.util.*; 2 class MyComparator implements Comparator { 3 public int compare(Object obj1, Object obj2) { 4 int i1 = ((Integer)obj1).intValue(); 5 int i2 = ((Integer)obj2).intValue(); 6 return Math.abs(i2) - Math.abs(i1); 7 } 8 } 9 public class TestCollections { 10 public static void main(String args[]) { 11 ArrayList array = new ArrayList(); 12 array.add(new Integer(-200)); 13 array.add(new Integer(100)); 14 array.add(new Integer(400)); 15 array.add(new Integer(-300)); 16 Collections.sort(array); 17 System.out.println("Natural ordering: " + array); 18 Collections.sort(array, new MyComparator()); 19 System.out.println("My own ordering : " + array); 20 } 21 }

Unit 2610 The Comparator Interface (cont’d) 1 A Comparator for BankAccount objects 2 class MyComparator implements Comparator{ 3 public int compare(Object object1, Object object2){ 4 BankAccount account1 = (BankAccount) object1; 5BankAccount account2 = (BankAccount)object2; 6String string1 = account1.name; 7String string2 = account2.name; 8// Reverse the comparison 9 return string2.compareTo(string1); 10 } 11 }

Unit 2611 Algorithm Complexity Classes Different algorithms require different amount of running time and space. The less amount of running time the more time-efficient the algorithm. The less amount of space requirements the more space-efficient the algorithm. The resources (such as time and space) required to solve a problem usually increase with an increase in the size n of the problem. Several factors affect the time and space requirements of an algorithm: hardware, language of implementation, Compiler being used, etc. The average running time of an algorithm is a function f(n) of the problem size n. Algorithms are classified into different groups (called complexity classes) based on f(n)

Unit 2612 Algorithm Complexity Classes (cont’d)

Unit 2613 Exercises 1. Write a Comparator to compare names by last name. 2. Write a Comparator to compare names by middle name. Assume that each name to be compared, consists of three names: first, middle, and last. 3. Arrange algorithm complexity classes from the most time-efficient to the least time-efficient.