Recap: The design of Word Games We have answered the “four questions”



Yüklə 469 b.
tarix23.09.2017
ölçüsü469 b.
#1272


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, etc plus an instruction-follower with rules, such as for Capitalizer:
        • 1. Read input 2. Produce capitalized version of it 3. Write output

How to implement rules: Outline

  • An instruction-follower has a rule, e.g. for Capitalizer:

        • 1. Read input 2. Produce capitalized version of it 3. 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 and constructors


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


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 :



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 thePhrase that 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!"


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 its transform rule

    • It should take a String and produce a String


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



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 Pedant for a Pedantic Transformer



Classes and Instances

  • This class describes what a Pedant can do

    • The class is like a recipe from 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


Creating Instances

  • To create a new instance of a class, we use the new operator:



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"


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 myName field


Consider the transform rule for NameDropper:

  • Consider the transform rule for NameDropper:

  • 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


In Java, the transform method for NameDropper is:

  • In Java, the transform method for NameDropper is:

    • this.myName means this instance’s myName field


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:


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;

  • }

  • }



Summary: Java syntax of a class

  • 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

  • return this.myName + " says " + whatToSay;

  • }

  • }



Summary

  • All Transformers obey the same general rules and interface

    • Each defines a transform method (rule) that takes a String and 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!!!


Yüklə 469 b.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə