Question
upstudy study bank question image url

(a) Write a program to define a class Item with instance attributes name and price. Use assert to ensure that the user inputs a string for name and an integer for price. Also instantiate the class by using a suitable variable name. [4 marks] (b) Write a python class Employee. Create an _-init_- method to add attributes name and salary to the class. Create another method displayEmployee that prints the details of an instance. For instance: Name :Zara,Salary: 2000

Ask by Huff Pritchard. in Kenya
Feb 03,2025

Real Tutor Solution

Tutor-Verified Answer

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

error msg
Explain
Simplify this solution

Mind Expander

```python class Item: def __init__(self, name, price): assert isinstance(name, str), "Name must be a string." assert isinstance(price, (int, float)), "Price must be a number." self.name = name self.price = price # Instantiate the Item class item1 = Item("Laptop", 1200) 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 employee1 = Employee("Zara", 2000) employee1.displayEmployee() ```

Related Questions

Latest Computer Technology Questions

Try Premium now!
Try Premium and ask Thoth AI unlimited math questions now!
Maybe later Go Premium
Study can be a real struggle
Why not UpStudy it?
Select your plan below
Premium

You can enjoy

Start now
  • Step-by-step explanations
  • 24/7 expert live tutors
  • Unlimited number of questions
  • No interruptions
  • Full access to Answer and Solution
  • Full Access to PDF Chat, UpStudy Chat, Browsing Chat
Basic

Totally free but limited

  • Limited Solution
Welcome to UpStudy!
Please sign in to continue the Thoth AI Chat journey
Continue with Email
Or continue with
By clicking “Sign in”, you agree to our Terms of Use & Privacy Policy