1 / 10

Recap: Key ideas in WordGames

This recap covers key concepts in word games, including defining classes, extending classes vs. implementing interfaces, the use of fields, methods, parameters and local variables, as well as constructors. Learn how to implement these concepts in your own projects.

rthurman
Download Presentation

Recap: Key ideas in WordGames

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recap: Key ideas in WordGames • Defininga class • Extendinga 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

  2. Defininga class • Step 1: Add the class to the project • Step 2: Implement the class • Step 2a: Write the Javadocspecifications with stubs • Step 2b: Convert stubsto the actual code • Obey Sun’s code conventions • Step 3: Test the class • For larger classes, repeat steps 2 and 3several times, implementing moreof the class each time Questions? Do you understand what a stub is, and why it is useful?

  3. 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 Extending a ClassImplementing an Interface Questions?

  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)

  5. Classes versus Instances: Example • Example: • Dogclass specifies: • Attributes (data): typically stored in fields - a Dog has breed, weight, age, ... • Operations (behavior): defined in methods - a Dog eats, fetches, sleeps, ... • Doginstance is an object that is a particular Dog • It has a particular breed, etc. It eats, etc. Questions?

  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

  7. myNamefield whatToSayparameter exclamatoryMarklocal 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

  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; } } myNamefield’s scope whatToSayparameter’s scope exclamatoryMarklocal variable’s scope Questions?

  9. 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?

  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

More Related