Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IT 2 0 9 - LAB ASSIGNMENT 6 ( LA 6 ) ENHANCED Cart Class - ' in ' Operator, 'list' Subclass This Lab has
IT LAB ASSIGNMENT LA
ENHANCED Cart Class in Operator, 'list' Subclass
This Lab has two parts. In Part you are to modify the Cart class created in a previous lab so that it
works with the builtin Python in operator. The result will be that code using the Cart class can check
to see whether an item is already in the cart using in Part requires you to modify the Cart class to
be a subclass of 'list'. You may use your previously created Cart class or the one lm providing with
this assignment.
Part
Recall that adding a method to a class causes Python to treat that class as a "duck
type" subclass of the builtin Container abstract base class. As a result the Python interpreter
recognizes when the in operator is used with the class and substitutes the logic of the provided
method if one is provided. If not provided, Python raises an exception. Test code is
provided with this assignment that will create a Cart object, several Item objects, and then run tests
using in
To complete this part, create a Cart method to check whether an item is contained in the Cart object
when code using the Cart class uses in When you inspect the test code you'll notice that in can be
invoked with either an Item object iei type "Item" or just the name of the item ie 'eggs', type
string Your method must detect which type is submitted and perform the test accordingly.
Part
Modify the Cart class either your own or the one I provide to be a subclass of list. In doing this
you'll be extending the builtin list class. Everything true about a Python list will also be true about
Cart, except you've added additional logic specific to Cart objects eg customer name, cart
sequence number
The major difference is that instead of having an internal list, self.contents, to hold the items in the
cart, the Cart object itself is a list. You refer to the items in Cart method code by just using 'self. For
example, when adding item 'itemObj' you issue the statement 'self.appenditemObj Likewise,
self refers to the first item in the list, self the second, etc. To complete this part modify the Cart
class to do just that. If you do this correctly the test code provided with the assignment will work with
both Part and without modification.
Provided Code and Test Script
A py template file is provided with this assignment that contains code for the Cart and item classes,
as well as a test script. Modify this Cart class, or use your own, for both parts using the template file
as a starting point for both. Copy the template into your own file and complete Part Then copy it
again into another file and complete Part The test script can be used for both parts and should run
without modification. As was mentioned before, please be aware that the script is not an exhaustive
test, so please perform some testing beyond what's in the script. It's an unfortunate reality of
software development that testing can only expose errors and not prove the lack of errors. Even the
most exhaustive testing cannot guarantee defectfree software.
This lab should require creating around lines of new code for Part and modifying around lines
for Part
What and where to submit:
Submit by uploading two files, one for each part, with the Python code to Blackboard
Submit the programs using the file template provided with the Lab assignment specification
Input a short set of comments as the first lines that identify the program, its purpose, and its
author. Also include other comments where you think necessary.
Include a screen shot of the program's output using, at minimum, the display output resulting from
running the test script
How the assignment will be assessed:
The Python code will be visually inspected and executed via command line python
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started