initial commit
This commit is contained in:
commit
ba1b4b80df
76
README.md
Normal file
76
README.md
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
### Assignment
|
||||||
|
|
||||||
|
In this project you will use object oriented programming to create a Rectangle class and a Square class. The Square class should be a subclass of Rectangle and inherit methods and attributes.
|
||||||
|
|
||||||
|
#### Rectangle class
|
||||||
|
When a Rectangle object is created, it should be initialized with `width` and `height` attributes. The class should also contain the following methods:
|
||||||
|
* `set_width`
|
||||||
|
* `set_height`
|
||||||
|
* `get_area`: Returns area (`width * height`)
|
||||||
|
* `get_perimeter`: Returns perimeter (`2 * width + 2 * height`)
|
||||||
|
* `get_diagonal`: Returns diagonal (`(width ** 2 + height ** 2) ** .5`)
|
||||||
|
* `get_picture`: Returns a string that represents the shape using lines of "\*". The number of lines should be equal to the height and the number of "\*" in each line should be equal to the width. There should be a new line (`\n`) at the end of each line. If the width or height is larger than 50, this should return the string: "Too big for picture.".
|
||||||
|
* `get_amount_inside`: Takes another shape (square or rectangle) as an argument. Returns the number of times the passed in shape could fit inside the shape (with no rotations). For instance, a rectangle with a width of 4 and a height of 8 could fit in two squares with sides of 4.
|
||||||
|
|
||||||
|
Additionally, if an instance of a Rectangle is represented as a string, it should look like: `Rectangle(width=5, height=10)`
|
||||||
|
|
||||||
|
#### Square class
|
||||||
|
The Square class should be a subclass of Rectangle. When a Square object is created, a single side length is passed in. The `__init__` method should store the side length in both the `width` and `height` attributes from the Rectangle class.
|
||||||
|
|
||||||
|
The Square class should be able to access the Rectangle class methods but should also contain a `set_side` method. If an instance of a Square is represented as a string, it should look like: `Square(side=9)`
|
||||||
|
|
||||||
|
Additionally, the `set_width` and `set_height` methods on the Square class should set both the width and height.
|
||||||
|
|
||||||
|
#### Usage example
|
||||||
|
```py
|
||||||
|
rect = shape_calculator.Rectangle(10, 5)
|
||||||
|
print(rect.get_area())
|
||||||
|
rect.set_height(3)
|
||||||
|
print(rect.get_perimeter())
|
||||||
|
print(rect)
|
||||||
|
print(rect.get_picture())
|
||||||
|
|
||||||
|
sq = shape_calculator.Square(9)
|
||||||
|
print(sq.get_area())
|
||||||
|
sq.set_side(4)
|
||||||
|
print(sq.get_diagonal())
|
||||||
|
print(sq)
|
||||||
|
print(sq.get_picture())
|
||||||
|
|
||||||
|
rect.set_height(8)
|
||||||
|
rect.set_width(16)
|
||||||
|
print(rect.get_amount_inside(sq))
|
||||||
|
```
|
||||||
|
That code should return:
|
||||||
|
```
|
||||||
|
50
|
||||||
|
26
|
||||||
|
Rectangle(width=10, height=3)
|
||||||
|
**********
|
||||||
|
**********
|
||||||
|
**********
|
||||||
|
|
||||||
|
81
|
||||||
|
5.656854249492381
|
||||||
|
Square(side=4)
|
||||||
|
****
|
||||||
|
****
|
||||||
|
****
|
||||||
|
****
|
||||||
|
|
||||||
|
8
|
||||||
|
```
|
||||||
|
|
||||||
|
The unit tests for this project are in `test_module.py`.
|
||||||
|
|
||||||
|
### Development
|
||||||
|
|
||||||
|
Write your code in `shape_calculator.py`. For development, you can use `main.py` to test your `shape_calculator()` function. Click the "run" button and `main.py` will run.
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
|
||||||
|
We imported the tests from `test_module.py` to `main.py` for your convenience. The tests will run automatically whenever you hit the "run" button.
|
||||||
|
|
||||||
|
### Submitting
|
||||||
|
|
||||||
|
Copy your project's URL and submit it to freeCodeCamp.
|
20
main.py
Normal file
20
main.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# This entrypoint file to be used in development. Start by reading README.md
|
||||||
|
import shape_calculator
|
||||||
|
from unittest import main
|
||||||
|
|
||||||
|
|
||||||
|
rect = shape_calculator.Rectangle(5, 10)
|
||||||
|
print(rect.get_area())
|
||||||
|
rect.set_width(3)
|
||||||
|
print(rect.get_perimeter())
|
||||||
|
print(rect)
|
||||||
|
|
||||||
|
sq = shape_calculator.Square(9)
|
||||||
|
print(sq.get_area())
|
||||||
|
sq.set_side(4)
|
||||||
|
print(sq.get_diagonal())
|
||||||
|
print(sq)
|
||||||
|
|
||||||
|
|
||||||
|
# Run unit tests automatically
|
||||||
|
main(module='test_module', exit=False)
|
6
shape_calculator.py
Normal file
6
shape_calculator.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class Rectangle:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Square:
|
115
test_module.py
Normal file
115
test_module.py
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import unittest
|
||||||
|
import shape_calculator
|
||||||
|
|
||||||
|
|
||||||
|
class UnitTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.rect = shape_calculator.Rectangle(3, 6)
|
||||||
|
self.sq = shape_calculator.Square(5)
|
||||||
|
|
||||||
|
def test_subclass(self):
|
||||||
|
actual = issubclass(shape_calculator.Square, shape_calculator.Rectangle)
|
||||||
|
expected = True
|
||||||
|
self.assertEqual(actual, expected, 'Expected Square class to be a subclass of the Rectangle class.')
|
||||||
|
|
||||||
|
def test_distinct_classes(self):
|
||||||
|
actual = shape_calculator.Square is not shape_calculator.Rectangle
|
||||||
|
expected = True
|
||||||
|
self.assertEqual(actual, expected, 'Expected Square class to be a distinct class from the Rectangle class.')
|
||||||
|
|
||||||
|
def test_square_is_square_and_rectangle(self):
|
||||||
|
actual = isinstance(self.sq, shape_calculator.Square) and isinstance(self.sq, shape_calculator.Square)
|
||||||
|
expected = True
|
||||||
|
self.assertEqual(actual, expected, 'Expected square object to be an instance of the Square class and the Rectangle class.')
|
||||||
|
|
||||||
|
def test_rectangle_string(self):
|
||||||
|
actual = str(self.rect)
|
||||||
|
expected = "Rectangle(width=3, height=6)"
|
||||||
|
self.assertEqual(actual, expected, 'Expected string representation of rectangle to be "Rectangle(width=3, height=6)"')
|
||||||
|
|
||||||
|
def test_square_string(self):
|
||||||
|
actual = str(self.sq)
|
||||||
|
expected = "Square(side=5)"
|
||||||
|
self.assertEqual(actual, expected, 'Expected string representation of square to be "Square(side=5)"')
|
||||||
|
|
||||||
|
def test_area(self):
|
||||||
|
actual = self.rect.get_area()
|
||||||
|
expected = 18
|
||||||
|
self.assertEqual(actual, expected, 'Expected area of rectangle to be 18')
|
||||||
|
actual = self.sq.get_area()
|
||||||
|
expected = 25
|
||||||
|
self.assertEqual(actual, expected, 'Expected area of rectangle to be 25')
|
||||||
|
|
||||||
|
|
||||||
|
def test_perimeter(self):
|
||||||
|
actual = self.rect.get_perimeter()
|
||||||
|
expected = 18
|
||||||
|
self.assertEqual(actual, expected, 'Expected perimeter of rectangle to be 18')
|
||||||
|
actual = self.sq.get_perimeter()
|
||||||
|
expected = 20
|
||||||
|
self.assertEqual(actual, expected, 'Expected perimeter of rectangle to be 20')
|
||||||
|
|
||||||
|
def test_diagonal(self):
|
||||||
|
actual = self.rect.get_diagonal()
|
||||||
|
expected = 6.708203932499369
|
||||||
|
self.assertEqual(actual, expected, 'Expected diagonal of rectangle to be 6.708203932499369')
|
||||||
|
actual = self.sq.get_diagonal()
|
||||||
|
expected = 7.0710678118654755
|
||||||
|
self.assertEqual(actual, expected, 'Expected diagonal of rectangle to be 7.0710678118654755')
|
||||||
|
|
||||||
|
def test_set_atributes(self):
|
||||||
|
self.rect.set_width(7)
|
||||||
|
self.rect.set_height(8)
|
||||||
|
self.sq.set_side(2)
|
||||||
|
actual = str(self.rect)
|
||||||
|
expected = "Rectangle(width=7, height=8)"
|
||||||
|
self.assertEqual(actual, expected, 'Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"')
|
||||||
|
actual = str(self.sq)
|
||||||
|
expected = "Square(side=2)"
|
||||||
|
self.assertEqual(actual, expected, 'Expected string representation of square after setting new values to be "Square(side=2)"')
|
||||||
|
self.sq.set_width(4)
|
||||||
|
actual = str(self.sq)
|
||||||
|
expected = "Square(side=4)"
|
||||||
|
self.assertEqual(actual, expected, 'Expected string representation of square after setting width to be "Square(side=4)"')
|
||||||
|
|
||||||
|
def test_rectangle_picture(self):
|
||||||
|
self.rect.set_width(7)
|
||||||
|
self.rect.set_height(3)
|
||||||
|
actual = self.rect.get_picture()
|
||||||
|
expected = "*******\n*******\n*******\n"
|
||||||
|
self.assertEqual(actual, expected, 'Expected rectangle picture to be different.')
|
||||||
|
|
||||||
|
def test_squaree_picture(self):
|
||||||
|
self.sq.set_side(2)
|
||||||
|
actual = self.sq.get_picture()
|
||||||
|
expected = "**\n**\n"
|
||||||
|
self.assertEqual(actual, expected, 'Expected square picture to be different.')
|
||||||
|
|
||||||
|
def test_big_picture(self):
|
||||||
|
self.rect.set_width(51)
|
||||||
|
self.rect.set_height(3)
|
||||||
|
actual = self.rect.get_picture()
|
||||||
|
expected = "Too big for picture."
|
||||||
|
self.assertEqual(actual, expected, 'Expected message: "Too big for picture."')
|
||||||
|
|
||||||
|
def test_get_amount_inside(self):
|
||||||
|
self.rect.set_height(10)
|
||||||
|
self.rect.set_width(15)
|
||||||
|
actual = self.rect.get_amount_inside(self.sq)
|
||||||
|
expected = 6
|
||||||
|
self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 6.')
|
||||||
|
|
||||||
|
def test_get_amount_inside_two_rectangles(self):
|
||||||
|
rect2 = shape_calculator.Rectangle(4, 8)
|
||||||
|
actual = rect2.get_amount_inside(self.rect)
|
||||||
|
expected = 1
|
||||||
|
self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 1.')
|
||||||
|
|
||||||
|
def test_get_amount_inside_none(self):
|
||||||
|
rect2 = shape_calculator.Rectangle(2, 3)
|
||||||
|
actual = rect2.get_amount_inside(self.rect)
|
||||||
|
expected = 0
|
||||||
|
self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 0.')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue
Block a user