CSC 205 Java Programming II Defining & Implementing Classes.

12
CSC 205 Java Programming II Defining & Implementing Classes

Transcript of CSC 205 Java Programming II Defining & Implementing Classes.

Page 1: CSC 205 Java Programming II Defining & Implementing Classes.

CSC 205Java Programming II

Defining & Implementing Classes

Page 2: CSC 205 Java Programming II Defining & Implementing Classes.

Topics

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

Page 3: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 4: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 5: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 6: CSC 205 Java Programming II Defining & Implementing Classes.

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*

Page 7: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 8: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 9: CSC 205 Java Programming II Defining & Implementing Classes.

The Client (or External) View

getFlow isOn shutOff

Page 10: CSC 205 Java Programming II Defining & Implementing Classes.

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

Page 11: CSC 205 Java Programming II Defining & Implementing Classes.

Specification Format

Signature and description Parameters Returns Precondition Postcondition Throws

Page 12: CSC 205 Java Programming II Defining & Implementing Classes.

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