From a45c80a44c1647f2214267d567bde49886e4cc2a Mon Sep 17 00:00:00 2001 From: Daniel Moix <dwm69@drexel.edu> Date: Tue, 11 Jul 2023 16:57:51 -0400 Subject: [PATCH] EL Demo --- cs172/Lect03/EL.py | 59 ++++++++++++++++++++++++++++++++++++ cs172/Lect03/demo.py | 18 +++++++++++ cs172/Lect03/lect3.py | 34 +++++++++++++++++++++ cs172/Lect03/new.py | 7 +++++ cs172/Lect03/old.py | 10 ++++++ cs172/Lect03/starter_code.py | 30 ++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 cs172/Lect03/EL.py create mode 100644 cs172/Lect03/demo.py create mode 100644 cs172/Lect03/lect3.py create mode 100644 cs172/Lect03/new.py create mode 100644 cs172/Lect03/old.py create mode 100644 cs172/Lect03/starter_code.py diff --git a/cs172/Lect03/EL.py b/cs172/Lect03/EL.py new file mode 100644 index 0000000..90629ae --- /dev/null +++ b/cs172/Lect03/EL.py @@ -0,0 +1,59 @@ + +class EL: + + def __init__(self, inches=0, feet=0, yards=0): + self.__inches = inches % 12 + self.__feet = feet + inches // 12 + self.__yards = yards + self.__feet // 3 + self.__feet = self.__feet % 3 + + def getInches(self): + return self.__inches + + def getFeet(self): + return self.__feet + + def getYards(self): + return self.__yards + + def totalInches(self): + return self.__inches + self.__feet * 12 + self.__yards * 36 + + def addInches(self, additionalInches): + newTotalInches = self.totalInches() + additionalInches + tempLength = EL(newTotalInches) + self.__yards = tempLength.getYards() + self.__feet = tempLength.getFeet() + self.__inches = tempLength.getInches() + + def __add__(self, otherEL): + newEL = EL(self.totalInches() + otherEL.totalInches()) + return newEL + +if __name__ == "__main__": + len1 = EL(39) + print(len1.getInches(), "inches") + print(len1.getFeet(), "feet") + print(len1.getYards(), "yards") + print(len1.totalInches(), "inches total") + + print("Adding 20 inches...") + len1.addInches(20) + + print(len1.getInches(), "inches") + print(len1.getFeet(), "feet") + print(len1.getYards(), "yards") + print(len1.totalInches(), "inches total") + + print("Making 2 EL objects to add together") + lenA = EL(11) + lenB = EL(9) + + lenC = lenA + lenB + + print(lenC.getInches(), "inches") + print(lenC.getFeet(), "feet") + print(lenC.getYards(), "yards") + print(lenC.totalInches(), "inches total") + + \ No newline at end of file diff --git a/cs172/Lect03/demo.py b/cs172/Lect03/demo.py new file mode 100644 index 0000000..97f6ed4 --- /dev/null +++ b/cs172/Lect03/demo.py @@ -0,0 +1,18 @@ + +import lect3 + +a = lect3.Clock() +b = lect3.Clock() + +a.setTime(10, 10) + +b.setTime(1, 7) + +print(b.getTime()) + +left = 22 +right = 7 + +print(left + right) + +print(a + b) \ No newline at end of file diff --git a/cs172/Lect03/lect3.py b/cs172/Lect03/lect3.py new file mode 100644 index 0000000..dcb37e9 --- /dev/null +++ b/cs172/Lect03/lect3.py @@ -0,0 +1,34 @@ + +import math + +class Clock: + + def __init__(self): + self.__hours = 12 + self.__minutes = 0 + + def getHours(self): + return self.__hours + + def getMinutes(self): + return self.__minutes + + def getTime(self): + return f"{self.__hours}:{str(self.__minutes).zfill(2)}" + + def setTime(self, newHours, newMinutes): + self.__hours = abs(newHours) % 13 + self.__minutes = abs(newMinutes) % 60 + + #TODO: Fix this math + def nextMinute(self): + self.__minutes += 1 + if self.__minutes > 59: + self.__minutes = 0 + self.__hours += 1 + self.__hours = self.__hours % 13 + +if __name__ == "__main__": + myClock = Clock() + myClock.setTime(10, 2) + print(myClock.getTime()) diff --git a/cs172/Lect03/new.py b/cs172/Lect03/new.py new file mode 100644 index 0000000..8f04655 --- /dev/null +++ b/cs172/Lect03/new.py @@ -0,0 +1,7 @@ + +import old + +print("This code is in the new file") + +old.main() +old.printHappyFace() \ No newline at end of file diff --git a/cs172/Lect03/old.py b/cs172/Lect03/old.py new file mode 100644 index 0000000..6853513 --- /dev/null +++ b/cs172/Lect03/old.py @@ -0,0 +1,10 @@ + +def main(): + print("This code is in the old file") + +def printHappyFace(): + print("😘") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/cs172/Lect03/starter_code.py b/cs172/Lect03/starter_code.py new file mode 100644 index 0000000..52fa0de --- /dev/null +++ b/cs172/Lect03/starter_code.py @@ -0,0 +1,30 @@ +class EL: + + def __init__(self, inches=0, feet=0, yards=0): + self.__inches = inches % 12 + self.__feet = feet + inches // 12 + self.__yards = yards + self.__feet // 3 + self.__feet = self.__feet % 3 + + def getInches(self): + return self.__inches + + def getFeet(self): + return self.__feet + + def getYards(self): + return self.__yards + + def totalInches(self): + return self.__inches + self.__feet * 12 + self.__yards * 36 + + def addInches(self, additionalInches): + newTotalInches = self.totalInches() + additionalInches + tempLength = EL(newTotalInches) + self.__yards = tempLength.getYards() + self.__feet = tempLength.getFeet() + self.__inches = tempLength.getInches() + + def __add__(self, otherEL): + newEL = EL(self.totalInches() + otherEL.totalInches()) + return newEL \ No newline at end of file -- GitLab