Intro to Object-Oriented Programming
COMP 210 / 2024 Summer Session I / Lecture 03
Guest Lecturers: Ajay Gandecha and Liana Ma
Announcements
In-Class Example
Problem Statement:
Live Demonstration in IntelliJ
Motivations for OOP
Object Oriented Programming
Fields: Describes the attributes of the object.
Ex) color, weight, width, height
Methods: Describes what objects can do (the actions they can perform).
Ex) addWeight(), changeColor()
(??)�OBJECT
Example 1
Fields: breed, name, age
Methods: fetchBone(), digHole()
(??)�OBJECT
Classes (Review from COMP 110)
Fields: Describes the attributes of the object.
Methods: Describes what objects can do (the actions they can perform).
Constructor: Defines how objects are created.
Classes and Objects
Python
Java
Going Back to our Example
(x,y)
(x,y)
(x,y)
Our program created variables for every x and y coordinate for every point in our triangle (6 variables in total)
Let’s make a Point object.� (Represents a triangle in 3 variables!)
Creating the Point class
Fields:
Methods:
x, y
distanceTo(point)
Point
Demo in IntelliJ
Going Back to our Example
Let’s make a Triangle object that stores our three points!
Point
Our new program created variables for every Point in our triangle (3 variables in total)
Point
Point
Creating the Triangle class
Fields:
Methods:
pointA, pointB, pointC
findPerimeter()
Triangle
Demo in IntelliJ
Step 1: Name the Object
Creating a Java Class
Step 2: Declare its Fields
Step 3: Declare a Constructor
Step 4: Define Instance Methods
Anatomy of a Class