0% found this document useful (0 votes)
20 views

Class-Object-Constructor-Object-Oriented-Programming-OOP-in-Python

Pemrograman berbasis objek

Uploaded by

verdian.23052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Class-Object-Constructor-Object-Oriented-Programming-OOP-in-Python

Pemrograman berbasis objek

Uploaded by

verdian.23052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Object-Oriented

Programming (OOP)
Class, Object, Constructor,
Turtle in Python

I Gde Agung Sri Sidhimantra, S.Kom., M.Kom.


Dimas Novian Aditia Syahputra, S.Tr.T., M.Tr.T.
Moch Deny Pratama, S.Tr.Kom., M.Kom.
Binti Kholifah, S.Kom., M.Tr.Kom.
Object-Oriented Programming (OOP) Concepts

1 Encapsulation 2 Inheritance 3 Polymorphism


Bundling data (attributes) and Creating new classes based on The ability of objects of different
methods that operate on that data existing ones, inheriting their classes to respond to the same
within a single unit called a class. characteristics and adding new method call in their own unique
features. way.
Understanding the Concept
of Class
A class is like a blueprint or template that defines the structure and behavior
of objects.

Attributes Methods
Attributes define the Methods define the actions or
characteristics or properties of behaviors that an object can
an object. They store data perform. They are functions
associated with an object. associated with a class.

Object-Oriented Programming (OOP) is a powerful programming paradigm


that uses objects to represent real-world entities and their relationships.
Python, being an OOP language, provides a flexible and intuitive way to
implement OOP concepts.
Class Notation and Syntax
Classes in Python have a specific notation and syntax that helps structure
and organize code.

1 Class Declaration 2 Attributes


The 'class' keyword is used to Attributes are variables
define a class. defined within a class to
represent data.

3 Methods 4 Constructor
Methods are functions The __init__ method is the
defined within a class to constructor, used for
encapsulate behavior. initializing objects.
Defining a Class in Python
A class is defined using the 'class' keyword followed by the class name and
a colon (:).

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")
Initializing Objects with Constructors

The constructor method is usually named __init__ and takes the 'self'
parameter, which represents the object itself, along with other parameters
for initializing attributes.

class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed

def bark(self):
print("Woof!")
Creating Objects from a
Class
Objects are created from a class using the class name followed by
parentheses.

my_dog = Dog("Buddy", "Golden Retriever")


print(my_dog.name) # Output: Buddy
Accessing Class Attributes and Methods
Attributes and methods of an object are accessed using the dot notation (.).

print(my_dog.breed) # Output: Golden Retriever


my_dog.bark() # Output: Woof!
The Importance of Constructors
The constructor is a special method that gets automatically called when an object is created.

Initialization Consistency

It initializes the attributes of an object with default or user- It ensures that objects are created in a consistent state and
specified values. ready to use.
Example of OOP Implementations
in Python
Here's a simple example demonstrating the implementation of OOP principles in Python.

class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year

def start(self):
print(f"The {self.make} {self.model} is starting.")

def stop(self):
print(f"The {self.make} {self.model} has stopped.")

my_car = Car("Toyota", "Camry", 2023)


my_car.start() # Output: The Toyota Camry is starting.
my_car.stop() # Output: The Toyota Camry has stopped.
Practical Examples and
Applications
OOP concepts have wide applications in various domains. Consider using
them for building complex systems.

Game Development Objects can represent characters,


items, and environments.

Web Applications Objects can represent user


accounts, databases, and web
services.

Data Analysis Objects can represent datasets,


statistical models, and
algorithms.
Exploring Turtle
Graphics with OOP
Class Definition for Turtle
Graphics

Turtle Class Constructor Method


Defines the blueprint for turtle Initializes a new turtle object,
objects, specifying their setting its initial state (position,
attributes (position, direction, color, etc.).
color, etc.) and methods (move,
turn, draw, etc.).

Other Methods
Provides functions for controlling the turtle's movement, drawing, and
other actions.
Turtle Object Instantiation
Class Definition
The blueprint for creating turtle objects.

Object Creation
Instantiating a turtle object from the class.

Object Usage
Accessing and manipulating the turtle's attributes and
methods to create graphics.
Turtle Object Attributes and
Methods
Attribute Description

Position The turtle's current location on


the drawing canvas.

Heading The direction the turtle is facing.

Color The color of the turtle's pen.

Speed The turtle's animation speed.

Pen Size The thickness of the turtle's pen.


Turtle Graphics Workflow
1 Initialization
Setting up the drawing canvas and creating a turtle object.

2 Movement and Drawing


Using methods to move the turtle around the canvas and draw lines,
shapes, and other patterns.

3 Customization
Adjusting turtle attributes like color, pen size, and speed to create
desired effects.

4 Finalization
Ending the program and displaying the generated image.
Practical Applications of Turtle Graphics

Educational Tool Interactive Art Game Development

A fun and engaging way to teach Creating dynamic and interactive Building simple games where turtles
programming concepts, especially artwork that responds to user input. interact with each other and the
geometry and design. environment.
Implementing Turtle
Graphics in Python

Code Structure Geometric Concepts


A clear and organized code structure Understanding basic geometric
is essential for readability and concepts is crucial for effectively
maintainability. using Turtle Graphics.

Looping Structures Functions


Loops are used to repeat actions, Functions help to modularize the
allowing the turtle to draw complex code, making it reusable and easier to
patterns and shapes. manage.
Example of Turtle
Implementations in Python
import turtle

class MyTurtle:
def __init__(self, color, shape):
# Membuat objek turtle
self.t = turtle.Turtle() # Object dari class Turtle
self.t.color(color) # Mengatur warna turtle
self.t.shape(shape) # Mengatur bentuk turtle

def maju(self, jarak):


# Method untuk menggerakkan turtle maju
self.t.forward(jarak)

def putar_kanan(self, sudut):


# Method untuk memutar turtle ke kanan
self.t.right(sudut)
Example of Turtle
Implementations in Python
def buat_persegi(self, ukuran):
# Method untuk menggambar persegi
for _ in range(4):
self.maju(ukuran)
self.putar_kanan(90) # Sudut 90 derajat untuk persegi

def selesai(self):
# Method untuk menyelesaikan gambar
turtle.done()

# Membuat objek turtle dengan warna dan bentuk


turtle1 = MyTurtle("blue", "turtle")

# Menggambar persegi dengan ukuran sisi 150


turtle1.buat_persegi(150)

# Menyelesaikan gambar
turtle1.selesai()
Analyzing the Code Structure and Approach

Code Clarity Program Flow


Clearly commented code makes it easier to understand and Understanding the program's execution flow is crucial for
maintain. debugging and improving efficiency.
Conclusion
• Object-Oriented Programming (OOP) is a powerful programming
paradigm that uses objects to represent real-world entities and their
relationships.

• Python, being an OOP language, provides a flexible and intuitive way to


implement OOP concepts.

• Turtle Graphics, coupled with OOP principles, empowers you to create


stunning visual art and explore computational creativity.

• From educational tools to interactive art and game development, the


possibilities are vast.
Tugas
1. Buatlah program yang menampilkan Data Diri dengan atribut: Nama,
Kelas, NIM, Jurusan, Fakultas, dan Kampus dengan menerapkan
konsep PBO (Class, Object, Constructor)

2. Buatlah program yang menampilkan Turle, bergerak segitiga dimana


juga menerapkan konsep PBO (Class, Object, Constructor)
Thank You

I Gde Agung Sri Sidhimantra, S.Kom., M.Kom.


Dimas Novian Aditia Syahputra, S.Tr.T., M.Tr.T.
Moch Deny Pratama, S.Tr.Kom., M.Kom.
Binti Kholifah, S.Kom., M.Tr.Kom.

You might also like