Presentation is loading. Please wait.

Presentation is loading. Please wait.

Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.

Similar presentations


Presentation on theme: "Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They."— Presentation transcript:

1 Designing Classes CS239 – Jan 26, 2006

2 Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They form a java “collection”, so a for each loop can be used if we want to run through all of the values.  They can be used to declare new variables, which can only take on the values defined by the enum type.

3 Questions from the worksheet

4 Designing Classes  Context  Class Design process  Practice with a small class

5 Identifying Classes and Objects  The core activity of object-oriented design is determining the classes and objects that will make up the solution  The classes may be part of a class library, reused from a previous project, or newly written  One way to identify potential classes is to identify the objects discussed in the requirements  Objects are generally nouns, and the services that an object provides are generally verbs

6 Identifying Classes and Objects  A partial requirements document: The user must be allowed to specify each product by its primary characteristics, including its name and product number. If the bar code does not match the product, then an error should be generated to the message window and entered into the error log. The summary report of all transactions must be structured as specified in section 7.A. Of course, not all nouns will correspond to a class or object in the final solution

7 Identifying Classes and Objects  Remember that a class represents a group (classification) of objects with the same behaviors  Generally, classes that represent objects should be given names that are singular nouns  Examples: Coin, Student, Message  A class represents the concept of one such object  We are free to instantiate as many of each object as needed

8 Identifying Classes and Objects  Sometimes it is challenging to decide whether something should be represented as a class  For example, should an employee's address be represented as a set of instance variables or as an Address object  The more you examine the problem and its details the more clear these issues become  When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities

9 Identifying Classes and Objects  We want to define classes with the proper amount of detail  For example, it may be unnecessary to create separate classes for each type of appliance in a house  It may be sufficient to define a more general Appliance class with appropriate instance data  It all depends on the details of the problem being solved

10 Identifying Classes and Objects  Part of identifying the classes we need is the process of assigning responsibilities to each class  Every activity that a program must accomplish must be represented by one or more methods in one or more classes  We generally use verbs for the names of methods  In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design

11 Defining the class  Write an abstract (textual) description  Create an initial encapsulation  Refine the encapsulation  Identify constructors  Think about the need for private methods  Identify helpful overloaded methods  Identify helpful overloaded constructors  Identify class attributes  Identify class behaviors

12 Example You are working on a banking system. You have been given the following description of Money objects for the new system. Money will be represented as dollars and cents with cents never exceeding 99. Money can be increased or decreased by other Money amounts. Money can be negative or positive values. When displayed, money will be displayed in the usual way ex. $9999999.99 and if negative $99999999.99-. Money amounts can be compared.

13 Initial Encapsulation – Specs  Nouns-attributes  Verbs-behaviors/methods Money will be represented as dollars and cents with cents never exceeding 99. Money can be increased or decreased by other Money amounts. Money can have negative or positive values. When displayed, money will be displayed in the usual way ex. $9999999.99 and if negative $99999999.99-. Money amounts can be compared.

14 Initial Encapsulation  Attributes should be private  Accessor (get) methods  Mutator (set) methods  Other methods to take care of behaviors.

15 Initial encapsulation  In your group:  Determine your attributes and the type of each – How will you represent money?  Determine how you will deal with the negative amounts.  Then list the method headers which will represent the listed behaviors.

16 How many attributes do you have? Refine the attributes  Attributes should to define essential characteristics of the objects.  Local variables should be used for other types of data.

17 You should have 4 methods defined  What are they?  What is passed to the method?  What is returned from the method?

18 Refining the methods  Look for similar behaviors.  Are you increasing and decreasing as completely separate tasks. What is similar about them. Can you write one as a variant of another?  Did you build multiple methods for compare?  Is your display written as toString? Most classes should have one providing a “view” of the object.

19 Refining the methods  Look for missing behavior  Is a method complex and can/should be broken down into more than one method?  Is there something else within the scope of the definition that may be needed?

20 Constructors  What would a basic constructor look like?  What will it do to insure that all values are set correctly?

21 Private – helper methods  Is there anything that might help make the process of building and manipulating Money objects easier?  Is there any process that we don’t want to directly provide to users?

22 Are there methods that we might overload for different variants  What if we want to increase by a Money amount or simply by a multiplier?  Can these overloaded methods use one another?  (Java will use automatic type conversion if it cannot find an exact match but a similar match – think Math.pow(3,2);) You may not have to build every variant.

23 Overloaded constructors  Often use a default and an explicit value.  What kind of constructors would be appropriate to Money?

24 Any class attributes?  Typically, constants, special values.  Object counters.  Are there any appropriate to this class?

25 And are there any class behaviors?  Services that don’t work with a specific object.  Functions that return class attribute values or methods that manipulate class attribute values.  Utilities (think PA1).  Conversions  Factory methods (build objects of that type) – static but return an object of the class type.


Download ppt "Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They."

Similar presentations


Ads by Google