1 / 105

Chapter 3: Designing interactive classes.

Chapter 3: Designing interactive classes. Objectives. After studying this chapter you should understand the following: the role of responsibilities in the design of an object; the categorization of an object’s responsibilities as knowing responsibilities or doing responsibilities;

baldasarre
Télécharger la présentation

Chapter 3: Designing interactive classes.

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. Chapter 3: Designing interactive classes.

  2. Objectives • After studying this chapter you should understand the following: • the role of responsibilities in the design of an object; • the categorization of an object’s responsibilities as knowing responsibilities or doing responsibilities; • the difference between local variables and instance variables; • the structure of a complete program in Java; • the structure of a test system; • the purpose of named constants. NH-Chapter 3

  3. Objectives • Also, you should be able to: • analyze the role a simple object plays in a given problem and list its responsibilities; • use Java to specify the features of an object based on its responsibilities; • use Java to implement a simple class that has been specified; • implement a complete Java program using a simple text-based interface for an object; • implement a simple tester for testing a class implementation; • define named constants. NH-Chapter 3

  4. Designing with objects • Two questions when we design an OO system : • what are the objects? • what features should these objects have? • Our Initial goal: learn to design and implement simple objects. • We will ssume objects are there for the picking. NH-Chapter 3

  5. Designing with objects • Objects are designed to support system functionality. • System specification are distributed as responsibilities to objects identified. NH-Chapter 3

  6. Object responsibilities • Think in terms of • what object must know. • what object must do. NH-Chapter 3

  7. Object responsibilities • Knowing responsibilities include: • knowing properties of the entity object is modeling; • knowing about other objects with which it needs to cooperate. NH-Chapter 3

  8. Object responsibilities • Doing responsibilities include: • computing particular values; • performing actions that modify its state; • creating and initializing other objects; • controlling and coordinating the activities of other objects. NH-Chapter 3

  9. From responsibilities to class features • “Knowing responsibilities” • translate into data the object must maintain or • Translate into queries. • “Doing responsibilities” • translate into commands or • Translate into queries. NH-Chapter 3

  10. Designing a class • To design a class: • determine an object’s responsibilities, • classify them as knowing or doing responsibilities. NH-Chapter 3

  11. Example: Design of Nim game • Game: Players take turns removing sticks from a pile. Each player in turn removes one, two, or three sticks. The player who removes the last stick loses. NH-Chapter 3

  12. Design of Nim game • Two objects for the picking: • Player • Pile of sticks • Pile and Player are part of the model of problem. • Model aspects of game independently of how is presented to a user or how a user interacts with it. NH-Chapter 3

  13. Designing Pile • Pile a very simple object: • keeps track of how many sticks remain. • Pile responsibilities: • know: • number of sticks remaining • do: • reduce number of sticks (remove sticks) NH-Chapter 3

  14. Designing Player • Player takes a turn in the game to remove sticks from Pile. • Player responsibilities: • know: • this Player’s name. • how many sticks this Player removed on his/her most recent turn. • do: • take a turn by removing sticks from the Pile NH-Chapter 3

  15. From knowing responsibilities to queries • Class: Pile • queries:sticks the number of sticks remaining in this Pile, a non-negative integer NH-Chapter 3

  16. From knowing responsibilities to queries • Class: Player • queries: • name : this Player’s name, a String • sticksTaken : number of sticks this Player removed on his/her most recent turn ( 1, 2, or 3). NH-Chapter 3

  17. From doing responsibilities to commands • Class: Pile • commands:remove : reduce number of sticks by specified amount (number) NH-Chapter 3

  18. From doing responsibilities to commands • Class: Player • commands:takeTurn remove 1, 2, or 3 sticks from the specified Pile (pile) NH-Chapter 3

  19. Interaction diagram:Player takes turn NH-Chapter 3

  20. Pile, and Player constructors • How are the Player’s name and the initial number of sticks in the Pile determined? • Set these values when objects are created:Constructors. • Constructors can have parameters: • Player’s name parameter in Player’s constructor. • Initial number of sticks parameter in Pile constructor. NH-Chapter 3

  21. Pile specifications • nimGame • Class Pile public class Pile A pile of sticks for playing simple nim. • Constructors public Pile (int sticks) Create a new Pile, with the specified number of sticks. sticks must be non-negative. NH-Chapter 3

  22. Pile specifications • Queries publicint sticks () Number of sticks remaining in this Pile. • Commands publicvoid remove (int number) Reduce the number of sticks by the specified amount. number must be non-negative and not greater than the number of sticks remaining. NH-Chapter 3

  23. Player specifications • nimGame • Class Player public class Player A player in the game simple nim. • Constructors public Player (String name) Create a new Player with the specified name. NH-Chapter 3

  24. Player specifications • Queries public String name () The name of this Player. publicint sticksTaken () The number of sticks this Player removed on this Player’s most recent turn: 1, 2, or 3. NH-Chapter 3

  25. Player specifications • Commands publicvoid takeTurn (Pile pile) Remove 1, 2, or 3 sticks from the specified Pile. NH-Chapter 3

  26. Implementing the class Pile • Data maintained by Pile is number remaining sticks. • Instance variable sticksLeft is initialized in constructor and value returned by query sticks: privateint sticksLeft; // sticks left in the Pile public Pile (int sticks) { sticksLeft = sticks; } publicint sticks () { return sticksLeft; } NH-Chapter 3

  27. Implementing the class Pile • Command remove is specified with an int parameter number, indicating sticks to be removed: • Executing command reduces instance variable sticksLeft by value client supplies in number. publicvoid remove (int number) {… publicvoid remove (int number) sticksLeft = sticksLeft - number; } NH-Chapter 3

  28. Implementing the class Player • Player needs to know name and number of sticks taken on most recent turn. • Variables should be initialized in the constructor. private String name; // this Player’s name privateint sticksTaken; // sticks taken on this Player’s most recent turn public Player (String name) { this.name = name; this.sticksTaken = 0; } NH-Chapter 3

  29. Implementing the class Player • Queries simply return the values of the instance variables: public String name () { return name; } publicint sticksTaken () { return sticksTaken; } NH-Chapter 3

  30. Invoking a method: acting as client • General form for invoking a command is • General form for invoking a query: objectReference.queryName(arguments) objectReference.commandName(arguments); NH-Chapter 3

  31. Implementing Player’s takeTurn /** * Remove 1, 2, or 3 sticks from the specified Pile. * The Pile must not be empty. */ public void takeTurn (Pile pile) { … • takeTurn method must: • determine how many sticks to take; Give Player the move strategy to take 1 stick. • remove them from the Pile; pile.remove(1); • store the number removed in sticksTaken instance variable. sticksTaken = 1; NH-Chapter 3

  32. Implementing Player’s takeTurn /** * Remove 1, 2, or 3 sticks from the specified Pile. * The Pile must not be empty. */ public void takeTurn (Pile pile) { pile.remove(1); sticksTaken = 1; } NH-Chapter 3

  33. Interaction diagram: Player commands a Pile NH-Chapter 3

  34. Parameters v.s. arguments • Arguments are provided in a method invocation by expressions. Thus we could write something like pile.remove(sticksTaken + 1); or even pile.remove(2*sticksTaken+2); NH-Chapter 3

  35. Parameters v.s. arguments • Arguments must match in number, type and order: • An invocation of move must provide two arguments, an int and a double in that order. publicvoid move (int direction, double distance) { … } object.move(90, 2.5); NH-Chapter 3

  36. Commands and queries • A command invocation is a form of statement. • A query, which produces a value, is an expression. • If myCounter is a Counter object and i an int, i = myCounter.currentCount(); i = myCounter.currentCount()+10; NH-Chapter 3

  37. Example: Maze game • Player must find his/her way through a set of connected rooms to reach some goal. • there will be tricks player must figure out; • creatures of various kinds to be defeated along the way. NH-Chapter 3

  38. Example: Maze game • Objects for the picking: • player, • maze denizens, • rooms. NH-Chapter 3

  39. Designing Explorer • Explorer responsibilities: • know: • his/her name • location in the maze • amount of annoyance done when poking an opponent • amount of annoyance he/she can endure before being defeated. NH-Chapter 3

  40. Designing Explorer • Responsibilities into properties • namename of the Explorer • locationroom in which Explorer is in • strengthmeasure of Explorer’s offensive ability • tolerancemeasure of what it takes to defeat Explorer NH-Chapter 3

  41. Designing Explorer • Type of value for each Explorer’s property: • Name: String • Location: Room • Strength:int • Tolerance:int Explorer String String name Room location Room int strength 10 int tolerance 100 NH-Chapter 3

  42. Designing Explorer • Explorer responsibilities: • do: • change location in the maze (move from room to room) • fight a maze Denizen • commands to to perform these actions. • movechange location • poke poke a Denizen NH-Chapter 3

  43. Designing Explorer • Both commands will have parameters: • movechange location (new location) • pokepoke a Denizen (denizen to poke) NH-Chapter 3

  44. Interaction diagram NH-Chapter 3

  45. A constructor for the class Explorer • Need constructor for creating Explorer instances. • During creation properties must be initialized. • Require values for name, location, strength, and tolerance be provided as argument. • Constructor for Explorer has four parameters: create new Explorer (name, location, strength, tolerance) NH-Chapter 3

  46. Explorer specification • mazeGame • Class Explorer • public class Explorer • A maze game player. • Constructors • public Explorer (String name, Room location, int strength, int tolerance) • Create a new Explorer with specified name, initial location, strength,and tolerance. • Annoyance (hit points) required to defeat this Explorer. NH-Chapter 3

  47. Explorer specification • Queries • public String name () • Name of this Explorer. • public Room location () • Room in which this Explorer is currently located. • publicint strength () • Annoyance (hit points) this Explorer causes when poking an opponent. • publicint tolerance () • Annoyance (hit points) required to defeat this Explorer. NH-Chapter 3

  48. Explorer specification • Commands • publicvoid move (Room newRoom) • Move to the specified Room. • publicvoid takeThat (int hitStrength) • Receive a poke of the specified number of hit points. • publicvoid poke (Denizen opponent) • Poke the specified Denizen. NH-Chapter 3

  49. Implementing class Explorer • Explorer objects have four properties:name, location, strength, and tolerance. • Use instance variables to store these values: private String name; // name private Room location; // current location privateint strength; // current strength (hit points) privateint tolerance; // current tolerance (hit points) • These variables are initialized in the constructor. NH-Chapter 3

  50. Implementing class Explorer • Queries return current values of instance variables. • Example: query location returns value stored in location. public Room location () { return location; } NH-Chapter 3

More Related