Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will continue working on Python. Using a Google Colab notebook write the python statement to define a Pizza class. The test harness is provided

  1. We will continue working on Python. Using a Google Colab notebook write the python statement to define a Pizza class. The test harness is provided to you by the instructor as well as the resulting output given. 
  2. Topics covered:
  3. List
  4. Dictionary
  5. Class
    1. Constructor
    2. Property
    3. Methods
  6. Exception
  7. How to do this assignment.
  8. From the test harness and the given output, try to deduce the definition of the Pizza class. Code this class in a colab notebook and copy the test harness to a cell below. Execute the notebook and ensure that the output matches EXACTLY with the output on the following page.
  9. You must use Python f-strings for your output.
  10. Instruction.
  11. Do not modify the test harness.
  12. Documentation.
  13. Please enter your name and the current date somewhere at the top of your code.
  14. How to submit this assignment.
  15. Make the notebook shareable and submit the link to the course assessment folder.
  16. See the folder documentation for due date.
  17. See the course documentation for policy on deadlines.

  18. Specifications for the Pizza class.
  19. Please implement the Pizza class using the following instructions.
  20. Class attributes
  21. Remember that class members do not have a "self" prefix and are accessed with the class name and the dot operator.
  22. Valid sizes for pizza are contained in a collection at the class level.
    You get to decide on the type of collection to store the sizes below.
    Valid sizes are small, medium, large and x-large.
  23. Another class level collection having the prices for each valid size.
    You get to decide on the type of collection to store the prices below.
    Prices are small: $6.49, medium: $8.49, large: $10.49, x-large: $13.49.
  24. Constructor
  25. Instance methods have an implicit first argument "self".
  26. A constructor that takes a default size of medium and topping of a list of cheese that does the following:
    1. Set the first argument size to the instance attribute size. Size must be verified. This can be done by setting the size property.
    2. Creates an instance attribute a list with the second argument. If the second argument is missing, a single cheese topping is inserted in the list.
  27. Instance methods
  28. Instance methods have an implicit first argument "self".
  29. A method that takes an argument of type list of strings. It adds topping to the list of pizza toppings.
  30. Implement the __str__() method to return a formatted string. Examine the output from the test harness for clues on how to implement this method.
  31. Instance Properties
  32. Instance properties have an implicit first argument "self".
  33. A property that returns the price of the pizza.
    1. Price is based on the size as well as the number of toppings.
      See spec#2 for cost based on size. Each topping cost an additional $0.50 each.
  34. A property that returns the size of the pizza.
  35. A property that sets the size of the pizza.
    1. Size must be verified. A ValueError exception is raised if the size is invalid.
  36. Instance Attributes
  37. All of the following Instance attributes are initialized in the constructor from the values of the argument. Notice the __ prefix to make is private.
  38. __size is a str that stores the size of this object. This is initialized in the constructor. It is mutated by the property in #8. It is returned in #7. It is used in #6 to calculate the cost of the pizza.
  39. __toppings a list of string that represents the toppings for this object. This is initialized in the constructor. It is mutated in the add() method and is used in #6 to calculate the cost of the pizza.
  40.  

  41.  
  42.  
  43. The test harness.
  44. You may not change the test harness.
  45.  
  46.  
  47. print(f'Creating a default pizza')
  48. p = Pizza()
  49. print(p)
  50.  
  51. toppings = 'cheese olive'.split()
  52. print(f'Adding topping: {toppings}')
  53. p.add(toppings=toppings)
  54. print(p)
  55.  
  56. print(f'Creating a new pizza')
  57. p = Pizza('large', 'cheese pepper'.split())
  58. print(p)
  59.  
  60. toppings = ['pineapple', 'mushroom']
  61. print(f'Adding topping: {toppings}')
  62. p.add(toppings)
  63. print(p)
  64.  
  65. size = 'x-large'
  66. p.size = size
  67. print(f'Changing order size to {size}')
  68. print(p)
  69.  
  70. size = 'gigantic'
  71. print(f'Changing order size to {size}')
  72. try:
  73.  p.size = size
  74. except ValueError as err:
  75.   print(err) 
  76.  
  77. The resulting output
  78.  
  79.  
  80. Creating a default pizza
  81. medium pizza with ['cheese'] for $8.99
  82.  
  83. Adding topping: ['cheese', 'olive']
  84. medium pizza with ['cheese', 'cheese', 'olive'] for $9.99
  85.  
  86. Creating a new pizza
  87. large pizza with ['cheese', 'pepper'] for $11.49
  88.  
  89. Adding topping: ['pineapple']
  90. large pizza with ['cheese', 'pepper', 'pineapple', 'mushroom'] for $12.49
  91.  
  92. Changing order size to x-large
  93. x-large pizza with ['cheese', 'pepper', 'pineapple', 'mushroom'] for $15.99
  94.  
  95. Changing the order size to gigantic
  96. ERROR: Gigantic is not a valid size for a pizza

 

Step by Step Solution

3.52 Rating (162 Votes )

There are 3 Steps involved in it

Step: 1

python class Pizza Class attributes validsizes small medium large xlarge sizeprices small 649 medium ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial Reporting Financial Statement Analysis And Valuation A Strategic Perspective

Authors: James M. Wahlen, Stephen P. Baginski, Mark Bradshaw

9th Edition

1337614689, 1337614688, 9781337668262, 978-1337614689

More Books

Students also viewed these Programming questions