Question

In: Computer Science

This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...

This is python

#Create a class called Rectangle. Rectangle should
#have two attributes (instance variables): length and
#width. Make sure the variable names match those words.
#Both will be floats.
#
#Rectangle should have a constructor with two required
#parameters, one for each of those attributes (length and
#width, in that order).
#
#Rectangle should also have a method called
#find_perimeter. find_perimeter should calculate the
#perimeter of the rectangle based on the current values for
#length and width.
#
#perimeter should NOT be an attribute of the class; instead,
#perimeter should be calculated and returned live when the
#ethod find_perimeter is called.
#
#The find_perimeter method should have NO parameters
#besides self. Instead, it should calculate the perimeter
#based on the current values for the opposite and adjacent
#attributes.
#
#Hint: The formula for perimeter is 2 * length + 2 * width.


#Write your class here!

#The code below will test your function. If it works, it
#should print 3.0, 4.0, and 14.0 in that order.
test_rectangle = Rectangle(3.0, 4.0)
print(test_rectangle.length)
print(test_rectangle.width)
print(test_rectangle.find_perimeter())

Solutions

Expert Solution

# Create a class called Rectangle. Rectangle should
# have two attributes (instance variables): length and
# width. Make sure the variable names match those words.
# Both will be floats.
#
# Rectangle should have a constructor with two required
# parameters, one for each of those attributes (length and
# width, in that order).
#
# Rectangle should also have a method called
# find_perimeter. find_perimeter should calculate the
# perimeter of the rectangle based on the current values for
# length and width.
#
# perimeter should NOT be an attribute of the class; instead,
# perimeter should be calculated and returned live when the
# ethod find_perimeter is called.
#
# The find_perimeter method should have NO parameters
# besides self. Instead, it should calculate the perimeter
# based on the current values for the opposite and adjacent
# attributes.
#
# Hint: The formula for perimeter is 2 * length + 2 * width.


class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def find_perimeter(self):
        return 2 * (self.length + self.width)


# The code below will test your function. If it works, it
# should print 3.0, 4.0, and 14.0 in that order.
test_rectangle = Rectangle(3.0, 4.0)
print(test_rectangle.length)
print(test_rectangle.width)
print(test_rectangle.find_perimeter())



Related Solutions

#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make...
#Create a class called FrapOrder. FrapOrder should #have two attributes (instance variables): size and #extra_shots. Make sure the variable names match those #words. size will be a character, either "S", "M", or "L". #extra_shots will be an integer. # #FrapOrder should have a constructor with two required #parameters, one for each of those attributes (size and #extra_shots, in that order). # #FrapOrder should also have a method called get_total. #get_total should calculate the total cost of the order. #If size...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.”
Programming Problem 2 - Cycle[A] Create a class called “Cycle” which has two instance integer variables as properties, “numberOfWheels” and “weight.” Create a constructor with two parameters, using the same variable names in the parameter list. Assign each variable to numberOfWheels” and “weight” respectively. Write a separate application to test the class and display its properties. Note: Do not change the names of the instance variables or the variables listed in the constructor’s parameter list.[B] Edit your class Cycle by...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables:...
Using C# Create a class called Artist that contains 4 pieces of information as instance variables: name (datatype string), specialization – i.e., music, pottery, literature, paintings (datatype string), number of art items (datatype int), country of birth (datatype string). Create a constructor that initializes the 4 instance variables. The Artist class must contain a property for each instance variable with get and set accessors. The property for number should verify that the Artist contributions is greater than zero. If a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT