QuestionQuestion

Transcribed TextTranscribed Text

Programming and Computation II Lab #2 Read the instructions carefully before starting the assignment. Make sure your code follows the stated guidelines to ensure full credit for your work. Instructions: - The work in this lab must be completed alone and must be your own. - Watch the video lectures before you start this assignment - Download the starter code file from the LAB2 Assignment on Canvas. Do not change the function names or given started code on your script. - A doctest is provided as an example of code functionality. Getting the same result as the doctest does not guarantee full credit. You are responsible for debugging and testing your code with enough data, you can share ideas and testing code during your recitation class. As a reminder, Gradescope should not be used to debug and test code! - Each function must return the output (Do not use print in your final submission, otherwise your submissions will receive a -1 point deduction) - Do not include test code outside any function in the upload. Printing unwanted or ill- formatted data to output will cause the test cases to fail. Remove all your testing code before uploading your file (You can also remove the doctest). Do not include the input() function in your submission. Goal: [5 pts] Write the class VendingMachine that will represent a typical vending machine (item number, price, stock). An instance of VendingMachine has access to seven methods, isStocked, getStock, setName, setPrice, purchase, deposit and restock that will describe its interaction with the user returning strings or boolean values (read full description below for each method’s specifications). This vending machine does not give you money back unless you make a purchase, or the machine is out of stock. Tip: use the string format method to construct the strings 156 Chocolate 1.5 254 Nuts 2.0 384 Chips 2.5 879 Soda 3.0 Name and prices for those products can change at any time. A change in name or price affects all instances of this class. • Legible representation for VendingMachine objects: As in the Fraction Hands On lecture, you need to provide a legible representation for objects of this class. The representation should contain 5 lines in this order (the join method could be useful here). >>> x=VendingMachine() >>> print(x) Vending Machine stock Chocolate: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) • When creating an instance of this class, the machine contains 10 items of product 384. A dictionary could be helpful here to keep track of the stock. >>> x=VendingMachine() >>> print(x) Vending Machine stock Chocolate: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) >>> x.getStock() {156: [0, 0], 254: [0, 0], 384: [2.5, 10], 879: [0, 0]} • isStocked is a property method that returns a boolean value, True if all items have 0 stock, False if at least one item has stock greater than 0. Property methods are discussed in the video lectures! >>> x.getStock() {156: [1.5, 6], 254: [0, 0], 384: [2.5, 10], 879: [0, 0]} >>> x.isStocked True >>> x.getStock() {156: [1.5, 0], 254: [2.0, 0], 384: [2.5, 0], 879: [3.0, 0]} >>> x.isStocked False • restock increases the stock count for a given item. It returns an error message when the item is not in the dictionary >>> x.getStock() {156: [1.5, 0], 254: [2.0, 0], 384: [2.5, 0], 879: [3.0, 0]} >>> x.restock(254, 3) 'Current item stock: 3' >>> x.restock(5, 1) 'Invalid item' • If all products are out of stock, deposit and purchase return an out of stock message >>> x.getStock() {156: [1.5, 0], 254: [2.0, 0], 384: [2.5, 0], 879: [3.0, 0]} >>> x.deposit(10) 'Machine out of stock. Take your $10 back' >>> x.purchase(254) 'Machine out of stock' • purchase uses quantity=1 as a default value, which can be overwritten by providing the second argument. Before completing a purchase, machine has to check if the item is valid, if it is on stock and if the user has enough balance. >>> x.getStock() {156: [1.5, 4], 254: [2.0, 9], 384: [2.5, 7], 879: [3.0, 2]} >>> x.purchase(56) 'Invalid item' >>> x.purchase(879) 'Please deposit $3' >>> x.deposit(1) 'Balance: $1' >>> x.purchase(879) 'Please deposit $2' >>> x.deposit(10) 'Balance: $11' >>> x.purchase(156, 4) 'Item dispensed, take your $5 back' >>> x.deposit(10) 'Balance: $10' >>> x.purchase(156) 'Item out of stock' >>> x.getStock() {156: [1.5, 0], 254: [2.0, 9], 384: [2.5, 7], 879: [3.0, 2]} • getStock returns a dictionary where the key is the item and the value is the list [price, stock] • purchase will ask for the needed money to complete a purchase if enough items are in stock, otherwise, it returns the current stock of the item (for example 3 items in stock and attempting to purchase 4) >>> x.getStock() {156: [1.5, 0], 254: [2.0, 9], 384: [2.5, 7], 879: [3.0, 2]} >>> x.purchase(254,10) 'Current 254 stock: 9, try again' • setName and setPrice manipulate name and price for each product. When an instance calls this method, it affects all instances of the Vending Machine class >>> x=VendingMachine() >>> y=VendingMachine() >>> print(x) Vending Machine stock Chocolate: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) >>> print(y) Vending Machine stock Chocolate: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) >>> x.setName(156,'Pretzels') >>> print(x) Vending Machine stock Pretzels: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) >>> print(y) Vending Machine stock Pretzels: 0 unit(s) Nuts: 0 unit(s) Chips: 10 unit(s) Soda: 0 unit(s) • Once the user completes one purchase, machine returns remaining balance • For more examples of functionality, check the doctest in the starter code • Quotes mean method returned a string, no need to append them in your code • Return output with the sentences provided. Solution is not case sensitive, which means Balance: $10 is the same as balance: $10, but is not the same as Balance= $10 [5 pts] Write the class Complex that supports the basic complex number operations. Such operations are addition (+), subtraction (-), multiplication (*) and division (/) of complex numbers, and addition (+), subtraction (-) and multiplication (*) of a complex by a scalar (float or int). All methods must return (not print) the result. Class also supports the rich comparison for equality (= =) - Check the doctest for object behavior examples. - You must use the special methods for those 5 operators in order to override their behavior. - You will need other special methods to achieve a legible object representation (what you see when printing and returning the object). - Addition, subtraction and multiplication by scalar and by another complex number use the same operator, so you must check the type of the object in order to decide which operation you have to perform - Rich comparison returns a Boolean value - The rest of the methods must return a new Complex object, this means the original objects should not change after the operation is performed. - Test your code, this is how you ensure you get the most credit out of your work!! - When performing an invalid operation, return None. Examples of invalid operations are Complex + str, Complex * tuple, etc. - You are not allowed to modify the constructor or any given code. - Note that if you perform number (+,- or *) Complex, the left operand does not support the corresponding operation since Complex is a custom class. To support such operations, you are going to need 3 other special methods that call from the right operand and can reuse code from the Complex (+, - or *) number operations. Hint: Section 3.3.1. Basic customization (__eq__) and Full Section 3.3.8. Emulating numeric types in https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types Remember, you are not allowed to modify the given constructor. You will not get credit if you do. Deliverables: • Submit your code with the file name LAB2.py to the Lab2 Gradescope assignment before the due date General note: Start by watching the lectures. Do not overthink this assignment, you don’t have to come up with a complex algorithm to solve it. Just follow the formulas and directions, the purpose of this assignment is to familiarize you with object-oriented programming syntax. Ask us questions if you need help!

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#Lab #2
#Due Date:
########################################
#                                    
# Collaboration Statement:            
#
########################################

class VendingMachine:
    '''
       >>> x=VendingMachine()
       >>> print(x)
       Vending Machine stock
       Chocolate: 0 unit(s)
       Nuts: 0 unit(s)
       Chips: 10 unit(s)
       Soda: 0 unit(s)
       >>> x.getStock()    # {156: [1.5, 0], 254: [2.0, 0], 384: [2.5, 10], 879: [3.0, 0]} is also ok
       {156: [0, 0], 254: [0, 0], 384: [2.5, 10], 879: [0, 0]}
       >>> x.restock(215, 9)
       'Invalid item'
       >>> x.isStocked
       True
       >>> x.restock(156, 9)
       'Current item stock: 9'
       >>> x.getStock()
       {156: [1.5, 9], 254: [0, 0], 384: [2.5, 10], 879: [0, 0]}
       >>> x.purchase(156)
       'Please deposit $1.5'
       >>> x.purchase(156,2)
       'Please deposit $3.0'
       >>> x.purchase(156,23)...

By purchasing this solution you'll be able to access the following files:
Solution.py.

$90.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats