Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COS 260 DAY 6 Tony Gauvin 1 Agenda • Questions? • 2nd Mini quiz results poor • Assignment 1 Due – All turned in • Assignment 2 will be posted soon – Will include exercises 3.31 & 3.32 (page 81) • Review Class definitions • Begin Object Interaction 2 Class definition revisited • Template for a class definition public class ClassName { fields ... constructors … methods … } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 3 Fields • Also called instance variables accessibility modifier type fieldname ; private int gradeExam1; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 4 Constructors • Special method type has a header and body visibility modifier ClassName ( parameter list) pararmeter type, namePara { assignments statements } 5 Methods • Many types two most common are – Accessors – Mutators • All methods has a header and a body – Method header is also called a method signature since is most be unique within the class definition Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 6 Accessors used to retrieve state information from an object public type methodName() { return a field } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 7 Mutator • Used to change the sate of an object public return_type methodName( parameters) { at least one assignment statement } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 8 Object interaction Creating cooperating objects 5.0 Clock review Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 10 Concepts • • • • abstraction modularization classes define new types class diagram • • • • object diagram object references object types primitive types Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 11 Objects creating objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() { hours = new NumberDisplay(24); minutes = new NumberDisplay(60); … } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 12 Objects creating objects in class ClockDisplay: hours = new NumberDisplay(24); actual parameter in class NumberDisplay: public NumberDisplay(int rollOverLimit); formal parameter Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 13 ClockDisplay object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 14 Method calling public void timeTick() { minutes.increment(); if(minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 15 External method call • external method calls minutes.increment(); object . methodName ( parameter-list ) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 16 Internal method call • internal method calls updateDisplay(); • No variable name is required. • this – could be used as a reference to the invoking object, but not used for method calls. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 17 Internal method /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 18 Method calls • NB: A method call on another object of the same type would be an external call. • ‘Internal’ means ‘this object’. • ‘External’ means ‘any other object’, regardless of its type. 19 null • null is a special value in Java • Object fields are initialized to null by default. • You can test for and assign null: private NumberDisplay hours; if(hours != null) { ... } hours = null; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 21 The debugger • Useful for gaining insights into program behavior … • … whether or not there is a program error. • Set breakpoints. • Examine variables. • Step through code. 22 The debugger 23 Concept summary • object creation • overloading • internal/external method calls • debugger Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 24