Open main menu

CDOT Wiki β

Rchan sandbox

Revision as of 13:56, 2 November 2019 by Rchan (talk | contribs) (Created page with "Category:rchanCategory:OPS435-Python OPS435 Python Lab 7 OBJECTIVES (1) Create functions to process programmer-defined type objects. (2) Binding functions into metho...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

OPS435 Python Lab 7

OBJECTIVES

(1) Create functions to process programmer-defined type objects. (2) Binding functions into methods for programmer-defined type objects.

Overview:

Object-oriented programming is conceptually one level higher than simply structured programming style as you've experienced in Bash or C. In this lab, we're going to look at a few object-oriented programming examples using the Python language.

Reference:

Time object from Think Python Chapter 16 and 17 Date object from OPS435 Assignment 1

Investigation 1: Objects and Functions

Part 1 - Simple Object Class with external functions

In this part, we consider a time object which has three data attributes, namely: hour, minute, and second. The following Python script lab7a.py provides the blue print for building such a time object and also define three external functions that can manipulate the time object.

#!/usr/bin/env python3

class Time:
   """Simple object type for time of the day.
      data attributes: hour, minute, second
   """
   def __init__(self,hour=12,minute=0,second=0):
       """constructor for time object""" 
       self.hour = hour
       self.minute = minute
       self.second = second

def format_time(t):
    """Return time object (t) as a formatted string"""
    return '%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second)

def sum_times(t1, t2):
    """Add two time objests and return the sum."""
    sum = Time(0,0,0)
    sum.hour = t1.hour + t2.hour
    sum.minute = t1.minute + t2.minute
    sum.second = t1.second + t2.second
    
    return sum

def valid_time(t):
    """check for the validity of the time object attributes:
       24 > hour > 0
       60 > minute > 0
       60 > second > 0 """
    if t.hour < 0 or t.minute < 0 or t.second < 0:
       return False
    if t.minute >= 60 or t.second >= 60 or t.hour >= 24:
       return False
    return True
Perform the following steps:
  1. Download or create the above Pythone script lab7a.py in your ~/ops435/lab7 directory.
  2. Create a new Python script named lab7a1.py in the lab7 directory:
cd ~/ops435/lab7
vi ~/ops435/lab7/lab7a1.py
  1. Place the following content inside the new python file lab7a1.py and save it:
    #!/usr/bin/env python3
    # Student ID: [seneca_id]
    from lab7a import *
    t1 = Time(8,0,0)
    t2 = Time(8,55,0)
    t3 = Time(9,50,0)
    
    td = Time(0,50,0)
    
    tsum1 = sum_times(t1,td)
    tsum2 = sum_times(t2,td)
    tsum3 = sum_times(t3,td)   
    
    ft = format_time
    print(ft(t1),'+',ft(td),'-->',ft(tsum1))
    print(ft(t2),'+',ft(td),'-->',ft(tsum2))
    print(ft(t3),'+',ft(td),'-->',ft(tsum3))