首页 新闻 聚焦 科技 财经 创业 综合 图片 视频

IT界

旗下栏目: 行业 生态 IT界 创新

代写CMPSC-132编程、代做Programming课程编程、代做Python编程

来源:互联网 发布时间:2020-09-17
代写CMPSC-132编程、代做Programming课程编程、代做Python编程
CMPSC-132: Programming and Computation II
Lab 2 (10 points) Due date: September 18
th, 2020, 11:59 PM EST
Goal: The goal of this assignment is to familiarize with object-oriented programming and provide
functionality to custom classes
General 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 answer
any 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 full
range 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 be
graded.
Assignment-specific Instructions:
• Download the starter code file from Canvas. Do not change the function names or given
starter code in your script.
• You can assume the objects will be created providing the adequate input. There is no
need for input validation in the constructor.
• If you are unable to complete a method, leave the pass statement
Submission 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 a
purchase (or if the machine is out of stock).
This vending machine will sell four different products:
Product ID Price
代写CMPSC-132作业、代做Programming课程作业、
156 1.5
254 2.0
384 2.5
879 3.0
When creating An instance of this class, the machine starts out with 3 items of each product. A
dictionary could be useful here to keep track of the stock. A VendingMachine object returns strings
describing its interactions.
Tip: Python’s 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]'
Methods
Type Name Description
str 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 machine
bool isStocked(self) A property method that checks for the stock status.
dict getStock(self) A property method that gets the current stock status
of the machine.
None setPrice(self, item, new_price) Changes the Price of an item in the vending machine
Section 1: The VendingMachine class
Preconditions:
purchase(self, item, qty=1)
Attempts to buy something from the vending machine. Before completing the purchase, check to
make 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 purchase
int qty The desired quantity of said item. Defaults to 1
Output
str
“Item dispensed” if there is no money to give back to the user
“Item dispensed, take your $change back” if there is change to give back
“Invalid item” if the item id is invalid
“Machine out of stock” is returned if the machine is out of stock for all items
“Item 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 stock
“Please deposit $remaining” if there is not enough balance
deposit(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.
Output
str
“Balance: $balance” When machine is stocked
“Machine 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.
Output
str
“Current item stock: stock” for existing id
“Invalid item” is returned if the item id is invalid.
Section 1: The VendingMachine class
isStocked(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.
Output
bool 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].
Output
dict Current stock status represented as a dictionary.
setPrice(self, item, new_price)
Changes the price of an item for an instance of VendingMachine
Input (excluding self)
int item Item ID of item to change the price of.
int or float new_price The new price for the item.
Output
None 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.isStocked
True
More examples of class behavior provided in the starter code
Section 2: The Line class (5 pts)
The Line Class represents a 2D line that stores two Point2D objects and provides the distance
between the two points and the slope of the line using the property methods getDistance and
getSlope. The constructor of the Point2D class has been provided in the starter code.
Methods
Type Name Description
float getDistance Returns the distance between two Point2D objects
float getSlope Returns the slope of the line that passes through the two points
Special methods
Type Name Description
str __str__
__repr__
Gets a legible representation of the Line in the form y = mx + b
bool __eq__ Determines if two Line objects are equal
Line __mul__ Returns a new Line object where each coordinate is multiplied by a positive
integer
Preconditions:
getDistance(self)
A property Method (behaves like an attribute) that gets the distance between the two Point2D
objects that created the Line. The formula to calculate the distance between two points in a twodimensional
space is:
𝑑 = √(𝑥2 − 𝑥1
)
2 + (𝑦2 − 𝑦1
)
2
Returns a float rounded to 3 decimals. To round you can use the round method as round(value,
#ofDigits)
Output
float Returns the distance between the two point objects that pass through the Line
getSlope(self)
A property method (behaves like an attribute) that gets the slope (gradient) of the Line object. The
formula to calculate The slope using two points in a two-dimensional space is:
𝑚 =
𝑦2 − 𝑦1
𝑥2 − 𝑥1
Returns a float rounded to 3 decimals. To round you can use the round method as round(value,
#ofDigits)
Output
float Returns the slope of the line,
float inf float for undefined slope (denominator is zero)
Section 2: The Line class
Examples:
>>> p1 = Point2D(-7, -9)
>>> p2 = Point2D(1, 5.6)
>>> line1 = Line(p1, p2)
>>> line1.getDistance
16.648
>>> line1.getSlope
1.825
 
__str__ and __repr__
A special method that provides a legible representation for instances of the Line class. Objects will
be represented using the "slope-intercept" equation of the line:
y = mx + b
To find b, Substitute m with the slope and y and x for any of the points and solve for b. b must be
rounded to 3 decimals. To round you can use the round method as round(value, #ofDigits)
Output
str Slope-intercept equation of the line, representation must be adjusted if m or b are 0, or
if b is positive/negative
str ‘Undefined’ for undefined slope
Examples:
>>> p1 = Point2D(-7, -9)
>>> p2 = Point2D(1, 5.6)
>>> line1 = Line(p1, p2)
>>> line1
y = 1.825x + 3.775
>>> line5=Line(Point2D(6,48),Point2D(9,21))
>>> line5
y = -9.0x + 102.0
>>> line6=Line(Point2D(2,6), Point2D(2,3))
>>> line6.getDistance
3.0
>>> line6.getSlope
inf
>>> line6
Undefined
>>> line7=Line(Point2D(6,5), Point2D(9,5))
>>> line7
y = 5.0
Section 2: The Line class
__eq__
A special method that determines if two Line objects are equal. For instances of this class, we will
define equality as two lines having the same points. To simplify this method, you could try defining
equality in the Point2D class
Output
bool 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 of
every Point2D object is multiplied by the integer. The only operations allowed are integer*Line
and Line*integer, any other non-integer values return None. You will need to override both the
“normal” version And the “right side” version to support such operations.
Output
Line A new Line object where point1 and point2 are multiplied by an integer
Examples:
>>> p1 = Point2D(-7, -9)
>>> p2 = Point2D(1, 5.6)
>>> line1 = Line(p1, p2)
>>> line2 =Line1*4
>>> isinstance(line2, Line)
True
>>> line2
y = 1.825x + 15.1
>>> line3 = 4*line1
>>> line3
y = 1.825x + 15.1
>>> line1==line2
False
>>> line3==line2
True
>>> line5==9
False
如有需要,请加QQ:99515681 或邮箱:99515681@qq.com 微信:codehelp
  免责声明:珠穆朗玛网对于有关本网站的任何内容、信息或广告,不声明或保证其正确性或可靠性,用户自行承担使用网站信息发布内容的真假风险。
责任编辑:珠穆朗玛网

最火资讯