Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending."— Presentation transcript:

1 Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending a class versus implementing an interface Classes versus instancesClasses versus instances The use of fieldsThe use of fields Methods, parameters and local variablesMethods, parameters and local variables Blocks and scopeBlocks and scope ConstructorsConstructors –E.g., to initialize fields We will review these ideas in the next several slides Declaring variablesDeclaring variables ExpressionsExpressions StatementsStatements –Assignment –Conditionals –Loops –Blocks First these ideas Later: these ideas

2 Fundamentals of Software Development 1Slide 2 Defining a 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 Javadoc specifications with stubs –Step 2b: Convert stubs to the actual 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 3 several times, implementing more of the class each time Questions? Do you understand what a stub is, and why it is useful?

3 Fundamentals of Software Development 1Slide 3 Extending a Class Implementing an Interface When a class extends another class:When a class extends another class: –it inherits all the functionality of the other class When a class implements an interface:When a class implements an interface: –it must supply the methods specified by the interface A Pawn class might extend ChessPiece –So your Pawn object can do all the things that a ChessPiece can do: –Such as leavePosition and goToPosition –Can do some things special to the Pawn itself –Such as rules governing how a Pawn moves A ChessBoard class might implement a MouseListener –So a ChessBoard must implement methods such as: mouseClicked and mouseEntered that specify how objects of your class respond to those events –This contract ensures a successful “interface” between your code and the system’s control of the mouse Questions?

4 Fundamentals of Software Development 1Slide 4 Classes versus Instances Classes are like recipes for making instances of the class.Classes are like recipes for making instances of the class. –The instances are called objects –In general, there can be many instances of any given class –Classes themselves are objects They are instances of the Class classThey are instances of the Class class Exercise:Exercise: –What class did you write for WordGames, Part 1? Answer: CapitalizerAnswer: Capitalizer –When you tested your class, about how many instances of that class did you create? Answer: Several (one for each time you pressed the “Capitalizer” button)Answer: Several (one for each time you pressed the “Capitalizer” button) –In what ways are those instances the same? different? Answer: Same transform rule, but applied to different Strings sent to transform (hence different returned values)Answer: Same transform rule, but applied to different Strings sent to transform (hence different returned values)

5 Fundamentals of Software Development 1Slide 5 Classes versus Instances: Example Example:Example: –Dog class specifies: Attributes (data): typically stored in fields - a Dog has breed, weight, age,...Attributes (data): typically stored in fields - a Dog has breed, weight, age,... Operations (behavior): defined in methods - a Dog eats, fetches, sleeps,...Operations (behavior): defined in methods - a Dog eats, fetches, sleeps,... –Dog instance is an object that is a particular Dog It has a particular breed, etc. It eats, etc.It has a particular breed, etc. It eats, etc. Questions?

6 Fundamentals of Software Development 1Slide 6 Fields A field is data associated with an object (particular instance)A field is data associated with an object (particular instance) –Example: the name that a NameDropper drops Fields are:Fields are: –Persistent They live as long as the object livesThey live as long as the object lives –Global to the class They can be referenced anywhere inside the class definitionThey can be referenced anywhere inside the class definition –Unique to the instance Each instance has its own version of the fieldEach instance has its own version of the field Contrast this with methods, which are shared among all instances (the same copy of the method is used by all instances of the class)Contrast this with methods, which are shared among all instances (the same copy of the method is used by all instances of the class) Fields versus parameters versus local variables: next slide

7 Fundamentals of Software Development 1Slide 7 Fields, parameters, local variables public class NameDropperExclaim extends StringTransformer implements StringTransformable { private String name; private String name; public String transform(String whatToSay) { public String transform(String whatToSay) { char exclamatoryMark = '!'; char exclamatoryMark = '!'; return this.name + " says " + whatToSay + exclamatoryMark; return this.name + " says " + whatToSay + exclamatoryMark; }} name field exclamatoryMark local variable whatToSay 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?

8 Fundamentals of Software Development 1Slide 8 Scope public class NameDropperExclaim extends StringTransformer implements StringTransformable { private String name; private String name; public String transform(String whatToSay) { public String transform(String whatToSay) { char exclamatoryMark = '!'; char exclamatoryMark = '!'; return this.name + " says " + whatToSay + exclamatoryMark; return this.name + " says " + whatToSay + exclamatoryMark; }} name field’s scope exclamatoryMark local variable’s scope whatToSay parameter’s scope Questions?

9 Fundamentals of Software Development 1Slide 9 Constructors public class NameDropperExclaim extends StringTransformer 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 whatToSay) { public String transform(String whatToSay) { char exclamatoryMark = '!'; char exclamatoryMark = '!'; return this.name + " says " + whatToSay + exclamatoryMark; return this.name + " says " + whatToSay + exclamatoryMark; }} 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?

10 Fundamentals of Software Development 1Slide 10 Recap/Summary: Class: Fields, Constructors, Methods public class NameDropper extends StringTransformer 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 + " says " + whatToSay; return this.name + " says " + whatToSay; }} Questions? Review these ideas now by doing Fields, Constructors and Methods – review activity Class declaration Keywords for extension & interface Field(s) Method(s) Constructor(s)


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

Similar presentations


Ads by Google