Presentation is loading. Please wait.

Presentation is loading. Please wait.

COS 260 DAY 6 Tony Gauvin.

Similar presentations


Presentation on theme: "COS 260 DAY 6 Tony Gauvin."— Presentation transcript:

1 COS 260 DAY 6 Tony Gauvin

2 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

3 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

4 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

5 Constructors Special method type has a header and body
visibility modifier ClassName ( parameter list) pararmeter  type, namePara { assignments statements }

6 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

7 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

8 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

9 Objects First with Java Creating cooperating objects
Object interaction Creating cooperating objects 5.0 © David J. Barnes and Michael Kölling

10 Clock review Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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.

20 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

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 The debugger Useful for gaining insights into program behavior …
… whether or not there is a program error. Set breakpoints. Examine variables. Step through code.

23 The debugger

24 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


Download ppt "COS 260 DAY 6 Tony Gauvin."

Similar presentations


Ads by Google