120 likes | 339 Views
CSC 205 Java 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 {}
E N D
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 Class name Throttle top:int position:int Data members getFlow():double isOn():boolean shift(amt:int) shutOff() 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 class constructor V V* main method X V methods 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
Information Hiding Private data members Public methods Serve as interfaces getFlow shift top position isOn shuttOff 142857 A handle Each object is identified by a unique (hash-)code A Throttle object
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