Question

import math ''' Finish the code below as described. Use the completed class Square as an...

import math
'''
Finish the code below as described.
Use the completed class Square as an example.
'''

class Square:
    '''
    Each Square has a width and can calculate
    its area, its perimeter, and return a string
    representation of itself.
    '''
    
    def __init__(self, width):
        '''
        (float) -> None
        Create a new Square with the given width.
        '''
        
        self.width = width

    def get_area(self):
        '''
        () -> float
        Return this square's area.
        '''
        
        return self.width*self.width

    def get_perimeter(self):
        '''
        () -> float
        Return this square's perimeter.
        '''
        
        return 4*self.width

    def __str__(self):
        '''
        () -> str
        Return string representation of this square.
        '''
        
        s = "A Square with width {0} ".format(self.width)
        s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
        return s

class Rectangle:
    '''
    Each rectangle has a width and height and
    can calculate its area, its perimeter, and can
    return a string representation of itself.

    Complete the class Rectangle below.
    '''
    
    def __init__(self, width, height):
        '''
        (float, float) -> None
        Create a new rectangle with the given width and height.
        '''

        pass # YOUR CODE GOES HERE

    def get_area(self):
        '''
        () -> float
        Return the area of this rectangle.
        '''
        
        pass # YOUR CODE GOES HERE

    def get_perimeter(self):
        '''
        () -> float
        Return the perimeter of this rectangle.
        '''
        
        pass # YOUR CODE GOES HERE

    def __str__(self):
        '''
        () -> str
        Return a string representation of this rectangle.
        '''
        
        pass # YOUR CODE GOES HERE


class Circle:
    '''
    Each circle has a radius and can calculate
    its area, its perimeter, and can
    return a string representation of itself.
    
    Complete class Circle below.
    '''
    
    def __init__(self, radius):
        '''
        (float) -> None
        Create a new Circle with the given radius.
        '''
        
        self.radius = radius

    # YOUR CODE GOES HERE
    # YOU NEED TO DEFINE get_area, get_perimeter, __str__

    # define get_area(self) below
    # Hint: use math.pi

    # define get_perimeter
    # Hint: use math.pi

    # define __str__

class Triangle:
    '''
    Each triangle knows the lengths of its three sides
    and can calculate its area, its perimeter, and can
    return a string representation of itself.
    
    Complete class Triangle below.
    '''

    def __init__(self, side1, side2, side3):
        '''
        (float, float, float) -> None
        Create a new triangle given the lengths of its
        three sides.
        '''
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

    # YOUR CODE GOES HERE
    # define get_area
    # Hint: Use Herons formulae, use math.sqrt(x)
    # p = 1/2 perimeter
    # area = sqrt(p(p-side1)(p-side2)(p-side3))
    # http://www.mathopenref.com/heronsformula.html

    # define get_perimeter

    # define __str__

s1 = Square(100) # This creates a new square with a width of 100; calls Square.__init__(self, 100)

print(str(s1)) # This automatically calls the __str__ method of Square

s2 = Square(5) # Now there are two squares

print("First Square: "+str(s1))
print("Second Square: "+str(s2))

r1 = Rectangle(10,20)
print(str(r1))

c1 = Circle(5)
print(str(c1))

t1 = Triangle(3,4,5)
print(str(t1))

'''
Output of above should be .............................................

A Square with width 100 area 10000 and perimeter 400
First Square: A Square with width 100 area 10000 and perimeter 400
Second Square: A Square with width 5 area 25 and perimeter 20
A Rectangle with width 10 and height 20, area 200 and perimeter 60
A Circle with radius 5 area 78.53981633974483 and perimeter 31.41592653589793
A Triangle with sides 3, 4, 5, area 6.0 and perimeter 12

Process finished with exit code 0
'''
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

import math
'''
Finish the code below as described.
Use the completed class Square as an example.
'''

class Square:
'''
Each Square has a width and can calculate
its area, its perimeter, and return a string
representation of itself.
'''
  
def __init__(self, width):
'''
(float) -> None
Create a new Square with the given width.
'''
  
self.width = width

def get_area(self):
'''
() -> float
Return this square's area.
'''
  
return self.width*self.width

def get_perimeter(self):
'''
() -> float
Return this square's perimeter.
'''
  
return 4*self.width

def __str__(self):
'''
() -> str
Return string representation of this square.
'''
  
s = "A Square with width {0} ".format(self.width)
s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
return s

class Rectangle:
'''
Each rectangle has a width and height and
can calculate its area, its perimeter, and can
return a string representation of itself.

Complete the class Rectangle below.
'''
  
def __init__(self, width, height):
'''
(float, float) -> None
Create a new rectangle with the given width and height.
'''

self.width = width
self.height = height

def get_area(self):
'''
() -> float
Return the area of this rectangle.
'''
  
return self.height*self.width

def get_perimeter(self):
'''
() -> float
Return the perimeter of this rectangle.
'''
  
return 2*(self.height+self.width)

def __str__(self):
'''
() -> str
Return a string representation of this rectangle.
'''
  
s = "A Rectangle with width {0} and height {1}, ".format(self.width,self.height)
s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
return s


class Circle:
'''
Each circle has a radius and can calculate
its area, its perimeter, and can
return a string representation of itself.
  
Complete class Circle below.
'''
  
def __init__(self, radius):
'''
(float) -> None
Create a new Circle with the given radius.
'''
  
self.radius = radius
  
def get_area(self):
'''
() -> float
Return the area of this rectangle.
'''
  
return math.pi*self.radius*self.radius

def get_perimeter(self):
'''
() -> float
Return the perimeter of this rectangle.
'''
  
return 2*math.pi*self.radius
  
def __str__(self):
'''
() -> str
Return a string representation of this rectangle.
'''
  
s = "A Circle with radius {0} ".format(self.radius)
s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
return s

# YOUR CODE GOES HERE
# YOU NEED TO DEFINE get_area, get_perimeter, __str__

# define get_area(self) below
# Hint: use math.pi

# define get_perimeter
# Hint: use math.pi

# define __str__

class Triangle:
'''
Each triangle knows the lengths of its three sides
and can calculate its area, its perimeter, and can
return a string representation of itself.
  
Complete class Triangle below.
'''

def __init__(self, side1, side2, side3):
'''
(float, float, float) -> None
Create a new triangle given the lengths of its
three sides.
'''
self.side1 = side1
self.side2 = side2
self.side3 = side3

# YOUR CODE GOES HERE
# define get_area
# Hint: Use Herons formulae, use math.sqrt(x)
# p = 1/2 perimeter
# area = sqrt(p(p-side1)(p-side2)(p-side3))
# http://www.mathopenref.com/heronsformula.html

# define get_perimeter

# define __str__

def get_area(self):
'''
() -> float
Return the area of this rectangle.
'''
p = self.get_perimeter()/2
return math.sqrt(p*(p-self.side1)*(p-self.side2)*(p-self.side3))

def get_perimeter(self):
'''
() -> float
Return the perimeter of this rectangle.
'''
  
return self.side1+self.side2+self.side3

def __str__(self):
'''
() -> str
Return a string representation of this rectangle.
'''
  
s = "A Triangle with sides {0}, {1}, {2}, ".format(self.side1,self.side2,self.side3)
s = s + "area {0} and perimeter {1}".format(self.get_area(), self.get_perimeter())
return s
  
s1 = Square(100) # This creates a new square with a width of 100; calls Square.__init__(self, 100)

print(str(s1)) # This automatically calls the __str__ method of Square

s2 = Square(5) # Now there are two squares

print("First Square: "+str(s1))
print("Second Square: "+str(s2))

r1 = Rectangle(10,20)
print(str(r1))

c1 = Circle(5)
print(str(c1))

t1 = Triangle(3,4,5)
print(str(t1))

Output

Feel free to reach out regarding any queries . And please do rate the answer . Thank you .

Add a comment
Know the answer?
Add Answer to:
import math ''' Finish the code below as described. Use the completed class Square as an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

  • Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 :...

    Must be in Python 3.6 (please have a screen shot on the code) ex 2.14 : # Enter three points for a triangle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) # Compute the length of the three sides side1 = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) ** 0.5 side2 = ((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Thanks in advance The class below represents a group of shapes. For instance, a group could...

    Thanks in advance The class below represents a group of shapes. For instance, a group could be a square, a rectangle, and a triangle. Implement the area method for the Group class. The area of a group is just the total area of the shapes in the group. For the purpose of this assignment, we will not consider cases where shapes overlap, you simply need to add the areas of the shapes in a group independently. ]:class Group def init...

  • JAVA Code Requried Copy the file java included below. This program will compile, but, when you...

    JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT