1 / 18

Recap: The design of Word Games

Recap: The design of Word Games. We have answered the “four questions” (at least partially): System behavior? Allows a user to play various word games Members of the community? ( one ) User interface & User , ( many ) Transformers, ( many ) Connections How do they interact?

conner
Télécharger la présentation

Recap: The design of Word Games

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: The design of Word Games • We have answered the “four questions”(at least partially): • System behavior? • Allows a user to play various word games • Members of the community? • (one) User interface & User , (many) Transformers, (many) Connections • How do they interact? • User operates user interface, which creates Transformers and Connections. Transformers communicate through Connections. • What goes inside a Transformer? • ConnectionAcceptors, Connections, etcplus an instruction-follower with rules, such as for Capitalizer: 1. Read input 2. Produce capitalized version of it 3. Write output

  2. How to implement rules: Outline • An instruction-follower has a rule, e.g. for Capitalizer: 1. Read input2. Produce capitalized version of it3. Write output • Now let’s see how to implement such rules, as follows! • What is a String? What can you do with them? • Rules, Parameters, Arguments – Methods • Classes and instances –Creating instances (new) • Fields andconstructors

  3. Strings • Java has a special kind of object called a String: • "Hello""What is this?" • "x&% _()^*!" • Double quotes are not part of the string, they simply indicate where the string begins and ends • Our Transformers are StringTransformers. • Each takes String(s) and produces a transformed String • String concatenation: • "Turkeys are"+"bzzz what?!"yields • "Turkeys arebzzz what?!" • The Java + operator takes two Strings and produces a String

  4. String dot method arguments, in parentheses (start at , end before) What Can You Do with Strings? • Use concatenation operator (per previous slide) • Invoke a String method toUpperCase: • "Hello".toUpperCase() yields "HELLO" • Invoke a String method substring: Numbering starts at zero "Hello".substring(3) yields "Hello".substring(1, 3) yields "Hello".substring(0) yields "lo" "el" "Hello"

  5. Rules, Parameters, Arguments • Now that we know about Strings, we can look inside our Transformers • Here is the transform rule for a Capitalizer: • The parameter: The temporary name thePhrasethat refers to the actual argument supplied to the Capitalizer. • What is the transform rule for a Pedantic Transformer that seems to know everything? • E.g., given "you stink!"it outputs "Obviously you stink!" to transform a String (say, thePhrase),returnthePhrase.toUpperCase(); Answer on next slide

  6. Rules, Parameters, Arguments • Here is the transform rule for a Pedantic: • What do we call whatToSay? • Answer: the parameter • If the parameter whatToSay is given the actual value "you stink", it is called an argument • Could we have used thePhrase instead of whatToSay? • Yes, we could use any legal Java name where we used whatToSay • We must, of course, use the same name throughout the rule • Why is Obviously inside quotes while whatToSay is not? • The phrase Obviously is literally what we want, while whatToSay is only a name that lets us refer to some actual value • Think of another Transformer and write itstransformrule • It should take a String and produce a String to transform a String (say, whatToSay),return"Obviously "+ whatToSay; Questions?

  7. Methods (Rules in Java) • In Java, such rules are called methods • Here is the transform method for a Capitalizer: • The syntax (notation) for a method definition looks like this: • Our example begins with String indicating that the method returns a String • Exercise: Write the transform method for a Pedantic Transformer String transform(String thePhrase) { return thePhrase.toUpperCase(); } ReturnType methodName( Type parameter, ...) { Information on what to do } String transform(String whatToSay) { return "Obviously " + whatToSay; } Answer: Questions?

  8. Classes and Instances • All Capitalizers use the same rule • We say that these Capitalizers are all instances of the Capitalizer class • Capitalizer differs from a generic StringTransformer • in that it uses the particular transform rule (method) that is stated • Write the class Pedantfor a Pedantic Transformer class Capitalizer extends StringTransformer { String transform(String thePhrase) { return thePhrase.toUpperCase(); }} Answer on next slide

  9. Classes and Instances class Pedant extends StringTransformer { String transform(String whatToSay) { return "Obviously " + whatToSay; }} • This class describes what a Pedant can do • The class is like a recipefrom which to make a particular Pedant • The class is not a Pedant itself! • Can you guess what word would we use to create a new Pedant (more precisely, a new instance of a Pedant)? • Answer on next slide

  10. Creating Instances • To create a new instance of a class, we use the new operator: • To create a Pedant in Java, we use new Pedant() • Saying it again cooks up another Pedant: new Pedant()

  11. Many Instances • Creating multiple instances of Transformers makes life interesting. For example: • What does sending "I’m here!" to a Pedant, and sending that Pedant’s output to another Pedant yield? • Answer: "Obviously Obviously I’m here!" • What does sending "not much" to a Pedant, and sending that Pedant’s output to a Capitalizer yield? • Answer: "OBVIOUSLY NOT MUCH" • What does sending "not much" to a Capitalizer, and sending that Capitalizer’s output to a Pedant yield? • Answer: "Obviously NOT MUCH"

  12. Classes, Instances and Fields • One class can describe many different instances • Now let’s see multiple distinct instances of a class,with local state associated with each instance • Two NameDroppers, each with their own myNamefield Rumi Maria Hello Rumi says Hello Maria says Hello

  13. Rumi Maria Hello Rumi says Hello • Consider the transform rule forNameDropper: • myName is • NOT a parameter here (do you see why not?) • a persistent part of the particular NameDropper • So we need a local storage spot, with a name that lives with the NameDropper instance • Such a name is called a field Maria says Hello to transform a String (say, thePhrase),return myName + "says "+ thePhrase;

  14. Maria Hello Rumi Rumi says Hello • In Java, the transform method for NameDropper is: • this.myName means this instance’smyNamefield Maria says Hello String transform(String thePhrase) { return this.myName + " says " + thePhrase; }

  15. Fields and Constructors • A NameDropper must know its name from the very beginning • We use a constructor to set the field called myName: • Then, when we invoke NameDropper’s constructor, we give it an argument: NameDropper(String whatMyNameIs) { this.myName = whatMyNameIs; } new NameDropper("Rumi"); new NameDropper("Maria");

  16. Class:Fields, Constructors, Methods public class NameDropper extends StringTransformer implements StringTransformable { private String name; // field: persistent storage, a permanent part // of each NameDropper public NameDropper(String whatMyNameIs) { // Constructor this.name = whatMyNameIs; } public String transform(String whatToSay) { // Method return this.name + " says " + whatToSay; } } Questions? Private means private to the class. The standard is that fields are generally private and all else is public (for now) – more on this later.

  17. Summary: Java syntax of a class Yellow font on this slide indicates Java syntax. Questions? public class NameDropper extends StringTransformer { private String myName;// field: persistent storage, a permanent part of each object public NameDropper(String whatMyNameIs){ // Creation rule this.myName = whatMyNameIs; } public String transform(String whatToSay){ // Transform rule returnthis.myName +" says "+ whatToSay; } }

  18. Summary • All Transformers obey the same general rules and interface • Each defines a transform method (rule) that takes a Stringand returns a String • This enables us to use the same Connection interaction for all of them • Differences among Transformer behaviors are hidden inside the transform method that each of them implements • Today we saw most of the basic pieces of Java • None was shown in sufficient detail to permit mastery • We’ll revisit all these pieces over the next few weeks!!!

More Related