1. Write a class named Person that has attributes for name, age, andoccupation. These attributes should follow the Python convention forprivate attributes. Create getters and setters for each attribute. Name yourgetters and setters as get_ followed by the attribute name or set_ followedby the attribute name. 2. Write a Python class Stats that accepts a Python list of values during initialization. Implement methods to calculate the mean, median and mode and return the result.3. Write a Python class called Shape with an abstract method calculateArea(). Create two subclasses, Triangle and Rectangle, that implement the calculateArea() method.4. Write a Python class called Vehicle with properties like brand, model, and year. Implement a method to display the vehicle details.5. Write a Python Calculator class which will accept two values as arguments, then add them, subtract them, multiply them together, or divide them on request. For example :mycalc = MyCalculator( 12, 6); print( mycalc.add()); // Displays 18 print(mycalc.multiply()); // Displays 72
Question
- Write a class named Person that has attributes for name, age, andoccupation. These attributes should follow the Python convention forprivate attributes. Create getters and setters for each attribute. Name yourgetters and setters as get_ followed by the attribute name or set_ followedby the attribute name. 2. Write a Python class Stats that accepts a Python list of values during initialization. Implement methods to calculate the mean, median and mode and return the result.3. Write a Python class called Shape with an abstract method calculateArea(). Create two subclasses, Triangle and Rectangle, that implement the calculateArea() method.4. Write a Python class called Vehicle with properties like brand, model, and year. Implement a method to display the vehicle details.5. Write a Python Calculator class which will accept two values as arguments, then add them, subtract them, multiply them together, or divide them on request. For example :mycalc = MyCalculator( 12, 6); print( mycalc.add()); // Displays 18 print(mycalc.multiply()); // Displays 72
Solution
- To solve this problem, we need to create a class named "Person" with attributes for name, age, and occupation. These attributes should be private, following the Python convention. To achieve this, we can use the double underscore "__" before the attribute names.
Here is an example implementation of the class:
class Person:
def __init__(self, name, age, occupation):
self.__name = name
self.__age = age
self.__occupation = occupation
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
def get_occupation(self):
return self.__occupation
def set_occupation(self, occupation):
self.__occupation = occupation
In this implementation, we have defined the class "Person" with the attributes name, age, and occupation. We have also created getter and setter methods for each attribute, following the naming convention of get_attribute_name() and set_attribute_name().
- To solve this problem, we need to create a class named "Stats" that accepts a Python list of values during initialization. We will implement methods to calculate the mean, median, and mode and return the result.
Here is an example implementation of the class:
from statistics import mean, median, mode
class Stats:
def __init__(self, values):
self.values = values
def calculate_mean(self):
return mean(self.values)
def calculate_median(self):
return median(self.values)
def calculate_mode(self):
return mode(self.values)
In this implementation, we have imported the mean, median, and mode functions from the statistics module. We have defined the class "Stats" with an init method that accepts a list of values. We have also implemented methods to calculate the mean, median, and mode using the imported functions.
- To solve this problem, we need to create a class named "Shape" with an abstract method called "calculateArea()". We will then create two subclasses, "Triangle" and "Rectangle", that implement the "calculateArea()" method.
Here is an example implementation of the classes:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def calculate_area(self):
pass
class Triangle(Shape):
def __init__(self, base, height):
self.base = base
self.height = height
def calculate_area(self):
return 0.5 * self.base * self.height
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def calculate_area(self):
return self.length * self.width
In this implementation, we have imported the ABC (Abstract Base Class) and abstractmethod from the abc module. We have defined the abstract class "Shape" with the abstract method "calculate_area()". We have then created two subclasses, "Triangle" and "Rectangle", that inherit from "Shape" and implement the "calculate_area()" method.
- To solve this problem, we need to create a class named "Vehicle" with properties like brand, model, and year. We will implement a method to display the vehicle details.
Here is an example implementation of the class:
class Vehicle:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def display_details(self):
print(f"Brand: {self.brand}")
print(f"Model: {self.model}")
print(f"Year: {self.year}")
In this implementation, we have defined the class "Vehicle" with the attributes brand, model, and year. We have also implemented a method called "display_details()" that prints the details of the vehicle.
- To solve this problem, we need to create a class named "Calculator" that accepts two values as arguments. We will then implement methods to add, subtract, multiply, and divide these values.
Here is an example implementation of the class:
class Calculator:
def __init__(self, value1, value2):
self.value1 = value1
self.value2 = value2
def add(self):
return self.value1 + self.value2
def subtract(self):
return self.value1 - self.value2
def multiply(self):
return self.value1 * self.value2
def divide(self):
return self.value1 / self.value2
In this implementation, we have defined the class "Calculator" with the attributes value1 and value2. We have implemented methods to add, subtract, multiply, and divide these values.
Similar Questions
1. In a survey of ages of people in a town, the following ages were recorded: 24, 25, 26, 26, 27, 27, 28, 29, 30. Calculate the mean, median, and mode.*5 points26.89; 27 ; 26 and 27 respectively25.90; 28 ; 27 and 28 respectively26.91; 29 ; 28 and 29 respectively27.91; 30 ; 29 and 30 respectively
You are given a dataset of 20 ages of participants in a science competition. The ages are as follows: 34, 29, 45, 42, 19, 38, 27, 23, 55, 34, 42, 51, 38, 29, 42, 45, 19, 56, 29, 34. Calculate the mean, median, and mode of the ages.
Calculate the Mean, Mode, and Median for the following variables: Age, Numberof Trucks (NoTrucks), Number of Drivers (NoDrivers), Frequency of Trips(Ftrips), Speed on Non-Freeway Routes (Spdnonfrwm), and Speed on FreewayRoutes (Spdfrw)
32. The table below shows the age of each employee at a cardboard box factory ID 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 AGE 51 39 34 27 23 43 41 55 24 25 38 17 21 37 35 38 31 24 Calculate the following summary statistics for the age feature using python (num py) code snippet: a. Minimum,Maximum, Range b.Mean and Medianc. Variance and Statnard deviation
The listing of attributes and methods of user-defined class in Python is generated by
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.