******************************************************************************* 0) Last time- how to write an instantiable class - made the Counter class and added some neat functionality to it 1) Administrative - next time will be in the smallest lab, first on right - hw: bring in one or two ideas of programs that you could write for yourself, however crazy, whether you know how to now or not. - go over A0 (grades online, but worth more for thought than points) Bus location: Point2D.Double ID: int {frozen} CAPACITY: int {frozen route: Route isExpress: boolean passengers: List or numPassengers: int Bus() addPassenger() removePassenger() drive() stop() Methods to get or change other data Route color: Color _startHub_: BusStop _stopHub_: BusStop buses: List stops: List expressStops: List Route() addBus() removeBus() addStop() removeStop() Methods to get or change other data Passenger location: Point2D.Double category: String hasTicket: boolean Passenger() arriveStop() leaveStop() boardBus() leaveBus() Methods to get or change other data - notes: * really entertaining: passenger's max capacity, bus location constant * what would it mean to make the route color static? * class names should be singular * capitalization, noun/verb naming, descriptiveness * if you have a method getX() you should have a data member x * most data will probably need methods to get and change it 2) The rest of writing classes A. Official terminology * accessor: method that gets value of a data member ("get") * mutator: method that changes value of a data member * overloaded methods: same name, diff number and/or type of params - why don't visibility and return value matter? * self-referencing pointer: "this" * code duplication: rewriting lines of code unnecessarily - avoid it by re-using what's already written B. The self-referencing pointer in detail * what are the ways you can use it? - in an instance method, to get to instance data: this.x - in an instance method, to get to another method: this.x() - in a constructor, to call another constructor: this() (has to be first line of the constructor) - "this" all by itself is a reference to the object itself * you can't say "this" from a class (static) method- why? - instance methods run "on" objects, but static = separate - could call static method before any instance exists C. Passing and returning objects * pass-by-value means? -value of argument is copied into param * example of passing an object public static void tickTwice(Counter c) { c.tick(); c.tick(); } * rewrite main to use it, draw memory diagram of process - alias: two things point to counter - c is a local variable; what happens when method ends? - object stays b/c someone still points to it * assign counter to null: object is garbage collected * example of returning an object public static Counter makeCounter(int limit) { Counter ctr = new Counter(limit); return ctr; } * rewrite main to use it, draw memory diagram of process - note that you could skip the middle step - reference gets passed to main just before local is erased D. Instantiable main classes * main class just has to have a main method * it can have a constructor, data and methods, too (like BusStop) * can make an object of the main class from inside main method - show by moving Clock functionality into Counter - can be simpler, but sometimes enough for separate main 3) How to design a program with classes - people engage their mouths at different points in the thought process, but you should not engage your keyboard before you've finished the design process - "There is not now and never will be a language in which it is the least bit difficult to write bad programs." -Lawrence Flon - in Java it is difficult to write dangerous programs, but easy to write bad ones, and most of those are bad b/c they don't have a good OO design - this is more of an art than a science (which is why A0 weirded you out a bit) A. Design principles: * delegation and ownership of data in logical places - passengers don't have a max capacity; buses do * abstraction - we don't care how the Counter does the math to tick - code reuse - overloaded Counter constructors call each other - STO principle: one class, one purpose - BufferedReader and InputStreamReader and InputStream 4) Examples: design in groups (handout) - keep the designs so you can program them later - think about how you will program them, but don't do so now *******************************************************************************