Sale!

# Homework 02 METU CENG310

\$30.00

Category:

## Description

Homework 02
METU CENG310
Data Structures and Algorithms with Python

Write a Vector class that can handle various vector operations. You can (and should) start with the basic
implementation shown in subsection 2.3.3 in the textbook.
Task-1: Copy the Code Fragment 2.4 from the book and make sure that the basic Vector class works. Simple
test operations would be as listed:
Enrich the Vector class such that it supports vector subtraction, negation, addition, multiplication, and dot
product operations.
Task-2: Implement the __sub__ method for the Vector class so that the expression u-v returns a new vector instance
representing the difference between two vectors.
Task-3: Implement the __neg__ method for the Vector class so that the expression -v returns a new vector instance
whose coordinates are all the negated values of the respective coordinates of v.
Task-4: The Vector implementation in Section 2.3.3 supports a syntax such as v = u + [5, 3, 10, -2, 1], in which
the sum of a vector and list returns a new vector. However, the syntax v = [5, 3, 10, -2, 1] + u is illegal. Explain
how the Vector class definition can be revised so that this syntax generates a new vector.
Task-5: Implement the __mul__ method for the Vector class so that the expression v * 3 returns a new vector
with coordinates that are 3 times the respective coordinates of v.
Task-6: Implement the __rmul__ method, to provide additional support for syntax 3 * v.
Task-7: Enhance the __mul__ method for the Vector class so that the expression u * v returns a scalar that
represents the dot product of the vectors.
Task-8: Modify the constructor so that if a single integer is sent, it produces a vector of that dimension with all
zeros (e.g., Vector(3) should produce <0,0,0> vector), but if a sequence of numbers is provided, it produces a
vector with coordinates based on that sequence (e.g.,Vector([1,2,3]) should produce <1,2,3> vector).
Example Test Statements (\$> denotes the console prompt.):
1 \$> u = Vector (3)
2 \$> v = Vector (3)
3 \$> u [0] = 1
4 \$> v [1] = 1
5 \$> u + v
1
6 \$> u + [1 ,1 ,1]
7 \$> [1 ,1 ,1] + u
8 \$> u – v
9 \$> u * 3
10 \$> 3 * u
11 \$> u * v
12 \$> -u
2 Class Inheritance
Introduction: For this assignment, you are asked to develop a Python program that creates shapes.
You are expected to create the Point class to represent a location on the coordinate system. Point class has two
values; x and y. Point objects are initialized as follows:
1 p = Point (x , y);
Shape Class: Shape class has a point variable named leftTop. leftTop point coordinates will be entered by the user
through keyboard input. You can think the top left point as the anchor of the shape.
Shape class also has a points list as well. This list keeps the ordered list of vertices (in the top left, top right,
bottom right and bottom left order) for a shape. For each shape, calculatePoints() function defines the points and
adds them to the list. calculateArea() and calculatePerimeter() functions calculate the area and perimeter for the
shape. For moving an object from one location to another with respect to the leftTop point, move() function needs
to be implemented. Do not forget, you do not know how to fill these four functions in Shape class. Because
you do not know the type of it. So, these functions need to be implemented as abstract in Shape class. You
need to override these functions in Circle and Rectangle classes. You can see the class hierarchy in the figure below.
You can see the demo about the details of __str__ implementation.
Calculating The Points: The following figure shows an example for a rectangle created with leftTop point
(100,100), height of 50 and width of 200. The annotations were added later. Rectangle objects initialized as
follows:
1 r = Rectangle ( leftTop , height , width );
You need to keep the points of rectangular objects in the points List, described under Shape class. Again,
the order should be: top left, top right, bottom right and bottom left.
2
For the Circle class, there are only two points that you need to keep track of; top left and bottom right
as shown below. You can populate the points list with that order, using just 2 points. Circle objects initialized
as follows:
1 r = Circle ( leftTop , radius );
Scenario: The program will ask the user, type of shape in an infinite loop. The user has 3 different command
options here. Command r for the selecting rectangle, c for the selecting circle, and q to exit the program.
If the command r is typed, the program would ask for the coordinates for the leftTop (x and y), height and width.
Information on the rectangle will be printed afterward. Then it will ask for the new leftTop coordinate to move
the rectangle. After the moving operation done by the program new information of the rectangle will be printed.
If the command c is typed, the program would ask for the coordinates for the leftTop (x and y) and radius
. Information on the circle will be printed afterward. Then it will ask for the new leftTop coordinate to move
the circle. After the moving operation done by the program new information on the circle will be printed.
The program will repeat this scenario again and again until the q command is typed.
Demo: Red ones are inputs and after the demo part, there is a further instructions part. Do not forget
to look at that part before submitting your work.
1 Type of Shape (q for exit ): r
2
3 Coordinate ( leftTop ) , height and width : 100 100 50 100
4 — Rectangle —
5 Height : 50
6 Width : 100
7 Left Top Point : (100 ,100)
8 Area : 5000.00
9 Perimeter : 300.00
10 Points : (100 ,100) (200 ,100) (200 ,150) (100 ,150)
11
12 Move object to the new coordinate ( leftTop ): 30 40
13 — Rectangle —
14 Height : 50
15 Width : 100
16 Left Top Point : (30 ,40)
17 Area : 5000.00
18 Perimeter : 300.00
19 Points : (30 ,40) (130 ,40) (130 ,90) (30 ,90)
20
21 Type of Shape (q for exit ): c
22
23 Coordinates ( leftTop ) and radius : 20 20 30
24 — Circle —
26 Left Top Point : (20 ,20)
27 Area : 2827.43
28 Perimeter : 188.50
29 Points : (20 ,20) (80 ,80)
30
31 Move object to the new coordinate ( leftTop ): 100 150
32 — Circle —
34 Left Top Point : (100 ,150)
35 Area : 2827.43
36 Perimeter : 188.50
37 Points : (100 ,150) (160 ,210)
38
39 Type of Shape (q for exit ): c
40
41 Coordinates ( leftTop ) and radius : 564 742 10
42 — Circle —
44 Left Top Point : (564 ,742)
45 Area : 314.16
3
46 Perimeter : 62.83
47 Points : (564 ,742) (584 ,762)
48
49 Move object to the new coordinate ( leftTop ): 5 10
50 — Circle —
52 Left Top Point : (5 ,10)
53 Area : 314.16
54 Perimeter : 62.83
55 Points : (5 ,10) (25 ,30)
56
57 Type of Shape (q for exit ): r
58
59 Coordinate ( leftTop ) , height and width : 45 10 200 150
60 — Rectangle —
61 Height : 200
62 Width : 150
63 Left Top Point : (45 ,10)
64 Area : 30000.00
65 Perimeter : 700.00
66 Points : (45 ,10) (195 ,10) (195 ,210) (45 ,210)
67
68 Move object to the new coordinate ( leftTop ): 683 246
69 — Rectangle —
70 Height : 200
71 Width : 150
72 Left Top Point : (683 ,246)
73 Area : 30000.00
74 Perimeter : 700.00
75 Points : (683 ,246) (833 ,246) (833 ,446) (683 ,446)
76
77 Type of Shape (q for exit ): q
3 Further Instructions
Please hand in the results of your homework in text files with .py extension over ODTUClass by 11:59pm on
due date. A Homework-02 page will be generated soon after the start date of this homework and there will be
separate submission sections. One for Operator Overloading and the other for Class Inheritance. Should