Presentation is loading. Please wait.

Presentation is loading. Please wait.

Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.

Similar presentations


Presentation on theme: "Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes."— Presentation transcript:

1 Make Sure You Know All This!

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes Objects and Classes –State fieldsfields –Constructors parametersparameters Initialise the fieldsInitialise the fields –Methods parametersparameters return Values return Values Operate on the fieldsOperate on the fields Class diagrams show if one class refers to another (anywhere in its implementation). Object diagrams show field values (which may refer to specific other objects).

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling World of Zuul class diagram 3

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling notes:ArrayList<String> :String : String Iterator it = notes.iterator(); while (it.hasNext()) { System.out.println (it.next()); } ✔ it:Iterator<String> Reprise... object diagram 4

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A class is a template from which objects can be constructed. A class is a template from which objects can be constructed. A class defines the data fields held by its objects, together with logic for constructors of its objects and methods for accessing and operating on their data. A class defines the data fields held by its objects, together with logic for constructors of its objects and methods for accessing and operating on their data. Methods (and constructors) may declare and use their own local variables – these exist only for the duration of their method (or constructor). On the other hand, data fields last for the duration of their object. Methods (and constructors) may declare and use their own local variables – these exist only for the duration of their method (or constructor). On the other hand, data fields last for the duration of their object. Methods and constructors may be passed parameters – these exist only for the duration of the method or constructor. Methods and constructors may be passed parameters – these exist only for the duration of the method or constructor. 5

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Methods may be classified as: Methods may be classified as: –accessors : these return some value computed from the data fields (often just the value of a field – e.g. getYear() ). They do not change any field values. –mutators : these return nothing (i.e. void ) but they do operate on and change (some) field values. –some are both accessor and mutator. Data fields, parameters and local variables either have class types (i.e. they are references to other objects) or primitive types (e.g. int, float, boolean, char, …). Data fields, parameters and local variables either have class types (i.e. they are references to other objects) or primitive types (e.g. int, float, boolean, char, …). Data fields, constructors and methods may be declared public or private : Data fields, constructors and methods may be declared public or private : –fields should (almost) always be private. –some methods and (most) constructors will be public. –Supporting methods and (some) constructors will be private. 6

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Execution Structures Execution Structures –Assignment col = something.getColour (); col = something.getColour (); –Conditional if (a <= b) {... } if (a <= b) {... } switch (ch) {... } switch (ch) {... } –Looping for (Thing t : allMyThings) {... } for (Thing t : allMyThings) {... } for (int i = 0; i < A.length; i++) {... } for (int i = 0; i < A.length; i++) {... } while (searching && (i < n)) {... }while (searching && (i < n)) {... }* * not taught in this module 7

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Method Invocation Method Invocation –within the same class: checkDayOverflow ();checkDayOverflow (); if (!legalDate ()) {... } if (!legalDate ()) {... } –on objects other than this one: responder.generateResponse (); –on static methods of another class: if (DateStuff.legalDate (day, month, year)) {... }if (DateStuff.legalDate (day, month, year)) {... } Object Construction Object Construction –from the project: –from the Notebook project: notes = new ArrayList (); 8

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Commonly Used Classes Commonly Used Classes –Collections Lists (e.g. ArrayList )Lists (e.g. ArrayList ) Sets (e.g. HashSet )Sets (e.g. HashSet ) Iterators (obtained from the above)Iterators (obtained from the above) Arrays (e.g. Date[366] )Arrays (e.g. Date[366] ) Maps (e.g. HashMap )Maps (e.g. HashMap ) –Others String (e.g. equals() method, + operator) String (e.g. equals() method, + operator) Random (e.g. nextInt(n) method) Random (e.g. nextInt(n) method) System (e.g. static field: System.out ) System (e.g. static field: System.out ) 9

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Coding standards Coding standards –Layout indentationindentation position of { … } sposition of { … } s Spacing (e.g. after commas, around operators, …)Spacing (e.g. after commas, around operators, …) –Documentation class comments (formal javaDoc syntax)class comments (formal javaDoc syntax) method comments (formal javaDoc syntax)method comments (formal javaDoc syntax) local comments (e.g. // single line comments )local comments (e.g. // single line comments ) –Naming class names are nouns (and start with Upper case)class names are nouns (and start with Upper case) method names are verbs (and start with Lower case)method names are verbs (and start with Lower case) fields and variables can be abstract (and start with Lower case)fields and variables can be abstract (and start with Lower case) 10

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Other Things Other Things –Package Libraries standard JDK API ( Google : “Java API”)standard JDK API ( Google : “Java API”) import statements (e.g. java.util.* ) import statements (e.g. java.util.* ) write our own packages (e.g. package co520.stuff; )write our own packages (e.g. package co520.stuff; ) –Declaration Qualifiers public / private (visibility of classes, fields, constructors and methods) public / private (visibility of classes, fields, constructors and methods) static (class ownership of fields and methods) static (class ownership of fields and methods) final (fields initialised once – no re-assignment allowed) final (fields initialised once – no re-assignment allowed)* * not taught in this module 11

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling – Design Issues – Design Issues coupling between classes ( bad )coupling between classes ( bad ) –where the code in one class depends on implementation decisions (e.g. field structure, magic strings) in another. cohesion ( good )cohesion ( good ) –a class should be responsible for one conceptual item. –a method should be responsible for implementing one action. –a method may action several things … but only by invoking other methods that implement them separately. –the set of methods in a class should be complete. Other Things Other Things 12

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling – Design Issues (Continued) – Design Issues (Continued) STREAMSTREAM –write stub methods, with parameters and documentation, and check they compile. –think about testing as you design (may need more methods). –consider alternative representations to hold class information. –evaluate consequences for implementation from each representation. –declare the attributes (fields) for chosen representation. –complete the stub bodies for all the methods:  Mañana principle: defer special cases, hard logic, heavy functionality, nested loops, duplicated code to private methods. »write stubs for these deferred methods, invoke where needed, compile and test (as far as the stub code allows). »declare extra fields (as necessary) and complete the stub bodies for the deferred methods. 13

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling – Design Issues (Continued) – Design Issues (Continued) STREAMSTREAM –write stub methods, with parameters and documentation, and check they compile. –think about testing as you design (may need more methods). –consider alternative representations to hold class information. –evaluate consequences for implementation from each representation. –declare the attributes (fields) for chosen representation. –complete the stub bodies for all the methods:  Mañana principle: defer special cases, hard logic, heavy functionality, nested loops, duplicated code to private methods. »write stubs for these deferred methods, invoke where needed, compile and test (as far as the stub code allows). »declare extra fields (as necessary) and complete the stub bodies for the deferred methods. REFACTORING : be prepared to change your mind about design decisions if working with them (as user or implementer) proves difficult. Expect to make wrong decisions. As projects develop, they will show up. For commercial systems, development (and refactoring) never ends … 14

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling – Design Issues (Continued) – Design Issues (Continued) STREAMSTREAM –write stub methods, with parameters and documentation, and check they compile. –think about testing as you design (may need more methods). –consider alternative representations to hold class information. –evaluate consequences for implementation from each representation. –declare the attributes (fields) for chosen representation. –complete the stub bodies for all the methods:  Mañana principle: defer special cases, hard logic, heavy functionality, nested loops, duplicated code to private methods. »write stubs for these deferred methods, invoke where needed, compile and test (as far as the stub code allows). »declare extra fields (as necessary) and complete the stub bodies for the deferred methods. REFACTORING : be prepared to change your mind about design decisions if working with them (as user or implementer) proves difficult. Expect to make wrong decisions. As projects develop, they will show up. For commercial systems, development (and refactoring) never ends … REFACTORING : when changing requirements (e.g. from a single to a multi player game) force a refactoring of your class structures, do that refactoring – but initially make no changes to functionality. Does the system still perform the same way after this refactoring as before? Only when the answer is yes, introduce the new functionality for which the refactoring was designed. 15

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling – Testing – Testing unit testingunit testing –test each class, and each method of each class, separately. helps! –test each class, and each method of each class, separately. BlueJ helps! regression testingregression testing –build a test suite (more classes) for classes. –as a class is changed (refactored, respecified, extended), re-run the test suite to check nothing has broken. Of course, test suites themselves need to kept up to date. helps automate the building of testing classes. See Chapter 6.4. – BlueJ helps automate the building of testing classes. See Chapter 6.4. manual walkthroughmanual walkthrough –manual walkthrough (read through the code away from any computer, explain your code to others, understand others’ code). See Chapter 6.8. debuggersdebuggers –set breakpoints, add print statements, step execution of code (from breakpoint to breakpoint). See. –set breakpoints, add print statements, step execution of code (from breakpoint to breakpoint). See Chapter 3.12-13, 6.11. Other Things Other Things 16


Download ppt "Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes."

Similar presentations


Ads by Google