Question #171647

Write a Python class named "Pencil" that has three attributes: brand (string), color (string), thickness (int).

The class must include a constructor method that initializes each variable to an appropriate value, and the class should include methods for setting the value of each type and retrieving the value of each type.


Expert's answer

class Pencil():
  def __init__(self, brand, color, thickness):
    self.brand = None
    self.color = None
    self.thickness = None
    if type(brand) is not str:
      print("Error: wrong brand type")
    else:
      self.brand = brand
    if type(color) is not str:
      print("Error: wrong color type")
    else:
      self.color = color
    if type(thickness) is not int:
      print("Error: wrong thickness type")
    else:
      self.thickness = thickness
  def GetBrand(self):
    return self.brand
  def SetBrand(self, brand):
    if type(brand) is not str:
      return "Error: wrong type"
    self.brand = brand
    return True
  def GetColor(self):
    return self.color
  def SetColor(self, color):  
    if type(color) is not str:
      return "Error: wrong type"
    self.color = color
    return True
  def GetThickness(self):
    return self.thickness
  def SetThickness(self, thickness):
    if type(thickness) is not int:
      return "Error: wrong type"
    self.thickness = thickness
    return True
a = Pencil('a','12',12)
print(a.GetBrand())
print(a.GetColor())
print(a.GetThickness())
print(a.SetBrand('as'))
print(a.SetColor('a'))
print(a.SetThickness(12))
print(a.GetBrand())
print(a.GetColor())
print(a.GetThickness())

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS