CS 100, Summer 2001 Monday, 7/30 Lecture 18 ----------------------------------------------------------------------- Announcements: + Exercise 13 due 7/31 + Pick up graded Exercise 12 and Project 1 tomorrow + reading: Savitch 5.7, 7.3 ----------------------------------------------------------------------- Topics: + Project 2 + Inheritance (constructors, method overriding) + Variable shadowing ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Project 2 - Bridge: four players, 13 cards each - valuing hands - strength points: A4, K3, Q2, J1 - distribution points: void 3, singleton 2, doubleton 1 - this helps you decide how good your hand is - note: after doing four, need to reshuffle the deck and redeal. - What is the best bridge hand you can think of, in terms of our valuation rules? - A-Q in three suits, A-J in the fourth: 37 points - What is the worst bridge hand? - three non-face cards in three suits, four non-face in the last suit: 0 points - with this in mind, we can then sort bridge hands - Sorting: insertion, selection, bubble - we've seen sorting routines for sorting arrays of integers, but what if we want to sort something more interesting? - could write a sort() method for every class we encounter (yuck!) - instead, write one sort method for Comparable objects. - Comparable interface, compareTo() method (see Appendix 7) - if a class "implements" Comparable, then it has a compareTo() method - then we write sort() to take in Comparables and use their compareTo() method to sort them - all in the same way! (insertionSort() has been done in this way for you) - thus, you'll implement a compareTo() for bridge hands. compareTo takes in an Object, casts it to whatever the appropriate type is, and then returns -1 if the current object is "less than" the input object, 0 if they are equivalent, and 1 if the current object is "greater than". (the Card class is also Comparable, so look at it for a good example) - Analysis (MATLAB) - some sorting algorithms are faster than others. How can we tell which is the best? - for this project, count how many times compareTo() gets called by the sorting algorithm. - you'll run a bunch of sorting experiments, then output how big the array was and how many comparisons were required to sort it. - then, plot() this in MATLAB to see how the number of comparisons grows with the size of the array being sorted. - what do we expect? - linear graph O(n) - quadratic graph O(n^2) - cubic O(n^3) - exponential O(e^n) ---------------------------------------------------------------------- ---------------------------------------------------------------------- + Inheritance: classes can 'inherit' from other classes, to allow reuse of code. They then specialize the base class by adding more variables or methods, or overriding methods. - base class: the original class (also, "parent" class) - derived class: the class that "inherits" from or "extends" the base class (also, "child" class) - The derived class only needs to specify the additional variables and/or methods; the inherited ones are automatically copied + What does inheritance mean for the child class? - Each object of the child class is ALSO of the parent class's type - Thus if a method takes in (Parent p) - you can call it with (Child c) - no typecast is required - EXAMPLE: compareTo(Card c) will work, even though the method is written to take in an Object + Assignment - You can assign an object of a derived class to a variable of any ancestor class, but not the other way around - EXAMPLE: Inherit2.java + Constructors - use super(): It must come first in your constructor. If you leave it out, Java will call the default constructor for the superclass. - EXAMPLE: See constructors for Conifer and Deciduous - another special constructor is this(). It calls a constructor in the SAME class (not a superclass) with the specified arguments. - EXAMPLE: public Spaceship() { this(5, 5); } public Spaceship(int x, int y) { pos = new Position(x, y); } + Overriding methods - If subclass has a method with the same name and input args as a method in the superclass, the subclass's method will override the superclass's method - EXAMPLE: toString() is a method in the Object class that we regularly override with our own (nice) output methods - EXAMPLE: water() is overridden by both Conifer and Deciduous - Sometimes you'll want to call both the superclass's method and the overriding version; use super.method() to do this. - EXAMPLE: graphics programs inherit from JFrame and override the paint() method. Remember how we called super.paint()? - EXAMPLE: toString() in Conifer and Deciduous - Overriding is NOT the same as overloading + Variable shadowing - Remember scoping? (e.g. Problem 1 from prelim 2) - EXAMPLE: Problem1.java - what if we had a "global" int i = 0? It never gets modified or accessed in this program, because the "local" i variables will "shadow" (hide) the global one. - Declaring a new variable in a child class with the same name as a variable in the parent class means the new variable will shadow the old one. To get at the old one, use super. - EXAMPLE: Inherit3.java ---------------------------------------------------------------------- ---------------------------------------------------------------------- PICK UP handed out Exercise 13