Recap: The design of Word Games

Slides:



Advertisements
Similar presentations
Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Fundamentals of Software Development 1Slide 1 Today’s Summary UML class diagrams – –Why classes are important – –UML class diagrams – relationships – –UML.
Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,
Fundamentals of Software Development 1Slide 1 Methods: Defining, Invoking, Executing “There is a difference between knowing the path and walking the path.”
Classes, Encapsulation, Methods and Constructors
Fundamentals of Software Development 1Slide 1 Method invocation versus definition To define (write) a method means to write its codeTo define (write) a.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Fundamentals of Software Development 1Slide 1 Methods: Defining, Invoking, Executing “There is a difference between knowing the path and walking the path.”
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Fundamentals of Software Development 1Slide 1 Outline Designing a computational community for WordGamesDesigning a computational community for WordGames.
COMP More About Classes Yi Hong May 22, 2015.
Introducing Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in WordGames Defining a classDefining a class Extending a class versus implementing an interfaceExtending.
Fundamentals of Software Development 1Slide 1 Today’s summary Attitudes toward learning:Attitudes toward learning: –Working in groups –Learning by doing.
Classes CS 21a: Introduction to Computing I First Semester,
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Fundamentals of Software Development 1Slide 1 Today’s Summary InterfacesInterfaces –What are they? –Why are they important? –How do they relate to WordGames?
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in BallWorlds so far Writing a class:Writing a class: –structure –process Writing methods:Writing.
Recap: Key ideas in WordGames
Animate objects and threads
Topic: Classes and Objects
Summary prepared by Kirk Scott
Classes (Part 1) Lecture 3
More Sophisticated Behavior
Introduction to Classes and Objects
Object Oriented Programming
Inheritance and Polymorphism
Methods Chapter 6.
Agenda Warmup AP Exam Review: Litvin A2
Classes and Objects in Java
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Object Oriented Programming
Object Oriented Analysis and Design
Pemrograman Dasar Methods PTIIK - UB.
IMAT5205 Systems Analysis and Design
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Learning Objectives Classes Constructors Principles of OOP
Week 3 Object-based Programming: Classes and Objects
IFS410 Advanced Analysis and Design
Python Primer 1: Types and Operators
String.
Overview of Java 6-Apr-19.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Fundamentals of Software Development 1
Programming with inheritance Based on slides by Alyssa Harding
Classes CS 21a: Introduction to Computing I
In this class, we will cover:
CISC101 Reminders Assignment 3 due today.
String Objects & its Methods
Introduction to Computer Science and Object-Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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