Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap: Key ideas in WordGames

Similar presentations


Presentation on theme: "Recap: Key ideas in WordGames"— Presentation transcript:

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

2 Defining a class Step 1: Add the class to the project
Step 2: Implement the class Step 2a: Write the Javadoc specifications with stubs Step 2b: Convert stubs to the actual code Obey Sun’s code conventions Step 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? Fundamentals of Software Development 1

3 Extending a Class Implementing an Interface
When a class extends another class: it inherits all the functionality of the other class 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? Fundamentals of Software Development 1

4 Classes versus Instances
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 class Exercise: What class did you write for WordGames, Part 1? Answer: 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) 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) Fundamentals of Software Development 1

5 Classes versus Instances: Example
Dog class specifies: Attributes (data): typically stored in fields - a Dog has breed, weight, age, ... 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. Questions? Fundamentals of Software Development 1

6 Fields A field is data associated with an object (particular instance)
Example: the name that a NameDropper drops Fields are: Persistent They live as long as the object lives Global to the class They can be referenced anywhere inside the class definition Unique to the instance Each 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) Fields versus parameters versus local variables: next slide Fundamentals of Software Development 1

7 Fields, parameters, local variables
myName field whatToSay parameter exclamatoryMark local variable Fields, parameters, local variables public class NameDropperExclaim extends StringTransformer implements StringTransformable { private String myName; public String transform(String whatToSay) { char exclamatoryMark = '!'; return this.myName + " says " whatToSay + exclamatoryMark; } Questions? The myName field can be used anywhere within the class We write this.myName to emphasize that it is THIS instance’s name. Adopt this habit! (More on “this” later.) Each NameDropperExclaim instance has its own myName field (but all the instances share the same transform method) The myName field persists as long as the NameDropperExclaim instance lives Fundamentals of Software Development 1

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

9 Constructors Questions?
The NameDropperExclaim class (previous slide) has a problem. What is the problem? Answer: it uses the myName field but never gives it a value! What is the fix? Constructors public class NameDropperExclaim extends StringTransformer implements StringTransformable { private String myName; public NameDropperExclaim() { } public NameDropperExclaim(String givenName) { this.myName = givenName; public String transform(String whatToSay) { char exclamatoryMark = '!'; return this.myName + " says " whatToSay + exclamatoryMark; The “do-nothing” constructor is created for you if you don’t create a constructor yourself This constructor can be used to initialize the myName field Questions? Fundamentals of Software Development 1

10 Recap/Summary: Class: Fields, Constructors, Methods
Class declaration Keywords for extension & interface public class NameDropper extends StringTransformer implements StringTransformable { private String myName; public NameDropper(String whatMyNameIs) { this.myName = whatMyNameIs; } public String transform(String whatToSay) { return this.myName + " says " + whatToSay; Field(s) Constructor(s) Method(s) Questions? Review these ideas now by doing Fields, Constructors and Methods – review activity Fundamentals of Software Development 1


Download ppt "Recap: Key ideas in WordGames"

Similar presentations


Ads by Google