Download presentation
Presentation is loading. Please wait.
1
Recap: The design of Word Games
Fundamentals of Software Development 1 4/17/2018 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 Fundamentals of Software Development 1 Rose-Hulman Institute of Technology
2
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 Fundamentals of Software Development 1
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 Fundamentals of Software Development 1
4
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 : String method dot arguments, in parentheses Numbering starts at zero "Hello".substring(3) yields "Hello".substring(1, 3) yields "Hello".substring(0) yields "lo" "el" "Hello" (start at , end before) Fundamentals of Software Development 1
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 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!" to transform a String (say, thePhrase), return thePhrase.toUpperCase(); Answer on next slide Fundamentals of Software Development 1
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 its transform rule It should take a String and produce a String to transform a String (say, whatToSay), return "Obviously " + whatToSay; Questions? Fundamentals of Software Development 1
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: Fundamentals of Software Development 1 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 Pedant for a Pedantic Transformer class Capitalizer extends StringTransformer { String transform(String thePhrase) { return thePhrase.toUpperCase(); } } Answer on next slide Fundamentals of Software Development 1
9
Classes and Instances This class describes what a Pedant can do
class Pedant extends StringTransformer { String transform(String whatToSay) { return "Obviously " + whatToSay; } } 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 Fundamentals of Software Development 1
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: Fundamentals of Software Development 1
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" Fundamentals of Software Development 1
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 myName field Rumi Maria Hello Rumi says Hello Maria says Hello Fundamentals of Software Development 1
13
Consider the transform rule for NameDropper:
Rumi Maria Hello Rumi says Hello 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 Maria says Hello to transform a String (say, thePhrase), return myName + " says " + thePhrase; Fundamentals of Software Development 1
14
In Java, the transform method for NameDropper is:
Maria Hello Rumi Rumi says Hello Maria says Hello In Java, the transform method for NameDropper is: this.myName means this instance’s myName field String transform(String thePhrase) { return this.myName + " says " + thePhrase; } Fundamentals of Software Development 1
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"); Fundamentals of Software Development 1
16
Class: Fields, Constructors, Methods
Fundamentals of Software Development 1 4/17/2018 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. Fundamentals of Software Development 1 Rose-Hulman Institute of Technology
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 return this.myName + " says " + whatToSay; Fundamentals of Software Development 1
18
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!!! Fundamentals of Software Development 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.