The document provides examples of defining Python classes to represent geometric shapes like triangles and circles. It includes instructions for initializing classes with parameters, defining methods to calculate properties, and printing class instances. Classes defined include Triangle, Point3D, Rectangle, Circle, and Rocket.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views1 page
Askiseis Class
The document provides examples of defining Python classes to represent geometric shapes like triangles and circles. It includes instructions for initializing classes with parameters, defining methods to calculate properties, and printing class instances. Classes defined include Triangle, Point3D, Rectangle, Circle, and Rocket.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
1.
Follow the steps:
Create a class, Triangle. Its __init__() method should take self, angle1, angle2, and angle3 as arguments. Make sure to set these appropriately in the body of the __init__()method. Create a variable named number_of_sides and set it equal to 3. Create a method named check_angles. The sum of a triangle's three angles is It should return True if the sum of self.angle1, self.angle2, and self.angle3 is equal 180, and False otherwise. Create a variable named my_triangle and set it equal to a new instance of your Triangle class. Pass it three angles that sum to 180 (e.g. 90, 30, 60). Print out my_triangle.number_of_sides and print out my_triangle.check_angles(). 2. Define a Point3D class that inherits from object Inside the Point3D class, define an __init__() function that accepts self, x, y, and z, and assigns these numbers to the member variables self.x,self.y,self.z. Define a __repr__() method that returns "(%d, %d, %d)" % (self.x, self.y, self.z). This tells Python to represent this object in the following format: (x, y, z). Outside the class definition, create a variable named my_point containing a new instance of Point3D with x=1, y=2, and z=3. Finally, print my_point. 3. Write a Python class named Rectangle constructed by a length and width and a method which will compute the area of a rectangle. 4. Write a Python class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle. 5. Let's consider what you'd need to do if you were creating a rocket ship in a game, or in a physics simulation. Example: One of the first things you'd want to track are the x and y coordinates of the rocket. Write your own Python class named Rocket.