From 418a6b9deb6a88dda451c5c74afd0f12d07bd9b0 Mon Sep 17 00:00:00 2001 From: coco3427 Date: Wed, 14 May 2025 13:09:51 -0400 Subject: [PATCH 1/2] fixes issue #162 --- ...bjectOrientedProgrammingDefiningClasses.ptx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx b/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx index 5f0e4c49..6065bf81 100644 --- a/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx +++ b/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx @@ -298,8 +298,22 @@ int main() { Python -def show(self): - print(self.num,"/",self.den) +class Fraction: + + def __init__(self,top = 0,bottom = 1): + + self.num = top + self.den = bottom + def show(self): + print(self.num,"/",self.den) +# main code below +fraca = Fraction(3,5) +fracb = Fraction(3) +fracc = Fraction()# notice no parameters are passed +# notice print(fraca) will give you an unexpected output +fraca.show() +fracb.show() +fracc.show() From b49eefa428c0240635b6a866766051e2ef024e98 Mon Sep 17 00:00:00 2001 From: coco3427 Date: Wed, 14 May 2025 14:38:00 -0400 Subject: [PATCH 2/2] fixed code to follow best practices --- ...jectOrientedProgrammingDefiningClasses.ptx | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx b/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx index 6065bf81..f0d2368d 100644 --- a/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx +++ b/pretext/Introduction/ObjectOrientedProgrammingDefiningClasses.ptx @@ -285,12 +285,16 @@ class Fraction { int main() { Fraction fraca(3, 5); - Fraction fracb(3); - Fraction fracc; //notice there are no parentheses here. - // cout << fraca << endl; //uncomment to see error fraca.show(); + + Fraction fracb(3); fracb.show(); + + Fraction fracc; //notice there are no parentheses here. fracc.show(); + + // cout << fraca << endl; //uncomment to see error + return 0; } @@ -306,14 +310,20 @@ class Fraction: self.den = bottom def show(self): print(self.num,"/",self.den) -# main code below -fraca = Fraction(3,5) -fracb = Fraction(3) -fracc = Fraction()# notice no parameters are passed -# notice print(fraca) will give you an unexpected output -fraca.show() -fracb.show() -fracc.show() + +def main(): + fraca = Fraction(3,5) + fraca.show() + + fracb = Fraction(3) + fracb.show() + + fracc = Fraction()# notice no parameters are passed + fracc.show() + + # notice print(fraca) will give you an unexpected output + +main()