©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 11 - 1 Chapter 11 Searching and Sorting with Objects.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 11 Sorting and Searching.
Advertisements

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.
Written by: Dr. JJ Shepherd
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
©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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Searching and Sorting I 1 Searching and Sorting 1.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Polymorphism.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Introduction to Searching and Sorting
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © The McGraw-Hill Companies, Inc
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
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.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting.
CS1101: Programming Methodology
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Written by: Dr. JJ Shepherd
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Three Statics of Structures Reactions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Searching and Sorting
Programming in C# Comparison (Sorting)
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Searching and Sorting with Objects Comparable Interface Comparator objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching Objects Similar to searching primitive arrays except you have several choices –Search for a particular object (by reference) in the array Compare using == –Search for a single object with a particular property or group of properties Match all the properties of a particular object using equals method Use accessor methods to get appropriate data for comparison –Search for all objects that meet search criterion Return an array (or collection) instead of a single object

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sorting Objects In order to sort objects of a particular type, we need a way to compare them –The relational operators ( = >) do not work with objects Two approaches to object comparison –Comparable interface The object class must implement the Comparable interface Class must define the method int compareTo( o) –Comparator interface Create a new class which implements the Comparator interface Need to define two methods int compare( o1, o2) boolean equals( Object o) // compare two Comparators

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Comparable Interface Any class that implements Comparable must contain the method int compareTo( o) Since it is inside the class, compareTo has access to all instance variables The Arrays class has a sort method that sorts Comparable objects –public static void sort(Comparable[] a)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Semantics of compareTo o1.compareTo( o2) returns –negative number if o1 comes before o2 –zero if o1 and o2 are the same –positive number if o1 comes after o2 For Strings –"abc".compareTo("bcd") is negative –"abc".compareTo("ABC") is positive –"abc".compareTo("abc") is 0

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Comparator Interface Create a separate class whose sole purpose is to provide a comparison method int compare( o1, o2) Since it is a separate class, compareTo has to use accessor methods to get instance data The Arrays class has a sort method that sorts Comparable objects public static void sort(T[] a, Comparator c)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Semantics of compare ComparatorClass.compare ( o1, o2) returns –negative number if o1 comes before o2 –zero if o1 and o2 are the same –positive number if o1 comes after o2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Comparing Person Objects We define the Person class so we can compare Person objects by name or by age

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Comparing Person Objects First, we need to determine how to compare Person objects The following does not make sense: Person p1 = …; Person p2 = …; if ( p1 < p2 ) { … }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Choosing an attribute to compare Modify the Person class to include a static variable that tells which attribute to compare. class Person implements Comparable { … public static final int NAME = 0; public static final int AGE = 1; public static int compareAttribute = NAME; … public static void setCompareAttribute(int attribute) { compareAttribute = attribute; public int compareTo( Object person){ …} } … }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The compareTo Method To compare Person objects, first set the comparison attribute and then call the compareTo Method. Person.setCompareAttribute (Person.NAME); int compResult = p1.compareTo(p2); if (compResult < 0) { //p1 “less than” p2 } else if (compResult == 0) { //p1 “equals” pw } else { //p2 “greater than” p2 } public int compareTo(Person p) { int compResult; if ( comparisonAttribute == AGE ) { int p2age = p.getAge(); if ( this.age < p2age ) { compResult = LESS; } … } else { //compare Name String p2Name = p.getName(); compResult = this.name. compareTo(p2Name); } return compResult; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using Comparators Implement the Comparator interface. We can define the implementation class so we can compare Person objects using any combination of their attributes –For example, we can define a comparator that compares Person objects by using both name and age

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The AgeComparator Class This class compares the age attributes of Person objects class AgeComparator implements Comparator { private final int LESS = -1; private final int EQUAL = 0; private final int MORE = 1; public int compare(Object p1, Object p2) { int comparisonResult; int p1age = ((Person)p1).getAge( ); int p2age = ((Person)p2).getAge( ); if (p1age < p2age) { comparisonResult = LESS; } else if (p1age == p2age) { comparisonResult = EQUAL; } else { assert p1age > p2age; comparisonResult = MORE; } return comparisonResult; }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The NameComparator Class This class compares the age attributes of Person objects Because the name attribute is a string we can use the compareTo method of the String class class NameComparator implements Comparator { public int compare(Object p1, Object p2) { String p1name = ((Person)p1).getName( ); String p2name = ((Person)p2).getName( ); return p1name.compareTo(p2name); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The sort Method We use the sort method of the java.util.Arrays class public Person[ ] sort ( int attribute ) { if (!(attribute == Person.NAME || attribute == Person.AGE) ) { throw new IllegalArgumentException( ); } Person[ ] sortedList = new Person[ count ]; //copy references to sortedList for (int i = 0; i < count; i++) { sortedList[i] = entry[i]; } Arrays.sort(sortedList, getComparator(attribute)); return sortedList; }