Write a program that would print the information (name, year of joining, salary, address) of threeemployees by creating a class named 'Employee
Question
Write a program that would print the information (name, year of joining, salary, address) of three employees by creating a class named 'Employee'
Solution
1. Break Down the Problem
To create a program that prints information for three employees using a class, we need to:
- Define a class named Employee.
- Create attributes for name,year_of_joining,salary, andaddress.
- Implement a constructor to initialize these attributes.
- Create a method to display this information.
- Create three instances of the Employeeclass and call the display method for each.
2. Relevant Concepts
- Class and Objects: Understanding how to define a class in Python and how to create objects (instances) of that class.
- Instance Attributes: Using __init__to initialize the attributes of the class.
- Methods: Creating methods to perform actions related to the class (like displaying information).
3. Analysis and Detail
Below is a Python program that follows the steps outlined:
class Employee:
    def __init__(self, name, year_of_joining, salary, address):
        self.name = name
        self.year_of_joining = year_of_joining
        self.salary = salary
        self.address = address
    def display_info(self):
        print(f"Name: {self.name}")
        print(f"Year of Joining: {self.year_of_joining}")
        print(f"Salary: {self.salary}")
        print(f"Address: {self.address}\n")
# Creating instances of Employee
employee1 = Employee("Alice", 2020, 50000, "123 Elm St, Springfield")
employee2 = Employee("Bob", 2018, 55000, "456 Oak St, Springfield")
employee3 = Employee("Charlie", 2019, 60000, "789 Pine St, Springfield")
# Displaying information of employees
employee1.display_info()
employee2.display_info()
employee3.display_info()
4. Verify and Summarize
The program defines an Employee class, initializes three employee objects with their respective attributes, and uses a method to print their details. The program should run without errors and display the information for all three employees.
Final Answer
The program successfully creates a class Employee that holds details for three employees and prints their information when executed.
Similar Questions
Write a programme in C++ which read and print employee information with department and pf information using hierarchical inheritance
Define a class Employee. Display the personal salary details of three employeesusing single inheritance
Write a program that displays the total students in each section.For example:Input ResultStudents.csvSectionA 2B 2C 3Name: StudentName, dtype: int64
Write a PHP program to connect to a database and create a database with employee table.
Develop a Java program to add student’s names and display it using vectorclass.
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.
