COS 260 DAY 6 Tony Gauvin
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
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
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
Constructors Special method type has a header and body visibility modifier ClassName ( parameter list) pararmeter type, namePara { assignments statements }
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
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
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
Objects First with Java Creating cooperating objects Object interaction Creating cooperating objects 5.0 © David J. Barnes and Michael Kölling
Clock review Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
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
Objects creating objects Objects First with Java 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 © David J. Barnes and Michael Kölling
Objects creating objects Objects First with Java Objects creating objects hours = new NumberDisplay(24); in class ClockDisplay: actual parameter public NumberDisplay(int rollOverLimit); in class NumberDisplay: formal parameter Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling
ClockDisplay object diagram Objects First with Java ClockDisplay object diagram Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling
Objects First with Java 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 © David J. Barnes and Michael Kölling
Objects First with Java External method call external method calls minutes.increment(); object . methodName ( parameter-list ) Dot notation Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling
Objects First with Java 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 © David J. Barnes and Michael Kölling
Objects First with Java 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 © David J. Barnes and Michael Kölling
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.
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
The debugger Useful for gaining insights into program behavior … … whether or not there is a program error. Set breakpoints. Examine variables. Step through code.
The debugger
Objects First with Java 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 © David J. Barnes and Michael Kölling