Introduction to Searching and Sorting

Slides:



Advertisements
Similar presentations
Java Programming Abstract classes and Interfaces.
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 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching with objects.
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,
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.
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.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting with Objects.
Unit 301 Java Collections Framework: Classes I JCF Class Hierarchy The ArrayList Class The Collections Class.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
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.
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
Comparable and Comparator. 2 Comparing our own objects The Object class provides public boolean equals(Object obj) and public int hashCode() methods –For.
CS102--Object Oriented Programming Lecture 15: Interfaces Copyright © 2008 Xiaoyan Li.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
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.
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.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
© 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.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Comparison-Based Sorting & Analysis Smt Genap
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
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.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
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?
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.
The Java Tutorials Comparable and Comparator. Object Ordering A List may be sorted as follows. Collections.sort(l); If the List consists of String elements,
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 CSE 331 Comparing objects; Comparable, compareTo, and Comparator slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
More constructors 1. Constructors  recall that a public constructor is what a client uses to create an object  the purpose of a constructor is to initialize.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
The Methods and What You Need to Know for the AP Exam
Introduction to Searching and Sorting
Comparable and Comparator Interfaces
Comparable and Comparator
CMSC 202 Interfaces.
The compareTo interface
Comparable and Comparator
Interfaces and Inner Classes
Comparable and Comparator Interfaces
CMSC 202 Interfaces.
Lexical Ordering and Sorting
CMSC 202 Interfaces.
Comparable and Comparator Interfaces
Presentation transcript:

Introduction to Searching and Sorting Comparable Interface -Reading p. 717-718 Comparator Interface

The Comparable Interface The Comparable interface is in the java.lang package, and so is automatically available to any program It has only the following method heading that must be implemented: public int compareTo(Object other); It is the programmer's responsibility to follow the semantics of the Comparable interface when implementing it

The Comparable Interface Semantics The method compareTo must return A negative number if the calling object "comes before" the parameter other A zero if the calling object "equals" the parameter other A positive number if the calling object "comes after" the parameter other If the parameter other is not of the same type as the class being defined, then a ClassCastException should be thrown

The Comparable Interface Semantics Almost any reasonable notion of "comes before" is acceptable In particular, all of the standard less-than relations on numbers and lexicographic ordering on strings are suitable The relationship "comes after" is just the reverse of "comes before"

The Comparable Interface Several core Java classes implement Comparable interface. It is also preferable for object1.compareTo(object2) to return 0 if and only if object1.equals(object2) is true.

The Comparable Interface (cont’d) Example 1: A BankAccount defining the natural ordering as the ascending order of account numbers. import java.util.*;   class BankAccount implements Comparable { private int accountNumber; private String name; private double balance;

The Comparable Interface (cont’d) 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; }

The Comparable Interface (cont’d) Assuming that account1 and account2 are BankAccount objects, a typical call to the compareTo method is: int comparisonResult = account1.compareTo(account2); if(comparisonResult == 0) System.out.println(“Same account”); else if (comparisonResult < 0) System.out.println(“acountNumber1 is smaller”); else System.out.println(“accountNumber2 is smaller”);

The Comparator Interface If we want to sort objects of a class which does not implement Comparable interface, or the class implements Comparable but we want To order its objects in a way different from the natural ordering defined by Comparable, the java.util.Comparator interface should be used. The Comparator interface is one of the java collections framework interfaces.

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. Comparator interface defines how collection objects are compared.

The Comparator Interface public interface Comparator { int compare(Object object1, Object object2); boolean equals(Object object); } A class that implements Comparator should implement the compare method such that its returned value is: 0 if object1 “is equal to” object2 > 0 if object1 “is greater than” object2 < 0 if object1 “is less than” object2

The Comparator Interface (cont’d) 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 Comparator Interface (cont’d) Example 2: This example sorts the strings in reverse order of the alphabetical one. import java.util.*; class StringReverseComparator implements Comparator { public int compare(Object object1, Object object2) String string1 = object1.toString(); String string2 = object2.toString(); // Reverse the comparison return string2.compareTo(string1); }

The Comparator Interface (cont’d) class Test { public static void main(String[] args) String[] array = {"Ahmad","Mohammad","Ali","Hisham","Omar", "Bilal","Hassan"}; Arrays.sort(array, new StringReverseComparator()); System.out.println(Arrays.asList(array)); }

The Comparator Interface (cont’d) -The sort method ,in the Arrays class, sorts the array “array” according to the comparator object. Notice the comparator object is provided as a parameter for the sorting method; it is an object from the class StringReverseComparator . After printing, we get the following order: [Omar, Mohammad, Hisham, Hassan, Bilal, Ali, Ahmad]