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.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Improving structure with inheritance
Road Map Introduction to object oriented programming. Classes
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Grouping Objects 3 Iterators, collections and the while loop.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Understanding class definitions Looking inside classes 3.0.
Testing and Debugging. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling (Reminder) Zuul Assignment Two.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
More Sophisticated Behaviour 2 Using library classes to implement more advanced functionality. Also … class ( static ) fields.
Grouping Objects 1 Introduction to Collections.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Understanding class definitions Looking inside classes.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
The Java Programming Language
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Understanding class definitions
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Testing. Have you contacted your project members lately?
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Written by: Dr. JJ Shepherd
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Chapter VII: Arrays.
More Sophisticated Behavior
Chapter 3: Using Methods, Classes, and Objects
More sophisticated behavior
Understanding class definitions
Collections and iterators
Improving structure with inheritance
Collections and iterators
Corresponds with Chapter 5
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 , Other Things Other Things 16