Unit 3 Classes and Objects Example 1 AP
Unit 3 - Classes and Objects - Example 1 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. 2 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. ) 3 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. 4 AP Computer Science A – Healdsburg High School
Class • A blueprint for objects of a particular type • Defines the structure (number, types) of the attributes • Defines available behaviors of its objects 5 Object Attributes Behaviors AP Computer Science A – Healdsburg High School
6 Class: Car Object: a car Attributes: String model Color color int num. Passengers double amount. Of. Gas Attributes: model = "Mustang" color = Color. YELLOW num. Passengers = 0 amount. Of. Gas = 16. 5 Behaviors: Add/remove a passenger Get the tank filled Report when out of gas Behaviors: AP Computer Science A – Healdsburg High School
Class • A piece of the program’s source code • Written by a programmer 7 vs. Object • An entity in a running program • Created when the program is running (by the main method or a constructor or another method) AP Computer Science A – Healdsburg High School
Class • 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 8 vs. Object • Holds specific values of attributes; these can change while the program is running • Behaves appropriately when called upon AP Computer Science A – Healdsburg High School
Introducing The Vic Class A programmable machine that stores CD’s and moves them around with operator commands. 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. 10 AP Computer Science A – Healdsburg High School
- Slides: 10