Starting from:

$30

COM1001 Lab 7 -Solved

Write five classes conforming the relation shown in UML class diagram below, i.e. a Rectangle is a Shape, a Circle is a Shape, a Triangle is a Shape, and a Square is a Shape. Write all the class implementations to Shapes.py. Test your code with the sample main function provided to you (Main.py). It must print the sample output provided to you (output.txt). You can also create and test your own main functions.

Details:

In the diagram, class notations consist of three parts; (1) class name, (2) class attributes, (3) class methods. The name of the class appears in the first partition, e.g., Shape, Rectangle etc. Attributes are

shown in the second partition, e.g., name, color, width etc. Methods are shown in the third partition, e.g., get_name(), get_area() etc.

Member functions: 

-          get methods: returns the related attribute of the class. For ex; get_name(self) returns name attribute.

-          set methods: sets the related attribute of the class. For ex; set_name(self, n) assign the input n to self.name.

-          get_area(self): calculates the area of the shape and returns it.

-          get_perimeter(self): calculates the perimeter of the shape and returns it.

-          __str__(self): returns a string representation of the class. For the output format, please check provided output file (output.txt). Note that, print the area and perimeter with 2 digit after decimal point. Hint: '{0:.2f}'.format(x)

Hints: 

-          You can use math library (import math)

Area of rectangle =   

Area of circle =     

Area of triangle =                        
 

Area of square =   

Perimeter of rectangle =   

Perimeter of circle =   

Perimeter of triangle =    

Perimeter of square =   

 Testing: You are provided with an output file. You can test your program from the command line using the following commands. (>: stands for command prompt)  

> python Main.py > my_output.txt                                                          

The outputs, which you generate using the print() function in the source codes, are redirected to my_output.txt file. You can then check if my_output.txt file is exactly the same with the provided output.txt file, using diff command from the command prompt: > diff output.txt my_output.txt  

Good luck. 



 

More products