CS110- Lecture 15 April 13, 2005 - PowerPoint PPT Presentation

About This Presentation
Title:

CS110- Lecture 15 April 13, 2005

Description:

CS110-Spring 2005, Lecture 15. 2. Comparing Strings. public int ... if obj1 is greater than obj2, return ve. if obj2 is greater than obj1, return ve. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 14
Provided by: Robert1138
Category:
Tags: april | cs110 | lecture | ve

less

Transcript and Presenter's Notes

Title: CS110- Lecture 15 April 13, 2005


1
CS110- Lecture 15April 13, 2005
  • Announcements
  • Project 3 is due tomorrow at 10.45 PM.
  • No labs next week.
  • Agenda
  • Comparing objects
  • Project 3 Questions
  • Interfaces
  • Method Overloading
  • Sorting

2
Comparing Strings
  • public int compareTo(String s) // not boolean
  • hello.compareTo(help!) // -4 (l - p)
  • help!.compareTo(hello) // 4 ( p l)
  • hello.compareTo(hell) // 1 ( 5 - 4)
  • hello.compareTo(hello) // 0
  • The idea is
  • x.compareTo(y) 0 just when x.equals(y)
  • x.compareTo(y) lt 0 when x lt y
  • x.compareTo(y) gt 0 when x gt y
  • compareTo is wonderful for sorting
    (alphabetizing)

3
String compareTo pseudocode
  • March through the character arrays looking for
    first char difference
    (be sure not to run off the end, since
    lengths may differ)
  • if you find a char difference, return it
    (numerically)
  • if no difference when you reach the end of the
    shorter string, return the difference in lengths
    (0 if the same)

4
Comparing Objects
  • The idea is same
  • obj1.compareTo(obj2)
  • if objects are equal, return 0
  • if obj1 is greater than obj2, return ve.
  • if obj2 is greater than obj1, return ve.
  • But how we decide that when objects are equal,
    one object is less than the other or greater than
    the other.
  • We choose our own criteria.

5
Comparing Objects
  • For e.g.
  • A pixel is greater than other pixel if its
    luminance is more
  • A student is less than the other if his/her
    age is less than other student
  • or
  • A student is less than other student if
    his/her name comes alphabetically before the
    other student.

6
Interfaces
  • We have used the term interface to refer to the
    set of public methods through which we can
    interact with one another.
  • We now we are going to formalize this concept
    using a particular language construct in Java
  • A java interface is a collection of constants and
    abstract methods.

7
Abstract Method
  • An abstract method is a method that does not have
    an implementation.
  • There is no body of code defined for an abstract
    method.
  • The header of the method, including its parameter
    list, is simply followed by a semicolon.
  • An abstract method can be preceded by the
    reserved word abstract.
  • An interface cannot be instantiated.

8
Comparable Interface
  • public interface Comparable
  • public int compareTo(Object o)
  • All the methods in interfaces are abstract
    and have public visibility by default.
  • A class implements an interface by providing
    method implementations for each of the abstract
    methods defined in the interface.

9
Interfaces
  • When a class implements an interface
  • the class definition becomes
  • public class AClass implements AnInterface
  • For e.g.
  • public class String implements Comparable
  • public class Pixel implements Comparable
  • public class ImageBase implements ActionListener

10
Comparator Interface
  • public interface Comparator
  • public int compare(Object o1, Object o2)
  • public boolean equals(Object obj)
  • Any class that will implement this Interface
    should ideally provide implementation for these
    method. But sometimes we dont provide
    implementation of equals method (use equals of
    Object class)

11
Interfaces
  • Cannot instantiate an interface
  • Cannot do the following
  • // cannot instantiate Comparable class
  • // since it is not a concrete class
  • Comparable c new Comparable()
  • However we can do the following
  • Comparable c new String()

12
Method Overloading
  • Often the method name is sufficient to indicate
    which method is being called by a specific
    invocation.
  • But we can use the same method name with
    different parameters lists for multiple methods.
    This technique is known as method overloading.

13
Method Overloading
  • So a method name can be used again as long as the
    number of parameters, the type of those
    parameters, and/or the order of the types of
    parameters are different.
  • A methods name along with the number, type and
    order of its parameters is called the methods
    signature.
Write a Comment
User Comments (0)
About PowerShow.com