辅导CMPSC-132编程、 写作Programming课程编程、 写作Python编程

” 辅导CMPSC-132编程、 写作Programming课程编程、 写作Python编程CMPSC-132: Programming and Computation IILab 2 (10 points) Due date: September 18th, 2020, 11:59 PM ESTGoal: The goal of this assignment is to familiarize with object-oriented programming and providefunctionality to custom classesGeneral instructions: The work in this assignment must be your own original work and be completed alone. The instructor and course assistants are available on Piazza and with office hours to answerany questions you may have. You may also share testing code on Piazza. A doctest is provided to ensure basic functionality and may not be representative of the fullrange of test cases we will be checking. Further testing is your responsibility. Debugging code is also your responsibility. You may submit more than once before the deadline; only the final submission will begraded.Assignment-specific Instructions: Download the starter code file from Canvas. Do not change the function names or givenstarter code in your script. You can assume the objects will be created providing the adequate input. There is noneed for input validation in the constructor. If you are unable to complete a method, leave the pass statementSubmission format: Submit your LAB2.py file to the Lab 2 Gradescope assignment before the due date.Section 1: The VendingMachine class (5 pts)This class will represent a vending machine that only gives the user back money after making apurchase (or if the machine is out of stock).This vending machine will sell four different products:Product ID Price 辅导CMPSC-132作业、 写作Programming课程作业、156 1.5254 2.0384 2.5879 3.0When creating An instance of this class, the machine starts out with 3 items of each product. Adictionary could be useful here to keep track of the stock. A VendingMachine object returns stringsdescribing its interactions.Tip: Pythons string formatting syntax could be useful item, price, stock = Potatoes, 3.5, [20] {} cost {} and we have {}.format(item, price, stock)Potatoes cost 3.5 and we have [20]MethodsType Name Descriptionstr purchase(self, item, qty=1) Attempts to buy something from the machine.str deposit(self, amount) Deposits money into the vending machine.str restock(self, item, stock) Adds stock to the vending machinebool isStocked(self) A property method that checks for the stock status.dict getStock(self) A property method that gets the current stock statusof the machine.None setPrice(self, item, new_price) Changes the Price of an item in the vending machineSection 1: The VendingMachine classPreconditions:purchase(self, item, qty=1)Attempts to buy something from the vending machine. Before completing the purchase, check tomake sure the item is valid, there is enough stock of said item, and there is enough balance.Input (excluding self)int item An integer that might represent item ID of item to purchaseint qty The desired quantity of said item. Defaults to 1OutputstrItem dispensed if there is no money to give back to the userItem dispensed, take your $change back if there is change to give backInvalid item if the item id is invalidMachine out of stock is returned if the machine is out of stock for all itemsItem out of stock is returned if there is no stock left of requested item.Current item_id stock: stock, try again if there is not enough stockPlease deposit $remaining if there is not enough balancedeposit(self, amount)Deposits money into the vending machine, adding it to the current balance.Input (excluding self)int or float amount Amount of money to add to the balance.OutputstrBalance: $balance When machine is stockedMachine out of stock. Take your $amount back if the machine is out of stock.restock(self, item, stock)Adds stock to the vending machine.Input (excluding self)int item Item ID of item to restock.int stock The amount of stock to add to the vending machine.OutputstrCurrent item stock: stock for existing idInvalid item is returned if the item id is invalid.Section 1: The VendingMachine classisStocked(self)A property method (behaves like an attribute) that returns True if any item has any nonzero stock,and False if all items have no stock.Outputbool Status of the Vending machine.getStock(self)A property method (behaves like an attribute) that gets the current stock status of the machine.Returns a dictionary where the key is the item and the value is the list [price, stock].Outputdict Current stock status represented as a dictionary.setPrice(self, item, new_price)Changes the price of an item for an instance of VendingMachineInput (excluding self)int item Item ID of item to change the price of.int or float new_price The new price for the item.OutputNone Nothing is returned for normal operation.str Invalid item is returned if the item id is invalid.Examples: x.getStock{156: [1.5, 0], 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(10)Balance: $10 x.purchase(156, 4)Item dispensed, take your $4 back x.isStockedTrueMore examples of class behavior provided in the starter codeSection 2: The Line class (5 pts)The Line Class represents a 2D line that stores two Point2D objects and provides the distancebetween the two points and the slope of the line using the property methods getDistance andgetSlope. The constructor of the Point2D class has been provided in the starter code.MethodsType Name Descriptionfloat getDistance Returns the distance between two Point2D objectsfloat getSlope Returns the slope of the line that passes through the two pointsSpecial methodsType Name Descriptionstr __str____repr__Gets a legible representation of the Line in the form y = mx + bbool __eq__ Determines if two Line objects are equalLine __mul__ Returns a new Line object where each coordinate is multiplied by a positiveintegerPreconditions:getDistance(self)A property Method (behaves like an attribute) that gets the distance between the two Point2Dobjects that created the Line. The formula to calculate the distance between two points in a twodimensionalspace is: = (2 1)2 + (2 1)2Returns a float rounded to 3 decimals. To round you can use the round method as round(value,#ofDigits)Outputfloat Returns the distance between the two point objects that pass through the LinegetSlope(self)A property method (behaves like an attribute) that gets the slope (gradient) of the Line object. Theformula to calculate The slope using two points in a two-dimensional space is: =2 12 1Returns a float rounded to 3 decimals. To round you can use the round method as round(value,#ofDigits)Outputfloat Returns the slope of the line,float inf float for undefined slope (denominator is zero)Section 2: The Line classExamples: p1 = Point2D(-7, -9) p2 = Point2D(1, 5.6) line1 = Line(p1, p2) line1.getDistance16.648 line1.getSlope1.825__str__ and __repr__A special method that provides a legible representation for instances of the Line class. Objects willbe represented using the slope-intercept equation of the line:y = mx + bTo find b, Substitute m with the slope and y and x for any of the points and solve for b. b must berounded to 3 decimals. To round you can use the round method as round(value, #ofDigits)Outputstr Slope-intercept equation of the line, representation must be adjusted if m or b are 0, orif b is positive/negativestr Undefined for undefined slopeExamples: p1 = Point2D(-7, -9) p2 = Point2D(1, 5.6) line1 = Line(p1, p2) line1y = 1.825x + 3.775 line5=Line(Point2D(6,48),Point2D(9,21)) line5y = -9.0x + 102.0 line6=Line(Point2D(2,6), Point2D(2,3)) line6.getDistance3.0 line6.getSlopeinf line6Undefined line7=Line(Point2D(6,5), Point2D(9,5)) line7y = 5.0Section 2: The Line class__eq__A special method that determines if two Line objects are equal. For instances of this class, we willdefine equality as two lines having the same points. To simplify this method, you could try definingequality in the Point2D classOutputbool True is lines have the same points, False otherwise__mul__A special method to support the * operator. Returns a new Line object where the x,y attributes ofevery Point2D object is multiplied by the integer. The only operations allowed are integer*Lineand Line*integer, any other non-integer values return None. You will need to override both thenormal version And the right side version to support such operations.OutputLine A new Line object where point1 and point2 are multiplied by an integerExamples: p1 = Point2D(-7, -9) p2 = Point2D(1, 5.6) line1 = Line(p1, p2) line2 =Line1*4 isinstance(line2, Line)True line2y = 1.825x + 15.1 line3 = 4*line1 line3y = 1.825x + 15.1 line1==line2False line3==line2True line5==9False如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导