Q.2: Python: Classes a) Moment of Inertia Can you come up with centroid calculat
ID: 3834353 • Letter: Q
Question
Q.2: Python: Classes
a) Moment of Inertia
Can you come up with centroid calculation that it includes the calculation of the two moments of inertia, Ix, and Iy.
b) Centroid
Please to write a centroid program which should ask the user to input a user-defined number of rectangles and calculate the centroid of the compound shape. Also, draw the rectangles using "turtle" so you can see if it is doing what you wanted.
c) Rectangle Class
Create a class to describe a rectangle. It doesn't have to be an actual Python class - you can just describe the important elements of the class.
Explanation / Answer
2a) def momentOfInertia():
ix = breadth * math.pow(length, 3)/12
iy = length * math.pow(breadth, 3)/12
2b)
import turtle
# Rectangle class with length and breadth
class Rectangle(object):
#constructor
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
#draw thw rectangle
turtle.forward(self.breadth)
turtle.left(90)
turtle.forward(self.length)
turtle.left(90)
turtle.forward(self.breadth)
turtle.left(90)
turtle.forward(self.length)
turtle.left(90)
#calculating area
def getArea(self):
return self.length*self.breadth
#returns the objects of the class as a string
def __str__(self):
return length + " " + breadth
def calculateCentroid():
x = breadth/2;
y = length/2;
print("centroid: " + x + "," + y);
while(True):
choice = input("1. Enter rectangle values 2. Exit)
if choice == 1:
len = int(input('Enter length: '));
bre = int(input('Enter breadth: ));
rec = Rectangle(len , bre);
rec.calculateCentroid();
elif:
break;
2c)
# Rectangle class with length and breadth
class Rectangle(object):
#constructor
def __init__(self, length, breadth):
self.length = length
self.breadth = breadth
#calculating area
def getArea(self):
return self.length*self.breadth
#returns the objects of the class as a string
def __str__(self):
return length + " " + breadth
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.