ap computer science a – healdsburg high school 1 unit 3 - classes and objects - example
TRANSCRIPT
![Page 1: AP Computer Science A – Healdsburg High School 1 Unit 3 - Classes and Objects - Example](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022082806/551b77c4550346d31b8b63ee/html5/thumbnails/10.jpg)
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.