Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about.

Similar presentations


Presentation on theme: " Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about."— Presentation transcript:

1

2  Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about the need for private methods  Identify helpful overloaded methods  Identify helpful overloaded constructors  Identify class attributes  Identify class behaviors

3  Should include what we know about the object data  Should include what the object should be able to do

4  Identify nouns and verb phrases in the description.  Nouns are data. What data types?  Verbs are the methods. What parameters do they need? What do they need to return?

5  The money class will represent US Money amounts. Once created a particular Money object cannot change.  We need to be able to add two Money objects to get a third, subtract two Money objects, multiply a money object by a value (such as.05) to get a new Money amount and divide a Money object by a value (such as 2.5).  We must also be able to compare Money objects to determine which is larger, small or if they are the same.  We must be able to display a Money object as normal dollars and cents.  Money may only be a whole number of cents. In other words, Money object may be 3 dollars and 12 cents, but not 3 dollars and 12.5 cents.

6  On your whiteboards, begin writing the class. You should carefully think about the instance variables that you need. How will you internally store money values?  What methods are directly suggested by the description? (Don’t worry about constructors at this point).

7  Do the attributes really reflect the state of the object? Are there any that are better served in the bodies of the methods themselves.  Are there any methods that may be able to be used in different ways? Or can we use one method as a helper method to another.

8  How will we want to build new Money objects?  What makes sense for the user of this class to do?

9  These can be methods that would be “utility” methods to users of the class or could be a helper method for other methods in the class.  Private if only used inside of the class; public if a utility.

10  Different parameter types may have subtle differences.  We may want to provide different interfaces if, for example, we have a similar function but want to pass in different parameters.  Or, if we want to allow the possibility of different numbers of parameters (min method).

11  May obviate the need to have overloaded methods in the case of cardinality (handle 2, handle 3, handle more).  See VarargsDemo1.javaVarargsDemo1.java

12  Should we have a default constructor? ◦ Not automatic if any other constructor is defined  Should we have multiple explicit value constructors? ◦ Are there multiple ways of initializing data  Are there methods that can help the constructor do it’s job? ◦ Adjust methods to check for validity  Constructors can call other constructors. ◦ Stock.java Stock.java ◦ CopyConDemo.java CopyConDemo.java

13  Helpful constants, special values  Common values used across all objects  Object counters  What might we want to include that are “constant” money amounts?

14  Utilities ◦ Math class  Type conversion ◦ Integer.parseInt()  “Factory” methods (produce an object of the specified type) ◦ example is NumberFormat.getCurrencyInstance()  returns a NumberFormat object that is formatted for currency – we don’t have to specify the format


Download ppt " Write a description (or use one provided)  Create an initial encapsulation  Refine the encapsulation  Identify helpful constructors  Think about."

Similar presentations


Ads by Google