OOP vs. Procedural Programming

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which programming paradigm focuses primarily on creating functions or procedures to solve specific tasks?

  • Procedural Programming (correct)
  • Data Encapsulation
  • Universal Modeling Language (UML)
  • Object-Oriented Programming (OOP)

Object-oriented programming generally offers faster execution speeds compared to procedural programming due to its streamlined approach.

False (B)

What term describes the bundling of data and methods that operate on that data within a class, protecting it from outside access?

Encapsulation

In object-oriented programming, a(n) ______ is a template or blueprint for creating objects.

<p>class</p> Signup and view all the answers

Match the following terms with their descriptions within the context of object-oriented programming:

<p>Class = A blueprint for creating objects Object = An instance of a class Attribute = A characteristic or property of an object Method = A function associated with an object</p> Signup and view all the answers

Which of the following is a disadvantage of procedural programming?

<p>Difficult to manage complex programs. (D)</p> Signup and view all the answers

Universal Modeling Language (UML) is primarily used in procedural programming to model system workflows.

<p>False (B)</p> Signup and view all the answers

What is the primary difference between accessor and mutator methods in the context of data encapsulation?

<p>Accessor methods retrieve attribute values while mutator methods set them.</p> Signup and view all the answers

The __init__ method in a class serves as a(n) ______, initializing the object's attributes when an object is created.

<p>constructor</p> Signup and view all the answers

Match the following concepts with their relevance to either Procedural or Object-Oriented Programming:

<p>Functions = Procedural Classes = Object-Oriented Top-down design = Procedural Encapsulation = Object-Oriented</p> Signup and view all the answers

Which of the following is NOT a typical attribute of a 'Loan' class, as discussed?

<p>Borrower's Credit Score. (D)</p> Signup and view all the answers

Methods defined within a class can directly access private attributes of other classes without using accessor methods, as long as they are within the same module.

<p>False (B)</p> Signup and view all the answers

Briefly describe the role of UML in object-oriented software development.

<p>UML provides a standard way to visualize and document the design of object-oriented systems.</p> Signup and view all the answers

In object instantiation, the process of creating a new object from a class is often referred to as ______ an object.

<p>constructing</p> Signup and view all the answers

Match the advantages with their respective programming approaches:

<p>Faster execution = Procedural Code reusability = Object-Oriented Top-down approach = Procedural Data integration = Object-Oriented</p> Signup and view all the answers

In event-driven programming, what primarily dictates the order in which the program's code is executed?

<p>User interactions and responses to the GUI. (D)</p> Signup and view all the answers

Which of the following statements accurately describes the role of 'callback' functions in GUI programming?

<p>They are executed in response to specific events triggered by user interactions. (B)</p> Signup and view all the answers

What is the significance of the __init__ constructor within a tkinter-based GUI class in Python?

<p>It acts as a central switchboard, initializing the main application window and other widgets. (C)</p> Signup and view all the answers

What is the primary function of the place(x, y) method in tkinter when positioning widgets?

<p>Specifies the exact pixel coordinates from the top-left edge of the main window. (D)</p> Signup and view all the answers

In tkinter, what does the get() method accomplish when used with an Entry widget?

<p>Retrieves the current content entered by the user in the Entry widget. (D)</p> Signup and view all the answers

What purpose does the focus() method serve when applied to an Entry widget in tkinter?

<p>It makes the Entry widget active and ready to receive user input. (A)</p> Signup and view all the answers

What is the primary role of a Frame widget in tkinter?

<p>To contain and organize other widgets into logical groupings. (B)</p> Signup and view all the answers

How does the IntVar class facilitate communication between the code and radio buttons in tkinter?

<p>It enables two-way communication by allowing the assignment and retrieval of values from radio buttons. (C)</p> Signup and view all the answers

What is a key distinction between the SINGLE and MULTIPLE selection modes in a tkinter Listbox widget?

<p>SINGLE mode allows only a single item to be selected, while MULTIPLE mode automatically deselects an already selected item when a new item is chosen. (C)</p> Signup and view all the answers

What is the purpose of the destroy method when used in the context of a command button widget in tkinter?

<p>It closes the main application window and terminates the program. (D)</p> Signup and view all the answers

How is the StringVar class primarily utilized in conjunction with a Label widget to display output in tkinter?

<p>It dynamically updates the Label widget's text based on changes to the <code>StringVar</code> object. (A)</p> Signup and view all the answers

In the context of GUI design with Qt Designer, what is the function of the pyuic5 command?

<p>It converts a <code>.ui</code> file created in Qt Designer into a Python code file. (B)</p> Signup and view all the answers

Which of the following best describes the role of the QApplication object in a PyQt GUI application?

<p>It manages the overall event loop and resources of the application. (D)</p> Signup and view all the answers

When using the place() method for exact placement of widgets in tkinter, what do the x and y parameters represent?

<p>The pixel distance from the top-left corner of the parent window. (D)</p> Signup and view all the answers

What is the significance of 'text units' when specifying the width of an Entry widget in tkinter?

<p>It refers to the width in terms of the average character size of the current font. (B)</p> Signup and view all the answers

When using Radiobutton widgets grouped within a Frame in tkinter, what is the standard behavior when a new radio button is selected?

<p>The previously selected radio button within the Frame is automatically deselected. (D)</p> Signup and view all the answers

In the context of displaying dynamic content using a Label widget in tkinter, what is the role of the textvariable property?

<p>It links the Label widget to a <code>StringVar</code> object, enabling dynamic text updates. (C)</p> Signup and view all the answers

What does the term 'event-driven programming' signify in the context of GUI applications?

<p>The program's flow is controlled by the sequence of events triggered by user interactions. (D)</p> Signup and view all the answers

In tkinter, which widget is most suitable for allowing the user to select one or more options from a list of items?

<p>Listbox (A)</p> Signup and view all the answers

In GUI programming with tkinter, assuming a button labeled 'Calculate' is intended to initiate a computational process, how would you typically associate the button click with the function that performs the calculation?

<p>By assigning the function to the command attribute of the button, creating a 'callback' function. (B)</p> Signup and view all the answers

Which characteristic distinguishes object-oriented programming (OOP) from procedural programming?

<p>OOP structures programs around data and methods, while procedural programming focuses on functions. (C)</p> Signup and view all the answers

What is the primary benefit of using data encapsulation in object-oriented programming?

<p>It protects the integrity of data by restricting direct access and requiring the use of methods. (B)</p> Signup and view all the answers

Which of the following accurately describes the relationship between a class and an object?

<p>An object is an instance of a class. (A)</p> Signup and view all the answers

Consider a Loan class with attributes like interest_rate and term. How would you typically prevent direct modification of interest_rate from outside the class in Python?

<p>By prefixing <code>interest_rate</code> with double underscores (e.g., <code>__interest_rate</code>). (B)</p> Signup and view all the answers

Why are accessor methods (getters) and mutator methods (setters) important in object-oriented programming?

<p>They provide controlled access to private attributes, allowing for validation and manipulation. (A)</p> Signup and view all the answers

In the context of the provided Loan class example, what is the purpose of the self parameter in the __init__ method?

<p>It refers to the current object being created. (C)</p> Signup and view all the answers

In UML diagrams for object-oriented design, what do the attributes and methods listed within a class represent?

<p>Attributes represent the data the class holds, and methods represent the actions it can perform. (A)</p> Signup and view all the answers

What is a key advantage of object-oriented programming (OOP) for large and complex systems?

<p>OOP facilitates modular design and easier maintenance through encapsulation and abstraction. (A)</p> Signup and view all the answers

Which term best describes keeping data safe by utilizing getters and setters?

<p>Encapsulation (D)</p> Signup and view all the answers

In GUI programming, what is the significance of 'event-driven programming'?

<p>The program's execution is determined by user interactions and system events. (A)</p> Signup and view all the answers

What role do callback functions play in GUI programming with Tkinter?

<p>They are executed in response to specific events, such as a button click. (D)</p> Signup and view all the answers

In Tkinter, which method is used to retrieve the text entered by the user in an Entry widget?

<p><code>get()</code> (C)</p> Signup and view all the answers

In Tkinter, what is the primary purpose of the IntVar() class when used with radio buttons?

<p>To link multiple radio buttons so only one can be selected at a time and to track the selected value. (D)</p> Signup and view all the answers

Which of the following is the correct way to dynamically update the text displayed in a Label widget in Tkinter?

<p>Use the <code>StringVar()</code> class and link it to the Label's <code>textvariable</code> property, then update the <code>StringVar()</code> value. (C)</p> Signup and view all the answers

What is the function of the pyuic5 command when working with PyQt and Qt Designer?

<p>It converts a Qt Designer <code>.ui</code> file into a Python file. (A)</p> Signup and view all the answers

In relational databases, what is the purpose of a primary key?

<p>To uniquely identify each record within a table. (B)</p> Signup and view all the answers

What is the role of a foreign key in relational databases?

<p>It establishes a link between two tables by referencing the primary key of another table. (B)</p> Signup and view all the answers

Which term describes the process of combining data from multiple tables in a relational database?

<p>Joining (A)</p> Signup and view all the answers

What type of database is SQLite?

<p>A file-based database (D)</p> Signup and view all the answers

In database application development with Python and SQLite, what Tkinter component is typically used for displaying loan details in a tabular format?

<p>Treeview (C)</p> Signup and view all the answers

Flashcards

Procedural Programming

Focuses on creating functions to solve specific tasks, using a top-down design approach.

Advantages of Procedural Programming

A modularized design approach that breaks down problems, suitable for sequential tasks, and relatively fast execution.

Disadvantages of Procedural Programming

Less suited for user-driven problems, limited code reuse, complex code operating on unprotected data, & difficult to manage.

Object-Oriented Programming

Focuses on creating objects that contain both data and procedures (functions).

Signup and view all the flashcards

Advantages of OOP

Data integration, hidden parts of objects, appropriate for user-driven problems, code reuse, higher reliability, and better productivity.

Signup and view all the flashcards

Disadvantages of OOP

Slower execution than functional approach, steep learning curve.

Signup and view all the flashcards

Class (in OOP)

A template for creating objects, providing attributes and methods.

Signup and view all the flashcards

Attributes

Properties or characteristics of an object.

Signup and view all the flashcards

Methods

Actions or behaviors an object can perform.

Signup and view all the flashcards

Object

An instance of a class.

Signup and view all the flashcards

Data Encapsulation

Hiding the internal state and protecting the data of an object. Accessed through Accessor and Mutator methods

Signup and view all the flashcards

Object Constructors

Special initializer method to set default values for attributes of an object (self, other parameters)

Signup and view all the flashcards

UML

A visual modeling language used to design complex object-oriented systems.

Signup and view all the flashcards

Event-Driven Programming

A programming approach where user interaction determines the program's execution order.

Signup and view all the flashcards

Tkinter Module

A module that comes with Python for creating GUIs.

Signup and view all the flashcards

Root Widget

The main application window in Tkinter, where all widgets are placed.

Signup and view all the flashcards

Label Widgets

Widgets used to display static text in a GUI.

Signup and view all the flashcards

place(x, y) Method

A method for positioning widgets in a GUI using exact pixel coordinates.

Signup and view all the flashcards

Entry Widget

A widget that allows users to enter text.

Signup and view all the flashcards

get() Method

Method that retrieves the content from an Entry widget.

Signup and view all the flashcards

focus() Method

Method that sets the focus to a widget, making it active for user input.

Signup and view all the flashcards

Frame Widget

A container to group other widgets together in a GUI.

Signup and view all the flashcards

Radiobutton Widget

A widget that allows the user to select only one option from a group.

Signup and view all the flashcards

IntVar Class

A Tkinter class used with radio buttons to assign unique values.

Signup and view all the flashcards

Listbox Widget

A widget that displays a list of items for the user to choose.

Signup and view all the flashcards

Command Button Widget

A widget that triggers an action when clicked.

Signup and view all the flashcards

Callback Functions

Functions attached to GUI widgets that get executed when a specific event occurs.

Signup and view all the flashcards

destroy Method

A special function linked to a command button.

Signup and view all the flashcards

Output Label

A widget to display dynamic text, often used for output.

Signup and view all the flashcards

StringVar Class

A Tkinter class to hold dynamic string values for labels.

Signup and view all the flashcards

textvariable Property

A property of the Label widget that links it to a StringVar.

Signup and view all the flashcards

Qt Designer

A tool for visually designing GUIs.

Signup and view all the flashcards

QApplication Object

An object that manages the overall GUI process in Qt.

Signup and view all the flashcards

Encapsulation

Hides internal data, accessed via methods.

Signup and view all the flashcards

DBMS

Software for managing relational databases.

Signup and view all the flashcards

Relational Databases

Tables, records, fields, and relationships.

Signup and view all the flashcards

SQLite

Simple file based database system.

Signup and view all the flashcards

Label

Use this widget to display text in GUI.

Signup and view all the flashcards

Radiobutton

Creates a labeled radio option for user selection.

Signup and view all the flashcards

Study Notes

  • Graphical User Interfaces (GUI) revolutionized the software industry

Introduction to Python GUI

  • User interaction drives program execution in GUIs.
  • Xerox Palo Alto Research Center developed the first GUI in 1979
  • Steve Jobs picked up the GUI idea
  • Apple software engineers release first GUI-based Mac in 1984
  • GUI programs use widgets e.g. labels, entry boxes, and buttons, placed on a window canvas for user interaction.
  • UX/UI designers create visual designs for optimal experience
  • Developers code designs using tools like Python

Event-Driven Programming

  • Event-driven programming is used in GUI programs
  • GUI programs run an infinite loop until terminated
  • Widgets are constantly monitored
  • Associated events are checked e.g. a button click
  • Python code in "callback" functions associated with events.
  • User decides the order of execution in event-driven programs

Tkinter Module

  • Tkinter comes standard installations and is easy to get started with
  • Tkinter doesn't have an easy-to-use designer tool and requires "design-with-code."
  • The program structure can be found in Lect13_Payment_Calc.py.
  • The code is contained in a single class, e.g. PmtCalcGUI()
  • An object like Pmt_calc of the class is instantiated in the main() function, which calls the __init__ constructor.

Main Window

  • The main window is also known as the root widget
  • The PmtCalcGUI() class’s __init__ constructor acts as a main switchboard
  • It creates the main application window (root widget)
  • Basic parameters such as window title and size are set
  • All widgets are placed inside this window
  • User-defined functions are called to create the user interface and implement functionality
  • Static and output labels, input entry boxes, frames, radio buttons, list boxes, and command buttons are created

Exact Placement

  • Geometry Manager manages the exact placement of widgets
  • The place(x, y) method is used for exact placement
  • x,y represent the number of pixels from the left/top edge of the main window
  • Label widgets are used for static text display using the place(x, y) method
  • Mostly uses an add-hoc trial-and-error approac
  • Labels display static text.

Input Entry Widget

  • Input Entry Widget are generic input box where content entered is interpreted as text.
  • There is no control over the kind of the information entered
  • The width of the Entry widget is NOT in pixels but in "text units" with 5 holding '00000'
  • Use get() to retrieve the content of Entry widgets.
  • .focus() sets the input entry active

Frame and Radiobutton

  • Frames are can be use to contain other widgets for organization
  • Radiobutton are able to only selecte on radio button within a frame
  • Selecting another button within a frame deselects the previous one
  • IntVar class assigns unique values to radio buttons.
  • Frames group widgets
  • Radiobuttons allow only one selection within their frame
  • IntVar() tracks the selected radio button's value
  • Two-way communication between code and buttons use
    • Object variable of IntVar class to set() turns on a button
    • Object variable to get() is how to take action

Listbox Widget

  • Listbox display a list of items for user selection
    • Height is number of list items
    • Width is number of text units
  • The options for Listboxe are
    • SINGLE: which allow user to select only a single item from the list
    • MULTIPLE: which allow user can select multiple items, items already selected gets deselected
  • Vertical scrolling with mouse wheel, scrollbar is a separate, non-covered, widget
  • Items are accessed with .get(index)

Command Button Widgets

  • Command Button are used to execute program tasks via appropriate functions
  • Callback functions will do the task. Attaches to the command attribute and predefine function
  • User-defined callback functions gather imputs from widgets, performs needed tasks/stores results
  • The function attaches to the Calculate command button for example.
  • Perdefined callback function is the destory method is the main function for root widgets
  • Typically its attaches to the Quit command button

Output Label

  • Output labels are initialized with an empty non-visible text
  • StringVar class is used to dynamically display output and uses object variable of StrVar class
  • Its a one way of communication. Uses texvariable property of Label widget
  • As soon as the variable of StrVar class is updated, change appears on label
  • Variable typically updated in callback function of a command button
  • Labels are linked with textvariable=StringVar()
  • The value is updated with .set()

Qt Designer

  • PyQt can be installed by pip install pyqt5
  • Qt Designer can be installed by pip install pyqt5-tools
  • When using Windows find designer.exe
  • Note using Qt creator would eat up space on the hard drive
  • Visual GUI is built, then exported to Python with: pyuic5 my_ui.ui -o my_ui.py
  • It Ideal for complex interfaces and logic

Payment Calculator with Qt Designer

  • It is possible to Run Qt Designer and Create UI visually.
  • Use similar object names
  • After it is completed the results are saved is Pmt_Calc.ui file
  • Convert the filetype to Pmt_Calc.py file with following command pyuic5 Pmt_Calc.ui > Pmt_Calc.py
    • Then conver UTF-16 to UTF-8 Encoding
  • Lect13_Pmt_Calc.pyis the main program. Set the UI setting withPmtCalcApp(QDialog) class set is to PmtCalc class
  • The QApplication object manages the overall process

Summary

  • Event-driven programming is define
  • Create GUI through widgets
    • Write the code for buttons etc
    • Test Interface and functionality
  • Structure of Python's GUI -
    • Import tkinter module (Mention the alternative also)
    • Entire program in a single class, construct as a switchboard
    • Create the widget and perform desired task
    • Cover widgets: Label, Entry, Frame, Rediobuttton, Listbox, Button
  • Examine visual GUI with qt Designer
    • Compare and contrast python and tkinter libraries

Intro to Object-Oriented Programming (OOP)

Procedural (Functional) Programming

  • The focus is on Functions that solve specific tasks
  • Pros:
    • Modular design (top-down)
    • Simple for sequential problems
      • Fast execution
  • Cons:
    • Not ideal for user-driven or non-linear problems
    • Poor code reuse (copy/paste)
    • Hard to manage as code complexity grows

Object-Oriented Programming (OOP)

  • Focus: Objects = data + functions (methods)
  • Pros:
    • Data hidden (encapsulation)
    • Methods interact with data consistently
    • Reusable, reliable, extensible
    • Easier to maintain large systems
  • Cons:
    • Slower execution
      • Harder to learn, more complex

Classes and Objects

  • Class: A blueprint for creating objects
    • Attributes: Data (e.g., color, size)
    • Methods: Behavior (e.g., drive, turn)
  • Object: An instance of a class
  • Example: car1 = Car()

Loan Class Example

  • Attributes: Interest rate, term, amount, loan type, period
  • Methods:
    • calc_mth_pmt() calculates the monthly payment
    • calc_remain_balance(per) calculates remaining balance
    • calc_interest_savings(per) calculates the total savings

Defining a Class

  • __init__: Constructor; runs when an object is created
  • self: Refers to current object
  • Private attributes start with __ (e.g., __rate)
  • Encapsulation hides data

Accessors (Getters)

  • Ex:
 def get_rate(self):
  return self.__rate

Mutators (Setters)

  • Ex:
 def set_rate(self, rate):
  if rate > 0:
   self.__rate = rate

Using the Class

  • Example:
loan1 = Loan(0.045, 30, 400000)
payment = loan1.calc_mth_pmt()

UML Diagram for Loan Class

  • Class Name: Loan
  • Attributes: __int_rate, __term
  • Methods: calc_mth_pmt(), accessors/setters

Summary of OOP

  • OOP builds programs using classes and objects
  • Procedural programming is step-by-step and better for simple tasks
  • OOP is better for user-driven, complex, and scalable systems
  • Classes are templates for making objects
  • Encapsulation keeps data safe using getters/setters
  • UML is helpful for designing and visualizing code

Visual Design with Qt Designer

  • Part of PyQt5 (install via pip install pyqt5-tools)
  • GUI built visually, then exported to Python with: pyuic5 my_ui.ui -o my_ui.py
  • Ideal for more complex interfaces
  • Can still add logic with Python after UI is generated

Databases

  • Databases are used in airlines, hospitals, banks, etc.
  • A database is a collection of related tables.
  • Tables consist of fields (columns) and records (rows).
  • A Primary Key is a unique identifier for records (e.g., custID in Customer table).
  • A Foreign Key is a field in one table referring to the primary key in another (e.g., custID in Loans table).
  • Relationships between tables are defined by primary and foreign keys
  • Joining tables combines data from multiple sources using common fields

Basic SQL Queries

  • SELECT * FROM Customer;
  • SELECT firstName, lastName FROM Customer WHERE city='Santa Fe';
  • SELECT COUNT(*) FROM Loan GROUP BY loanType;

Database Management Systems (DBMS)

  • DBMS software manages relational databases like Oracle, SQL Server, MySQL, SQLite, etc.
  • SQLite is a simple, file-based database system

Python and SQLite

  • Import the library sqlite3 to implement
  • Example:
db_conn = sqlite3.connect('Loans.db')
db_cursor = db_conn.cursor()
db_cursor.execute('SELECT * FROM Customer')
results = db_cursor.fetchall()
db_conn.close()

Database Application Development

  • Utilize GUI Components (Tkinter)
    • Listbox for customer selection
      • Treeview table for displaying loan details
      • Radiobuttons for filtering data (e.g., by loan type or city)
      • Events (<>) trigger functions to update displays

Advanced Queries and Summary Functions

Querying multiple tables

  • Example:
SELECT firstName, lastName, amount, mthPmt
FROM Customer INNER JOIN Loan
ON Customer.custID = Loan.custID
WHERE city = 'Santa Fe' AND loanType = 'Mortg';

Summary Queries

  • Count loans by type:
SELECT loanType, COUNT(*) AS NumLoans
FROM Loan
GROUP BY loanType;
  • Calculate average mortgage payment per city:
SELECT city, ROUND(AVG(mthPmt),2) AS avgMthPmt
FROM Customer INNER JOIN Loan
ON Customer.custID = Loan.custID
WHERE loanType = 'Mortg'
GROUP BY city;

Database Notes Summary

  • Relational Databases involve tables, records, fields, and relationships.
  • SQLite is an easy-to-use relational database.
  • Python with SQLite allows querying and managing data.
  • GUI Applications use Tkinter components like Listbox, Treeview, and Radiobuttons.
  • SQL Queries are used for retrieving, filtering, joining, and summarizing data.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser