Starting from:

$27

Computer Science 222, Laboratory 12 – Lists; Objects as Parameters and Return Values


Learning
objectives:


Implement a built-in Python list function.




Develop a class with object parameters and return types.

Use a driver program to test the class.





Getting
started: Download testMoney.py.



1.
Writing our own reverse.

Write your own
version of reverse. The real reverse is a method of the List class in
Python, and we are not allowed to add new methods to a built-in class, so our reverse will be a function. Put it in the file reverse.py.



Write reverse(<list) so that the values in <list are reversed in place.
Note that reverse does not return anything.
That is, there is not a second list.
The one and only list has its values reversed in place. reverse() must not use a break or a continue, and must have no return. Write a
main() that makes a couple of calls
to reverse().



Demonstrate for
the instructor.



2. A
Money Class

Define a class named Money
whose objects represent amounts in U. S. money, and put it in the file money.py.
The class must have two instance variables of type int for the dollars
and cents parts of the amount of money. Do
not use any float variables or values in this program.



Write a constructor that sets
dollars and cents to zero. Write getter methods for the dollars and the cents.
At this point all that testMoney does is create a Money object and print its values.



Demonstrate for the instructor.



3.
Setters

Write setter methods for the dollars
and the cents. The setters must ensure that only non-negative values are
stored. If a user enters a negative amount, keep the current value. Make sure
to always maintain a “correct” number of dollars and cents at all times. That
is, you should never store something like 1 dollar and 115 cents; the correct
representation is 2 dollars and 15 cents.



Uncomment so that testMoney
tests the setters. Demonstrate for the instructor.







3. __str__()
Method

Write a __str__ method that returns a string containing a dollar sign, the dollar
amount, a decimal point, and the cents amount as two digits (regardless of its value). Don't return or print
anything else!



Uncomment so that testMoney
tests the __str__ method. Demonstrate for the
instructor.



4. increment()
Method

Write a method, increment(m), that adds to the amount in self the amount in the parameter m, which is a Money object.
So if m1 holds 2 dollars 90 cents and m2 holds 1 dollar 20
cents, the call m1.increment(m2) changes the amount in m1 to 4
dollars 10 cents. No value is returned
by this method.



Uncomment so that testMoney
tests the increment() method. Note that increment() uses only two objects, the caller (which becomes self) and the argument. Demonstrate for the instructor.



5. add() Method

Write a method, add(m),
that adds the amount in self and the
amount in the parameter and returns
a new Money object. So if m1 holds 2 dollars 90 cents and m2 holds 1 dollar 20
cents, the call m3 =
m1.add(m2) returns
a third Money object m3 with a
value of 4 dollars 10 cents.



Uncomment so that testMoney
tests the add() method. Note that add() uses three objects, the caller (which becomes self), the argument, and a new Money object. Be sure to test fully.
Demonstrate for the instructor.



Upload
the files (don't forget to submit):



reverse.py ______ Money.py ______





Thanks for all your work in lab this semester!

More products