Docsity

Object-Oriented Programming: Classes and Objects for CMPU-101 - Prof. Marc L. Smith, Assignments of Computer Science

Toggle sidebar
bg1
Chapter 2
Classes and Objects
CMPU-101: Problem-Solving and Abstraction
Marc Smith (sect. 52)
Vassar College
Fall 2006
2
Object-Oriented Programming
Idea: Programs should closely model the real world.
Claim: Such programs are easier to write & maintain.
Must be able to translate between:
•Real World !Model !classes
•Information !Data !fields
•Behavior !Interrogation/ !methods
Manipulation
3
Example: A Bank
Object-Oriented Design:
1. Study the problem
2. Identify the major classes of objects involved
Relevant classes:
•Account - place where money is kept
•Person - who the money belongs to
•Employee - person who facilitates bank transactions
4
Wait--we were speaking of Objects…
What is a Class?
•Class
•a set of like objects, or
•a template for creating objects
•Objects
•instances of classes,
•created by instantiation.
An object fills in the open slots of the template.
pf3
pf4
pf5

Partial preview of the text

Download Object-Oriented Programming: Classes and Objects for CMPU-101 - Prof. Marc L. Smith and more Assignments Computer Science in PDF only on Docsity!

Chapter 2

Classes and Objects

CMPU-101: Problem-Solving and Abstraction Marc Smith (sect. 52) Vassar College Fall 2006 2

Object-Oriented Programming

Idea: Programs should closely model the real world. Claim: Such programs are easier to write & maintain. Must be able to translate between:

  • Real World! Model! classes
  • Information! Data! fields
  • Behavior! Interrogation/! methods Manipulation

Example: A Bank

Object-Oriented Design:

  1. Study the problem
  2. Identify the major classes of objects involved Relevant classes:
  • Account - place where money is kept
  • Person - who the money belongs to
  • Employee - person who facilitates bank transactions

Wait--we were speaking of Objects…

What is a Class?

  • Class
    • a set of like objects, or
    • a template for creating objects
  • Objects
    • instances of classes,
    • created by instantiation. An object fills in the open slots of the template.

5

Unified Modeling Language (UML)

ClassName data methods A visual language for describing and designing classes and class relationships Two types of class relationships: is-a, has-a 6

Class Account

Account owner balance accountNumber Account(…) — constructor getBalance deposit withdraw addInterest

A constructor is a special method

used to instantiate a new object

Account owner balance accountNumber Account(…) getBalance deposit withdraw addInterest

owner is of type Person ,

so Account has-a Person

has-a relationship between classes

is-a relationship between classes

Person name ssn dateOfBirth address Person(…) — constructor setName setAddress ! Employee dateHired monthlySalary

An Employee is-a Person

13

A Given Class Allows Only Certain Methods

Account allows getBalance and withdraw. PrintStream allows print and println. 14

Another example

Student name ssn dateOfBirth gpa Sometimes in a UML diagram, we show only the fields of the class, depending on our purpose

A Section of Students

Section roster instructor room timeSlot Section(…) — constructor addStudent(Student) removeStudent(Student) contains(Student) intersect(Section) In this case, roster is a collection of Student s - - a one-to-many, has-a relationship! (one Section has many Student s)

Naming conventions: Identifiers

Composed of: letters digits underscore ("_") Must begin with a letter. Examples: timeToLive bistro U MAXIMUM_VALUE

17

Naming conventions: Capitalization

Classes — all words are capitalized Account BankAccount ClosedCurve Matrix3By Instances — the first word is not capitalized cs numberOfStudents averageAge taxRate 18

Categories of Identifiers

Reserved words class, public, private, static, … (see Appendix Two) Names defined by other programmers in the API System.out, println, PrintStream Names we select

Brainstorm!

Given a problem, we must answer the questions: What are the classes? What are the methods? What are the instance variables? Written assignment: choose from Chapter 2 Exercises