fundamentals of software development 1slide 1 recap: key ideas in wordgames defining a classdefining...

10
Fundamentals of Softw are Development 1 Slide 1 Recap: Key ideas in Recap: Key ideas in WordGames WordGames Defining Defining a class a class Extending Extending a class a class versus versus implementing implementing an interface an interface Classes Classes versus versus instances instances The use of The use of fields fields Methods Methods , , parameters parameters and and local variables local variables Blocks Blocks and and scope scope Constructors Constructors E.g., to initialize E.g., to initialize fields fields We will review these ideas in the next several slides Declaring variables Declaring variables Expressions Expressions Statements Statements Assignment Assignment Conditionals Conditionals Loops Loops Blocks Blocks First these ideas Later: these ideas

Upload: claude-todd

Post on 28-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 1

Recap: Key ideas in Recap: Key ideas in WordGamesWordGames

• DefiningDefining a classa class• ExtendingExtending a classa class

versus versus implementing implementing an interfacean interface

• ClassesClasses versus versus instancesinstances

• The use of The use of fieldsfields• MethodsMethods, ,

parameters parameters andand local variableslocal variables

• BlocksBlocks and and scopescope• ConstructorsConstructors

– E.g., to initialize fieldsE.g., to initialize fieldsWe will review these ideas in the next several slides

• Declaring variablesDeclaring variables• ExpressionsExpressions• StatementsStatements

– AssignmentAssignment– ConditionalsConditionals– LoopsLoops– BlocksBlocks

First these ideas

Later: these ideas

Page 2: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 2

DefininDefiningga classa class• Step 1: Add the class to the projectStep 1: Add the class to the project

• Step 2: Implement the classStep 2: Implement the class– Step 2a: Write the JavadocStep 2a: Write the Javadoc

specifications with specifications with stubsstubs– Step 2b: Convert stubsStep 2b: Convert stubs

to the to the actual codeactual code• Obey Sun’s code conventionsObey Sun’s code conventions

• Step 3: Test the classStep 3: Test the class– For larger classes, repeat steps 2 and 3For larger classes, repeat steps 2 and 3

several times, implementing moreseveral times, implementing moreof the class each timeof the class each time

Questions?Do you understand what a stub is, and why it is useful?

Page 3: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 3

Extending a ClassExtending a ClassImplementing an InterfaceImplementing an Interface

• When a class When a class extends extends another another classclass::– it inherits all it inherits all

the the functionality functionality of the other of the other classclass

• When a class When a class implements implements an interfacean interface::– it must it must

supply the supply the methods methods specified by specified by the interfacethe interface

• A Pawn class might A Pawn class might extend extend ChessPieceChessPiece– So your Pawn object can do all the things So your Pawn object can do all the things

that a ChessPiece can do:that a ChessPiece can do:– Such as Such as leavePositionleavePosition and and goToPositiongoToPosition

– Can do some things special to the Pawn Can do some things special to the Pawn itselfitself– Such as rules governing how a Pawn Such as rules governing how a Pawn

movesmoves• A ChessBoard class might A ChessBoard class might implementimplement

a MouseListenera MouseListener – So a ChessBoard So a ChessBoard mustmust implement implement

methods such as:methods such as:• mouseClickedmouseClicked and and mouseEnteredmouseEntered• that specify how objects of that specify how objects of youryour class class

respond to those eventsrespond to those events– This This contractcontract ensures a successful ensures a successful

““interfaceinterface” between your code and the ” between your code and the system’s control of the mousesystem’s control of the mouseQuestio

ns?

Page 4: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 4

ClassesClasses versus versus InstancesInstances• ClassesClasses are like recipes for making are like recipes for making instancesinstances of of

the class.the class.– The The instancesinstances are called are called objectsobjects– In general, there can be many instances of any given classIn general, there can be many instances of any given class– Classes themselves are objectsClasses themselves are objects

• They are instances of the They are instances of the ClassClass class class

• Exercise:Exercise:– What class did you write for WordGames, Part 1?What class did you write for WordGames, Part 1?

• Answer: CapitalizerAnswer: Capitalizer– When you tested your class, about how many instances of When you tested your class, about how many instances of

that class did you create?that class did you create?• Answer: Several (one for each time you pressed the Answer: Several (one for each time you pressed the

“Capitalizer” button)“Capitalizer” button)– In what ways are those instances the same? different?In what ways are those instances the same? different?

• Answer: Same Answer: Same transformtransform rule, but applied to different rule, but applied to different Strings sent to Strings sent to transformtransform (hence different returned values) (hence different returned values)

Page 5: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 5

ClassesClasses versus versus Instances: Instances: ExampleExample

• Example:Example:– DogDog classclass specifies: specifies:

• Attributes (data)Attributes (data): : typically stored in typically stored in fieldsfields - a Dog has breed, weight, age, ... - a Dog has breed, weight, age, ...

• Operations (behavior)Operations (behavior): : defined in defined in methodsmethods - a Dog eats, fetches, sleeps, ... - a Dog eats, fetches, sleeps, ...

– DogDog instanceinstance is an is an objectobject that is a that is a particular Dogparticular Dog• It has a particular breed, etc. It eats, etc.It has a particular breed, etc. It eats, etc.

Questions?

Page 6: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 6

FieldsFields

• A A fieldfield is is datadata associated with an object associated with an object (particular instance)(particular instance)– Example: the name that a NameDropper dropsExample: the name that a NameDropper drops

• Fields are:Fields are:– PersistentPersistent

• They live as long as the object livesThey live as long as the object lives– Global to the classGlobal to the class

• They can be referenced anywhere inside the class They can be referenced anywhere inside the class definitiondefinition

– Unique to the instanceUnique to the instance• Each instance has its own version of the fieldEach instance has its own version of the field• Contrast this with Contrast this with methodsmethods, which are shared , which are shared

among all instances (the same copy of the method is among all instances (the same copy of the method is used by all instances of the class)used by all instances of the class)

Fields versus parameters versus local variables: next slide

Page 7: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 7

FieldsFields, , parametersparameters, , local local variablesvariablespublic class NameDropperExclaimpublic class NameDropperExclaim

extends StringTransformer extends StringTransformer implements StringTransformable { implements StringTransformable {

private String private String namename;;

public String transform(String public String transform(String whatToSaywhatToSay) {) { char char exclamatoryMarkexclamatoryMark = = ''!!'';; return return this.this.namename + + "" says says " "

+ + whatToSaywhatToSay ++ exclamatoryMarkexclamatoryMark;; }}}}

name field exclamatoryMark local variablewhatToSay parameter

Each NameDropperExclaim instance has its own name field (but all the instances share the same transform method) The name field persists as long as the

NameDropperExclaim instance lives

The name field can be used anywhere within the class

We write this.name to emphasize that it is THIS instance’s name. Adopt this habit! (More on “this” later.)

Questions?

Page 8: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 8

ScopeScopepublic class NameDropperExclaimpublic class NameDropperExclaim

extends StringTransformer extends StringTransformer implements StringTransformable { implements StringTransformable {

private String private String namename;;

public String transform(String public String transform(String whatToSaywhatToSay) {) { char char exclamatoryMarkexclamatoryMark = = ''!!'';; return return this.this.namename + + "" says says " "

+ + whatToSaywhatToSay ++ exclamatoryMarkexclamatoryMark;; }}}}

name field’s scope

exclamatoryMark local variable’s scope

whatToSay parameter’s scope

Questions?

Page 9: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 9

ConstructorsConstructors

public class NameDropperExclaim extends StringTransformerpublic class NameDropperExclaim extends StringTransformer implements StringTransformable { implements StringTransformable {

private String name;private String name;

public NameDropperExclaim() {public NameDropperExclaim() { }}

public NameDropperExclaim(String givenName) {public NameDropperExclaim(String givenName) { this.name = givenName;this.name = givenName; }}

public String transform(String public String transform(String whatToSaywhatToSay) {) { char char exclamatoryMarkexclamatoryMark = = ''!!'';; return return this.this.namename + + "" says says " "

+ + whatToSaywhatToSay ++ exclamatoryMarkexclamatoryMark;; }}}}

The NameDropperExclaim class (previous slide) has a problem. What is the problem?

Answer: it uses the name field but never gives it a value!What is the fix?

This constructor can be used to initialize the name field

The “do-nothing” constructor is created for you if you don’t create a constructor yourself

Questions?

Page 10: Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending

Fundamentals of Software Development 1

Slide 10

Recap/Summary:Recap/Summary:Class: Fields, Constructors, Class: Fields, Constructors,

MethodsMethods

public class NameDropper extends StringTransformerpublic class NameDropper extends StringTransformer implements StringTransformable { implements StringTransformable {

private String name;private String name; public NameDropper(String whatMyNameIs) {public NameDropper(String whatMyNameIs) { this.name = whatMyNameIs;this.name = whatMyNameIs; }} public String transform(String whatToSay) {public String transform(String whatToSay) { return this.name + return this.name + "" says says "" + whatToSay; + whatToSay; }}}} Questio

ns?Review these ideas now by doing Fields, Constructors and Methods – review activity

Class declaration

Keywords for extension & interface

Field(s)

Method(s) Constructor(s)