Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IT 2 0 9 - LAB ASSIGNMENT 6 ( LA 6 ) ENHANCED Cart Class - ' in ' Operator, 'list' Subclass This Lab has

IT209- LAB ASSIGNMENT 6(LA6)
ENHANCED Cart Class -'in' Operator, 'list' Subclass
This Lab has two parts. In Part 1 you are to modify the Cart class created in a previous lab so that it
works with the built-in 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 2 requires you to modify the Cart class to
be a subclass of 'list'. You may use your previously created Cart class or the one l'm providing with
this assignment.
Part 1
Recall that adding a '' method to a class causes Python to treat that class as a "duck
type" subclass of the built-in 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 (i.e.'i1', type "Item") or just the name of the item (i.e. 'eggs', type
string). Your method must detect which type is submitted and perform the test accordingly.
Part 2
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 built-in list class. Everything true about a Python list will also be true about
Cart, except you've added additional logic specific to Cart objects (e.g., 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.append(itemObj)'. Likewise,
self[0] refers to the first item in the list, self[1] 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 1 and 2 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 1. Then copy it
again into another file and complete Part 2. 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 defect-free software.
This lab should require creating around 10 lines of new code for Part 1 and modifying around 7 lines
for Part 2.
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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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_2

Step: 3

blur-text-image_3

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

Modern Datalog Engines In Databases

Authors: Bas Ketsman ,Paraschos Koutris

1st Edition

1638280428, 978-1638280422

More Books

Students also viewed these Databases questions

Question

=+. Does the source have any moral leverage with this audience?

Answered: 1 week ago