Answer
```python
class Item:
def __init__(self, name, price):
assert isinstance(name, str), "Name must be a string"
assert isinstance(price, int), "Price must be an integer"
self.name = name
self.price = price
item1 = Item("Notebook", 50)
print(f"Item created: Name = {item1.name}, Price = {item1.price}")
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def displayEmployee(self):
print(f"Name: {self.name}, Salary: {self.salary}")
emp1 = Employee("Zara", 2000)
emp1.displayEmployee()
```
**Output:**
```
Item created: Name = Notebook, Price = 50
Name: Zara, Salary: 2000
```
Solution
### (a) Defining the `Item` Class with Assertions
Below is a Python program that defines a class `Item` with instance attributes `name` and `price`. The `__init__` method uses `assert` statements to ensure that `name` is a string and `price` is an integer. The class is then instantiated using the variable name `item1`.
```python
class Item:
def __init__(self, name, price):
# Ensure that name is a string
assert isinstance(name, str), "Name must be a string"
# Ensure that price is an integer
assert isinstance(price, int), "Price must be an integer"
self.name = name
self.price = price
# Example instantiation
try:
item1 = Item("Notebook", 50)
print(f"Item created: Name = {item1.name}, Price = {item1.price}")
except AssertionError as e:
print(e)
```
**Explanation:**
1. **Class Definition:**
- The `Item` class has an `__init__` method that initializes the `name` and `price` attributes.
2. **Assertions:**
- `assert isinstance(name, str)`: Checks if `name` is a string.
- `assert isinstance(price, int)`: Checks if `price` is an integer.
- If the assertions fail, an `AssertionError` with the specified message is raised.
3. **Instantiation:**
- The class is instantiated with `item1 = Item("Notebook", 50)`.
- A `try-except` block is used to catch any assertion errors and display appropriate messages.
**Output:**
```
Item created: Name = Notebook, Price = 50
```
---
### (b) Defining the `Employee` Class with `displayEmployee` Method
Below is a Python class `Employee` with an `__init__` method to add `name` and `salary` attributes, and a `displayEmployee` method to print the employee's details.
```python
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def displayEmployee(self):
print(f"Name: {self.name}, Salary: {self.salary}")
# Example usage
emp1 = Employee("Zara", 2000)
emp1.displayEmployee()
```
**Explanation:**
1. **Class Definition:**
- The `Employee` class has an `__init__` method that initializes the `name` and `salary` attributes.
2. **`displayEmployee` Method:**
- This method prints the employee's `name` and `salary` in the specified format.
3. **Instantiation and Method Call:**
- An instance `emp1` is created with the name `"Zara"` and salary `2000`.
- `emp1.displayEmployee()` is called to display Zara's details.
**Output:**
```
Name: Zara, Salary: 2000
```
---
These implementations ensure that the classes are correctly defined with the necessary attributes and methods, and include proper type checking and output formatting as specified.
Reviewed and approved by the UpStudy tutoring team
Explain
Simplify this solution