CSC 205 Java Programming II Defining & Implementing Classes.
-
Upload
lydia-adams -
Category
Documents
-
view
213 -
download
0
Transcript of CSC 205 Java Programming II Defining & Implementing Classes.
CSC 205Java Programming II
Defining & Implementing Classes
Topics
Objects and their responsibilities Define a class Implement and test a class Use a class
A Dummy Class
Write a class can be simple A dummy class with only one line of code
class Dummy {} A default constructor will be provided: it’s equivalent to
Dummy(){} You can create a Dummy object
Dummy d = new Dummy(); But such a class is not useful
You can do very little with object d
Responsibilities
An object should Maintain its own state: know something
A String object should know its length A Date class should know the year, month, day
associated with it Be able to provide certain services: do something
A String object should be able to return a portion of itself (a substring) as required
A JOptionPane object should be able to show itself as a input or message dialog as required
Classes & Their Members
A class defines what its instances (or objects) should know and should be able to do Data members: used to maintain state Method members: define behaviors
Example: the Throttle class
Throttletop:intposition:int
getFlow():doubleisOn():booleanshift(amt:int)shutOff()
Class name
Data members
Methods
Define A Class
Classes Entity classes (or problem domain class)
Usually don’t have a main method Main classes: classes with a main method
Only one such class in an application Possible members in a class
entity class main classconstructor V V* main method X Vmethods V V*variables V V*
Encapsulation
Information hiding Make instance variables private Provide public accessors (or getters) Provide public mutators (or setters) only when needed Example: when designing a Person class
Define the age variable as private Provide a public getAge() method What about a public setAge(int n) method? NO! Provide a updateAgeAnnualy() method
142857
Information Hiding
topposition
getFlow
isOn
shift
shuttOff
A Throttle object
Each object is identified by a unique (hash-)code
A handle
Private data membersPublic methodsServe as interfaces
The Client (or External) View
getFlow isOn shutOff
Writing Methods
An entity class needs to interact with other classes Message passing
Message: receiver + operation name + parameter list Return value is optional
Contracts between client and server objects Accessibility modifier Return type, maybe void Method name Parameter list, maybe none
Specification Format
Signature and description Parameters Returns Precondition Postcondition Throws
The Throttle Class
See source code files for the class and a test driver class under our class folder
How to test an entity class? Write a test driver class, that has a main method Testing is trying to break the application
Use extreme values Use values beyond the normal ranges
Creating a Throttle object with a negative size Trying to shift it to illegal positions