Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Specification of a simple class. This chapter discusses n How to write the specifications for a class. u The precise description of features.

Similar presentations


Presentation on theme: "Chapter 4 Specification of a simple class. This chapter discusses n How to write the specifications for a class. u The precise description of features."— Presentation transcript:

1 Chapter 4 Specification of a simple class

2 This chapter discusses n How to write the specifications for a class. u The precise description of features common to all instances of the class. u Features of the server seen by the client. u Distinct from the implementation. n The client-server relationship between two objects. n Several example class specifications.

3 Client and Server n A client queries and commands a server. u Queries ascertain values of properties. u Commands change its state. n A client uses a server.

4 Client and Server (cont.)

5 Specification and implementation n specification: an object’s features, as seen by its clients. n implementation: the “internals” that make up the features.

6 Specification and implementation (cont.) n Specifications isolate you from the details of the implementation. u “I don’t care how you do it, just get the job done”(to specifications). u How the features are actually implemented by the server, is of no concern to the client. n Preserving the distinction between specification and implementation is absolutely essential.

7 Let’s start specifying n First, enumerate the object’s responsibilities. Then determine its properties and commands. n Java syntax does not allow us to separate a class specification from its implementation.

8 A simple counter A simple counter’s responsibilities: Know (one query): the value of the count Do (two commands): set the count to 0 increment the count by 1

9 A simple counter (cont.) n A simple counter Class: Counter Queries (Properties) : count (A non-negative integer) Commands: reset (sets count value to 0) step_count (increments the count by 1)

10

11 Specification documentation n Tools such as javadoc generate sets of HTML documents containing specifications extracted from program source files.

12

13 Invoking a method n method: a language construct that defines and implements a query or command. n In order to invoke a method, you must have an instance of the class call the method. Syntax: instance.method() Example: c.count()

14

15

16 Maze game example n An explorer (player) must navigate successfully through the rooms without being killed by denizen (monsters). n There can be several rooms and denizen, but only one explorer. n We need 3 classes: Explorer, Denizen, and Room.

17 Explorer responsibilities Know: his name his location in the maze damage inflicted upon an opponent damage received from an opponent his stamina Do: set or change name change location fight a maze Denizen

18

19 Invoking methods with parameters n To be invoked properly, a method sometimes requires information from the client. Information is passed in as parameters. n When a client invokes a method, the client must provide a value of the appropriate type for each parameter. Syntax: instance.method(p0,p1,p2…) n The only way an object’s state can change is by invoking one of the object’s methods.

20 Parameters and arguments n If we wish to change an explorer’s name, we must provide a new name. n If we wish to change an explorer’s location, we must provide either a new location or a movement from which the location can be determined. n The needed elements are referred to as the parameters of the command. n The actual values we provide are referred to as arguments.

21

22

23 Constructors n Can be used to set the initial values of an objects properties. n Examples: u A counter’s initial value of 0. u An explorer’s initial name, location, strength, and stamina.

24

25

26 Class Student n Consider a university registration system. u Students register for courses. F Each student is represented by a distinct object. F All students are members of the Student class. u The university F Assesses fees. F Prints student schedules. F Produces class rolls. F Etc.

27 Queries public String name () public String address () public String ssn () public int creditHours () public int fees () public int feesPaid () public courses.CourseList schedule()

28

29 Commands public void changeName () public void changeAddress () public void changeSsn () public void addCourse () public void dropCourse () public void payFees ()

30 Contructors public Student (String name, String address, String ssn)

31 We’ve covered n Specification of classes in Java. n Client-Server relationships. n Definitions of specification and implementation. n Several examples of simple class implementation.

32 Glossary

33 Glossary (cont.)


Download ppt "Chapter 4 Specification of a simple class. This chapter discusses n How to write the specifications for a class. u The precise description of features."

Similar presentations


Ads by Google