Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added Labs/.DS_Store
Binary file not shown.
Binary file added Labs/Lab.2/.DS_Store
Binary file not shown.
1,683 changes: 1,611 additions & 72 deletions Labs/Lab.2/Lab.2.ipynb

Large diffs are not rendered by default.

Binary file added Labs/Lab.3/.DS_Store
Binary file not shown.
453 changes: 364 additions & 89 deletions Labs/Lab.3/Lab.3.ipynb

Large diffs are not rendered by default.

397 changes: 397 additions & 0 deletions Labs/Lab.4/.ipynb_checkpoints/Lab.4-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,397 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lab 4- Object Oriented Programming\n",
"\n",
"For all of the exercises below, make sure you provide tests of your solutions.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Write a \"counter\" class that can be incremented up to a specified maximum value, will print an error if an attempt is made to increment beyond that value, and allows reseting the counter. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error: Attempted to increment beyond maximum value.\n",
"Error: Attempted to increment beyond maximum value.\n"
]
}
],
"source": [
"class SimpleCounter:\n",
" def __init__(self, max_value):\n",
" self.count = 0\n",
" self.max_value = max_value\n",
"\n",
" def increment(self):\n",
" if self.count >= self.max_value:\n",
" print(\"Error: Attempted to increment beyond maximum value.\")\n",
" else:\n",
" self.count += 1\n",
"\n",
" def reset(self):\n",
" self.count = 0\n",
"\n",
"# --- Test ---\n",
"c1 = SimpleCounter(2)\n",
"c1.increment()\n",
"c1.increment()\n",
"c1.increment() # This will print the error\n",
"c1.reset()\n",
"c1.increment() \n",
"c1.increment()\n",
"c1.increment() # This will print the error again"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Copy and paste your solution to question 1 and modify it so that all the data held by the counter is private. Implement functions to check the value of the counter, check the maximum value, and check if the counter is at the maximum."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Value: 2, Is Max?: False\n"
]
}
],
"source": [
"class Counter:\n",
" def __init__(self, max_value):\n",
" self.__count = 0 \n",
" self.__max_value = max_value\n",
"\n",
" def increment(self):\n",
" if self.__count >= self.__max_value:\n",
" print(\"Error: Attempted to increment beyond maximum value.\")\n",
" else:\n",
" self.__count += 1\n",
"\n",
" def reset(self):\n",
" self.__count = 0\n",
"\n",
" def get_value(self):\n",
" return self.__count\n",
"\n",
" def get_max(self):\n",
" return self.__max_value\n",
"\n",
" def is_at_max(self):\n",
" return self.__count == self.__max_value\n",
"\n",
"c2 = Counter(3)\n",
"c2.increment()\n",
"c2.increment()\n",
"print(f\"Value: {c2.get_value()}, Is Max?: {c2.is_at_max()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Implement a class to represent a rectangle, holding the length, width, and $x$ and $y$ coordinates of a corner of the object. Implement functions that compute the area and perimeter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Area: 8, Perimeter: 12.\n"
]
}
],
"source": [
"class Rectangle:\n",
" def __init__(self, length, width, x, y):\n",
" self.__length = length\n",
" self.__width = width\n",
" self.__x = x\n",
" self.__y = y\n",
"\n",
" def get_length(self): return self.__length\n",
" def get_width(self): return self.__width\n",
" def get_x(self): return self.__x\n",
" def get_y(self): return self.__y\n",
"\n",
" def area(self):\n",
" return self.__length * self.__width\n",
" \n",
" def perimeter(self):\n",
" return 2 * (self.__length + self.__width)\n",
" \n",
"# Test\n",
"rect = Rectangle(4, 2, 0, 0)\n",
"print(f\"Area: {rect.area()}, Perimeter: {rect.perimeter()}.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. Implement a class to represent a circle, holding the radius and $x$ and $y$ coordinates of center of the object. Implement functions that compute the area and perimeter of the rectangle. Make all data members private and privide accessors to retrieve values of data members. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. Implement a common base class for the classes implemented in 3 and 4 above which implements all common methods as not implemented functions (virtual). Re-implement your regtangle and circule classes to inherit from the base class and overload the functions accordingly. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. Implement a triangle class analogous to the rectangle and circle in question 5."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7. Add a function to the object classes, including the base, that returns a list of up to 16 pairs of $x$ and $y$ points on the parameter of the object. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8. Add a function to the object classes, including the base, that tests if a given set of $x$ and $y$ coordinates are inside of the object. You'll have to think through how to determine if a set of coordinates are inside an object for each object type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"9. Add a function in the base class of the object classes that returns true/false testing that the object overlaps with another object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10. Copy the `Canvas` class from lecture to in a python file creating a `paint` module. Copy your classes from above into the module and implement paint functions. Implement a `CompoundShape` class. Create a simple drawing demonstrating that all of your classes are working."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"11. Create a `RasterDrawing` class. Demonstrate that you can create a drawing made of several shapes, paint the drawing, modify the drawing, and paint it again. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"12. Implement the ability to load/save raster drawings and demonstate that your method works. One way to implement this ability:\n",
"\n",
" * Overload `__repr__` functions of all objects to return strings of the python code that would construct the object.\n",
" \n",
" * In the save method of raster drawing class, store the representations into the file.\n",
" * Write a loader function that reads the file and uses `eval` to instantiate the object.\n",
"\n",
"For example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class foo:\n",
" def __init__(self,a,b=None):\n",
" self.a=a\n",
" self.b=b\n",
" \n",
" def __repr__(self):\n",
" return \"foo(\"+repr(self.a)+\",\"+repr(self.b)+\")\"\n",
" \n",
" def save(self,filename):\n",
" f=open(filename,\"w\")\n",
" f.write(self.__repr__())\n",
" f.close()\n",
" \n",
" \n",
"def foo_loader(filename):\n",
" f=open(filename,\"r\")\n",
" tmp=eval(f.read())\n",
" f.close()\n",
" return tmp\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo(1,'hello')\n"
]
}
],
"source": [
"# Test\n",
"print(repr(foo(1,\"hello\")))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Create an object and save it\n",
"ff=foo(1,\"hello\")\n",
"ff.save(\"Test.foo\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo(1,'hello')"
]
}
],
"source": [
"# Check contents of the saved file\n",
"!cat Test.foo"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"foo(1,'hello')"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Load the object\n",
"ff_reloaded=foo_loader(\"Test.foo\")\n",
"ff_reloaded"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ds",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.14"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading