Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Java Programming II Defining & Implementing Classes.

Similar presentations


Presentation on theme: "CSC 205 Java Programming II Defining & Implementing Classes."— Presentation transcript:

1 CSC 205 Java Programming II Defining & Implementing Classes

2 Topics Objects and their responsibilities Define a class Implement and test a class Use a class

3 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

4 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

5 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 Throttle top:int position:int getFlow():double isOn():boolean shift(amt:int) shutOff() Class name Data members Methods

6 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 constructorV V* main methodX V methodsV V* variablesV V*

7 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

8 142857 Information Hiding top position getFlow isOn shift shuttOff A Throttle object Each object is identified by a unique (hash-)code A handle Private data members Public methods Serve as interfaces

9 The Client (or External) View getFlowisOnshutOff

10 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

11 Specification Format Signature and description Parameters Returns Precondition Postcondition Throws

12 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


Download ppt "CSC 205 Java Programming II Defining & Implementing Classes."

Similar presentations


Ads by Google