Answer to Question #171647 in Python for Andu

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.


1
Expert's answer
2021-03-14T18:45:27-0400
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS