1 / 12

CSC 205 Java Programming II

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 {}

dani
Télécharger la présentation

CSC 205 Java Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 205Java 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 Class name Throttle top:int position:int Data members getFlow():double isOn():boolean shift(amt:int) shutOff() 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 constructor V V* main method X V methods V V* variables V 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. 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

  9. The Client (or External) View getFlow isOn shutOff

  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

More Related