From 4c00a2c0b1c8a39df7d8e4b1dc9de416e868feb4 Mon Sep 17 00:00:00 2001 From: niklas Date: Wed, 29 Nov 2023 19:28:40 +0100 Subject: [PATCH] Uebung 05 --- uebung_05/exercise_01/exercise_01.py | 20 ++++++ uebung_05/exercise_02/exercise_02.py | 51 ++++++++++++++ uebung_05/exercise_03/.exercise_03.py.swp | Bin 0 -> 12288 bytes uebung_05/exercise_03/: | 65 ++++++++++++++++++ uebung_05/exercise_03/exercise_03.py | 79 ++++++++++++++++++++++ 5 files changed, 215 insertions(+) create mode 100644 uebung_05/exercise_01/exercise_01.py create mode 100644 uebung_05/exercise_02/exercise_02.py create mode 100644 uebung_05/exercise_03/.exercise_03.py.swp create mode 100644 uebung_05/exercise_03/: create mode 100644 uebung_05/exercise_03/exercise_03.py diff --git a/uebung_05/exercise_01/exercise_01.py b/uebung_05/exercise_01/exercise_01.py new file mode 100644 index 0000000..f18c0e9 --- /dev/null +++ b/uebung_05/exercise_01/exercise_01.py @@ -0,0 +1,20 @@ +class Ball: + __diameter__ = 0.0 + + ## Methods + # Constructor + def __init__(self, diameter = 1.0): + self.__diameter__ = diameter + pass + + # a simple method + def introduction(self): + print(f"My diameter is {self.__diameter__:.3f}.") + pass + +# main code +a = Ball() +b = Ball(2.0) + +a.introduction() +b.introduction() diff --git a/uebung_05/exercise_02/exercise_02.py b/uebung_05/exercise_02/exercise_02.py new file mode 100644 index 0000000..92290c8 --- /dev/null +++ b/uebung_05/exercise_02/exercise_02.py @@ -0,0 +1,51 @@ +import numpy as np + +class Ball: + __diameter__ = 0.0 + + ## Methods + # Constructor + def __init__(self, diameter = 1.0): + self.__diameter__ = diameter + pass + + # a simple method + def introduction(self): + print(f"My diameter is {self.__diameter__:.3f}.") + pass + + # Getter + def get_diameter(self): + return self.__diameter__ + + # overload __add_ (+) + def __add__(self, other): + d1 = self.get_diameter() + + if(isinstance(other, Ball)): + d2 = other.get_diameter() + combined_volume = 1/6 * np.pi * (d1**3 + d2**3) + elif(isinstance(other, int) or isinstance(other, float)): + combined_volume = 1/6 * np.pi*d1**3 + other + else: + raise ValueError("Invalid summand.") + d_combined = np.power(6 * combined_volume / np.pi, 1/3) + return Ball(d_combined) + + # overload __radd__ (number + Ball) + def __radd__(self, other): + return self + other + +# main code +a = Ball() +b = Ball(2.0) + +c = a + b + +d = a + 5 +e = 5 + a + +a.introduction() +b.introduction() +c.introduction() +d.introduction() diff --git a/uebung_05/exercise_03/.exercise_03.py.swp b/uebung_05/exercise_03/.exercise_03.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..06bd1b19e5ea68a1ea5f40e74f072d300d36ce28 GIT binary patch literal 12288 zcmeI2zmMER6vyWf2|q&km49Gx1bB(h`|D1MPFxibKSUHHAUY~zuV;6y_1e~W%-K2u z5&|Sj%Fu%l1r1W5g9a385Ct`ef`TRpXae7{XLp_br3+1nZmj#9XFT)Xy!V}Fdr!As z>*90g=~=r&a6CxJ2X7D9dmEo^Jn9h=1*6bSy?~G8;Oe8Aw;9EPd2zgY@|!b<&8Ol~ zHfE7v%~WK5kd2!OcSjJYq1zW>&>OPU;jA}|Sk%mzmqi1owcTVn^L?@)mKr5gX&i=Hz9w4-@s4cD)=0H2rh%yfC+wwPFKJs z@CF!yJ@71e21p&hI({1#f(}_LpcT*x{NEKwLSd)Fc;9Rgk|a0uFcPu>i+ocAB4l)z z8mDR8r!NmVbN$q45IH@J2l#=x>=dEv{bwKZhSC`eO`}jN<*J{nei@Z%ych?OkkOP8 zBw;MfWZNDv;pAq+qN-iUwvl8!37KKhvCBt{%amf}tuoxI8CLB>8g^@jRl71QSQy6p zYgX*k46Al!IN*#$YwX%JyQ*E;#oUbsiz$+18`&mXHitfAR}AeWpiSDrzHMQkL!Y2Y zhn}HHmu}KF!D*M4%2KmymD4(tD`wlGZP>M^7oa^=2WKMV5lvaxw;iW&oN~lxeJXdP zFy%mFXVv_CZhpRMei@pXTeN6Qmo5+w-7IrUgUIxml(2!sR4{Ok1k3SWoGy+}f|Z%% z(%0&#HCIcQnz)jAOTlFX7F7`~&mM8cV#xSZFUR5IEOZ=YTn^TilVNffU)iJ=-7sTM za~^ZkxR5XufKSI+DyYY*J+vrp5gK#a4`Wx@sCa2C4;H3uhm=kAOKo5!a7{TCo>f;< zU8}nb`ZRzfsc@qnGs~dM)f;m8#gQ@~WOWk}psXPOvN)=On?`V8tI%~f+Pj+?6H>F&#iV9Rbz`|?3H=~r90b{lSpc5`<7 zTXfPvgw-8Zd5hRUZEF45q}=ok^I+y<$L{toS(6L!Q(kBoWvr^a1YhD!xsclI(qfw0 z%rh?MH7`LFh-sxM0q~}%l$eBBzMN{}lus4a@8Ns;Y4zyD!#WrzF&FY}AU`97frl@} P%NBK0iq9;y@geyaL^Uo+ literal 0 HcmV?d00001 diff --git a/uebung_05/exercise_03/: b/uebung_05/exercise_03/: new file mode 100644 index 0000000..5c4e9eb --- /dev/null +++ b/uebung_05/exercise_03/: @@ -0,0 +1,65 @@ +import math + +class Point: + __x__ = 0.0 + __y__ = 0.0 + + def __init__(self, x = 0.0, y = 0.0): + self.__x__ = x + self.__y__ = y + + def __str__(self): + return f"({self.__x__:.3f},{self.__y__:.3f})" + + def __neg__(self): + return Point(-self.__x__, -self.__y__) + + def __add__(self, other): + x = 0.0 + y = 0.0 + + if isinstance(other, Point): + return Point(self.__x__ + other.__x__, self.__y__ + other.__y__) + elif isinstance(other, int) or isinstance(other, float): + return Point(self.__x__ + other, self.__y__ + other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __radd__(self, other): + return self + other + + def __sub__(self, other): + if isinstance(other, Point) or isinstance(other, int) or isinstance(other, float): + return self + (-other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __rsub__(self, other): + return self - other + + def __mul__(self, other): + if isinstance(other, Point): + return Point(self.__x__ * other.__x__, self.__x__ * other.__y__) + elif isinstance(other, int) or isinstance(other, float): + return Point(self.__x__ * other, self.__x__ * other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __rmul__(self, other): + return self * other + + def rot(self, degree): + x = math.cos(degree) * self.__x__ + math.sin(degree) * self.__y__ + y = -math.sin(degree) * self.__x__ + math.cos(degree) * self.__y__ + return Point(x, y) + + def get_x(self): + return self.__x__ + + def get_y(self): + return self.__y__ + +p1 = Point() +p2 = Point(1, 1) + +print(p2.rot(math.pi * 2)) diff --git a/uebung_05/exercise_03/exercise_03.py b/uebung_05/exercise_03/exercise_03.py new file mode 100644 index 0000000..53fcd5e --- /dev/null +++ b/uebung_05/exercise_03/exercise_03.py @@ -0,0 +1,79 @@ +import math +import matplotlib.pyplot as plt + +class Point: + __x__ = 0.0 + __y__ = 0.0 + + def __init__(self, x = 0.0, y = 0.0): + self.__x__ = x + self.__y__ = y + + def __str__(self): + return f"({self.__x__:.3f},{self.__y__:.3f})" + + def __neg__(self): + return Point(-self.__x__, -self.__y__) + + def __add__(self, other): + x = 0.0 + y = 0.0 + + if isinstance(other, Point): + return Point(self.__x__ + other.__x__, self.__y__ + other.__y__) + elif isinstance(other, int) or isinstance(other, float): + return Point(self.__x__ + other, self.__y__ + other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __radd__(self, other): + return self + other + + def __sub__(self, other): + if isinstance(other, Point) or isinstance(other, int) or isinstance(other, float): + return self + (-other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __rsub__(self, other): + return self - other + + def __mul__(self, other): + if isinstance(other, Point): + return Point(self.__x__ * other.__x__, self.__x__ * other.__y__) + elif isinstance(other, int) or isinstance(other, float): + return Point(self.__x__ * other, self.__x__ * other) + else: + raise ValueError("Operand must be Point, int or float.") + + def __rmul__(self, other): + return self * other + + def rot(self, degree): + x = math.cos(degree) * self.__x__ + math.sin(degree) * self.__y__ + y = -math.sin(degree) * self.__x__ + math.cos(degree) * self.__y__ + return Point(x, y) + + def get_x(self): + return self.__x__ + + def get_y(self): + return self.__y__ + +p1 = Point() +p2 = Point(1, 1) +p3 = Point (3, 2) +p4 = 2 * p2 + p3 - 1 +p5 = p4.rot(math.pi / 2) + +plt.plot(p1.get_x(), p1.get_y(), "orange", marker = "o") +plt.plot(p2.get_x(), p2.get_y(), "green", marker = "o") +plt.plot(p3.get_x(), p3.get_y(), "yellow", marker = "o") +plt.plot(p4.get_x(), p4.get_y(), "purple", marker = "o") +plt.plot(p5.get_x(), p5.get_y(), "purple", marker = "o") + +plt.title("Points") +plt.xlabel("x") +plt.ylabel("y") + +plt.show()