ap computer science a – healdsburg high school 1 unit 3 - classes and objects - example

10
1 AP Computer Science A – Healdsburg High School Unit 3 - Classes and Objects - Example

Upload: roy-hallsted

Post on 01-Apr-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

1 AP Computer Science A – Healdsburg High School

Unit 3- Classes and Objects

- Example

Page 2: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

2 AP Computer Science A – Healdsburg High School

OOP

• An OO program models the application as a world of interacting objects.

• An object can create other objects.

• An object can call another object’s (and its own) methods (that is, “send messages”).

• An object has data fields, which hold values that can change while the program is running.

Page 3: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

3 AP Computer Science A – Healdsburg High School

Objects

• Can model real-world objects

• Can represent GUI (Graphical User Interface) components

• Can represent software entities (events, files, images, etc.)

• Can represent abstract concepts (for example, rules of a game, a particular type of dance, etc.)

Page 4: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

4 AP Computer Science A – Healdsburg High School

Classes and Objects

• A class is a piece of the program’s source code that describes a particular type of objects. OO programmers write class definitions.

• An object is called an instance of a class. A program can create and use more than one object (instance) of the same class.

Page 5: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

5 AP Computer Science A – Healdsburg High School

Class Object

• A blueprint for objects of a particular type

• Defines the structure (number, types) of the attributes

• Defines available behaviors of its objects

Attributes

Behaviors

Page 6: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

6 AP Computer Science A – Healdsburg High School

Class: Car Object: a car

Attributes: String model Color color int numPassengers double amountOfGas

Behaviors: Add/remove a passenger Get the tank filled Report when out of gas

Attributes: model = "Mustang" color = Color.YELLOW numPassengers = 0 amountOfGas = 16.5

Behaviors:

Page 7: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

7 AP Computer Science A – Healdsburg High School

Class vs. Object

• A piece of the program’s source code

• Written by a programmer

• An entity in a running program

• Created when the program is running (by the main method or a constructor or another method)

Page 8: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

8 AP Computer Science A – Healdsburg High School

Class vs. Object

• Specifies the structure (the number and types) of its objects’ attributes — the same for all of its objects

• Specifies the possible behaviors of its objects

• Holds specific values of attributes; these can change while the program is running

• Behaves appropriately when called upon

Page 9: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

AP Computer Science A – Healdsburg High School

Introducing The Vic ClassA programmable machine that stores CD’s

and moves them around with operator commands.

Page 10: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example

10 AP Computer Science A – Healdsburg High School

Ex. Write an application program that creates a Vic object and then swaps the CD in its third slot with the CD in its second slot. Assume it has CDs in both slots.