Computer Class Hierarchy

Implement a class Computer with basic attributes like brand, model, and price. Then define child classes Desktop, Laptop, Tablet, each with unique attributes and methods.

Example 1:

Input: Desktop('Dell', 'OptiPlex', 800, 'Windows', True) 

Output: "This is a Dell OptiPlex desktop computer. It runs on Windows and costs $800. It supports multiple monitors."

Example 2:

Input: Laptop('Apple', 'MacBook Pro', 2000, 'macOS', 15) 

Output: "This is an Apple MacBook Pro laptop. It runs on macOS, costs $2000, and has a 15-inch screen."

Implement a unique method in each of the child classes that returns the respective description of the computer type.

class Computer:
    def __init__(self, brand, model, price):
        self.brand = brand
        self.model = model
        self.price = price

class Desktop(Computer):
    def __init__(self, brand, model, price, os, multi_monitor):
        super().__init__(brand, model, price)
        self.os = os
        self.multi_monitor = multi_monitor

    def description(self):
        return f"This is a {self.brand} {self.model} desktop computer. It runs on {self.os} and costs ${self.price}. It{' supports' if self.multi_monitor else ' does not support'} multiple monitors."

class Laptop(Computer):
    def __init__(self, brand, model, price, os, screen_size):
        super().__init__(brand, model, price)
        self.os = os
        self.screen_size = screen_size

    def description(self):
        return f"This is a {self.brand} {self.model} laptop. It runs on {self.os}, costs ${self.price}, and has a {self.screen_size}-inch screen."

# Test the classes
computer1 = Desktop('Dell', 'OptiPlex', 800, 'Windows', True)
print(computer1.description())

computer2 = Laptop('Apple', 'MacBook Pro', 2000, 'macOS', 15)
print(computer2.description())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!