Todays Summary Method Definition and Invocation Summarized on

  • Slides: 12
Download presentation
Today’s Summary • • • Method Definition and Invocation Summarized on the next Expressions

Today’s Summary • • • Method Definition and Invocation Summarized on the next Expressions few slides Statements Classes Parameters, actual arguments, fields, local variables Interfaces High-level UML Overview for Word. Games Stubs What’s Ahead Fundamentals of Software Development 1 1

Method Definition and Invocation • Method Definition and Invocation – Define (write) a write

Method Definition and Invocation • Method Definition and Invocation – Define (write) a write its code – Invoke (call, run) a cause the method’s code to run Fundamentals of Software Development 1 2

Expressions • Expressions – Every expression has a type and value – Simple Expressions

Expressions • Expressions – Every expression has a type and value – Simple Expressions • "I am your father. “ – Combine expressions • x + y * 35 – z % 3 / r Fundamentals of Software Development 1 3

Statements • A statement has neither type nor value; – it has an effect

Statements • A statement has neither type nor value; – it has an effect • The increment pattern (for “counting”) – x = x + 1; ++ x; x ++; • Block statement: – just enclose statements in {}s Fundamentals of Software Development 1 4

Recap: A class with fields A class contains 3 parts. What are they? public

Recap: A class with fields A class contains 3 parts. What are they? public class Name. Dropper extends String. Transformer implements String. Transformable { Field – persistent storage private String my. Name; associated with the object public Name. Dropper() { this. my. Name = "Bozo"; } Constructors – here they initialize the object’s name public Name. Dropper(String my. Name) { this. my. Name = my. Name; The required } transform method public String transform(String the. Phrase) { return this. my. Name + " says " + the. Phrase; } } Fundamentals of Software Development 1 5

Parameters and actual arguments versus fields versus local variables public class Name. Dropper extends

Parameters and actual arguments versus fields versus local variables public class Name. Dropper extends String. Transformer implements String. Transformable { Field – persistent storage private String my. Name; public Name. Dropper() { this. my. Name = "Bozo"; } associated with the object, global to the class Actual argument – value “sent” for my. Name: new Name. Dropper(“Ida”); public Name. Dropper(String my. Name) { Parameters – input this. my. Name = my. Name ; = to a method, local } to that method public String transform(String the. Phrase) { Local variable – String in. Between. String = " says "; String helper variable, local to its method return this. my. Name + in. Between. String + the. Phrase ; + + } } Fundamentals of Software. Parameters, arguments; Fields; Local variables: Development 1 How are they used? How are they declared? 6

public class Name. Dropper extends String. Transformer implements String. Transformable { private String my.

public class Name. Dropper extends String. Transformer implements String. Transformable { private String my. Name; public Name. Dropper(String name. To. Use) { this. my. Name = name. To. Use; } Definition of the Name. Dropper class public transform(String phrase) { return this. my. Name + “says ” + phrase; } } Name. Dropper person 1, person 2, person 3; Invoking the person 1 = new Name. Dropper(“Calvin”); Name. Dropper constructor and person 2 = new Name. Dropper(“Hobbes”); transform method person 3 = new Name. Dropper(“lobster”); person 1. transform(“you look funny today, Hobbes. ”); person 2. transform(“you looker even funnier. ”); Questions? person 1. transform(“no, YOU look funnier. ”); Fundamentals of Software Development 1 7 person 3. transorm(“I amd just a lonely lobster. ”);

Why have interfaces? • A Java interface is the “face” that one class shows

Why have interfaces? • A Java interface is the “face” that one class shows others – An interface contains the signature but not body for each method – Any class that implements an interface must provide the methods listed in the interface • So an interface specifies a contract Example of a Java interface public interface Clock. Radio { public Time get. Time(); public void set. Time(Time x, Station s); } Fundamentals of Software Development 1 8

UML class diagram for Word. Games All our stuff The String. Transformable interface is

UML class diagram for Word. Games All our stuff The String. Transformable interface is how our code knows how to “connect” to your code <<interface> String. Transformable -------------transform(String) : String Capitalizer Fundamentals of Software Development 1 Name. Dropper xxx … xxx Questions on this important idea? 9

Implementing by using documented stubs • Stub: a method whose body is either: –

Implementing by using documented stubs • Stub: a method whose body is either: – empty (if the method returns nothing), or – trivial, for example: • return null (if the method returns an object) • return 999 (if the method returns a number) • return true (if the method returns a boolean) • Documented stub: a stub with appropriate Javadoc • Example on next slide Fundamentals of Software Development 1 10

Implementing by using documented stubs Stub: a method whose body is either empty or

Implementing by using documented stubs Stub: a method whose body is either empty or trivial Documented stub: a stub with appropriate Javadoc /** * A String. Transformer that transforms the phrase that it is given * by capitalizing it. * * @author David Mutchler. */ public class Name. Dropper extends String. Transformer implements String. Transformable { /** * Does nothing beyond constructing the Capitalizer. */ Stub public Capitalizer() { Documentation } Questions? /** * Capitalizes each letter of the given phrase and return the result. * * @param the. Phrase The phrase to transform. * @return The capitalized version of the given phrase. */ public String transform(String the. Phrase) { Stub, to be filled in later by: return null; return this. my. Name + “says ” + } Fundamentals of Software the. Phrase; } Development 1 11

What’s Ahead? • Before the next session: – Do Homework 8 (no late homework!)

What’s Ahead? • Before the next session: – Do Homework 8 (no late homework!) Reminder: Find the homework assignment from the Schedule page of the CSSE 120 Angel site angel. rose-hulman. edu • Including the reading and the associated online quiz – Next session: • Programming patterns (conditionals, etc. ) • More Word. Games - Implementing your own classes! • Next week – Details to follow: – Review session Tuesday night – First exam Wednesday night Our usual suggestion: Routinely do your homework in F-217 (CSSE lab). A student assistant is there every Sunday through Thursday evening, 7 pm to 9 pm, so you can get immediate answers to any questions you might have. Fundamentals of Software Development 1 12