3300 Problems, Section 6: Object-Oriented Programming
1. Consider a class called Circle, whose objects have the following attribute variables:
• . rad, representing the radius of the circle;
• . h, representing the x-coordinate of the center ;
• . k, representing the y-coordinate of the center ;
and the following methods:
• a constructor which takes three outside arguments, which set r, h, and k;
• .isInside() which takes two outside arguments named xC and yC as, and returns True if the point (xC, yC) is
within distance r of the center ( h, k), and false otherwise.
(Recall the distance formula d =
p
(x2 − x1)
2 + (y2 − y1)
2.)
a. Suppose that the class Circle works. Complete the following client code so that the phrase Inside! is printed to
the console if (myX, myY) is within the circle you declared.
my_circ = Circle(3,4,2)
myX = float(input("Enter an x-coordinate: "))
myY = float(input("Enter an y-coordinate: "))
b. Now, write a definition for the class Circle that meets the given specifications and that makes the above code work.
c. Add another method so that the following code causes "(x - 3)^2 + (y - 2)^2 = 25" to print to the console:
my_circ = Circle(3,2,5)
print(my_circ)
2. Consider a class called Line whose objects have the following attribute variables:
• . m, representing the slope of a line y = mx + b;
• . b, representing the y-intercept of a line y = mx + b;
and the following methods:
• a constructor which takes two outside arguments, which set m and b respectively;
• .yInt(), a method which has no outside arguments and returns the y-intercept of the Line;
• .xInt(), a method which has no outside arguments and returns the x-intercept of the Line;
• .y value(), a method which takes an x value as outside input, and returns the y coordinate of the point of the
Line with that x value.
a. Write the definition for the class Line, with methods as described.
b. Suppose that the class Line works. Create a Line object called pretty line that represents the line y = 4x + 2.
Then, print the x- and y-intercepts of pretty line. (Obviously, use the methods you implemented.)
c. Suppose that x vals = [4.1, 5.2, 8.3, 9.1, 6.1] is a list of x-values. Write a loop that goes through this list,
and prints out the corresponding y-values of to these x-values on pretty line. (Again, use the method you
implemented.0
3. Consider a class called Account whose objects have the following attribute variables:
• . name, representing the name of the account holder;
• . prin, representing the principal;
• . rate, representing the interest rate (as a decimal rather than a percent);
and the following methods:
• a constructor, which takes three outside arguments, a str and two floats, which set the attribute variables;
• .presentValue(), which takes a time in years as an (outside) argument, and returns the present value of the
account at that time (using the formula P resentV alue = P rincipal ∗ e
rt).
a. Write the definition for the class Account.
b. Write client code to do the following: create an Account named x with account holder named Bob, with principal of
300 dollars and an interest rate of 5%. Then, have the user enter a time t; and print out "You’re rich!" if the present
value of the account at time t is greater than 500 dollars.
c. Implement the operator < for Accounts. In other words, write a function so that if x and y are Accounts, then x < y
won’t cause an error. (Perhaps this operator should compare the principals of the two accounts – that would make
sense, right?)
4. Consider a class called Student whose objects have the following attributes:
• . name, representing the name of the student;
• . scores, representing a list of test scores;
and the following methods:
• a constructor which takes in a str and a list of floats, which sets the attribute variables;
• .average() which takes no (outside) arguments, and returns the average of the test scores;
• .pass() which takes no (outside) arguments, and returns true if the average was greater than or equal to 60 and
false otherwise.
a. Write the class definition for the class Student. Write the function .pass() without making any direct reference to
. scores.
b. In client code, create a new Student object named x, whose name is Evan, with scores 20, 30 and 40; and then print
out this Student’s average using the member function.
5. Consider a class called Truck, meant to model an individual truck in a company’s shipping fleet. The objects of this
class should have the following attribute variables:
• .max items, representing the maximum number of items that the truck can hold;
• .max weight, representing the maximum weight of items that the truck can hold;
• .cur items, representing the current number of items that the truck can hold;
• .cur weight, representing the current total weight of items that the truck can hold;
and the following methods:
• a constructor which takes two arguments, the max number of items and the max weight for a given truck, which
sets all four attribute variables – assume that every truck starts out as empty;
• .add on(), which takes one float named w as an (outside) argument. This function should attempt to add 1 item
with weight w to the truck: specifically, if the item doesn’t cause the truck’s number of items or weight to exceed
the maximums, the function should update the member variables appropriately, and return true; otherwise, the
function shouldn’t update any variables and return false.
a. Write the class definition for the class Truck.
b. Write client code that does the following: create a new Truck object named biggie, whose weight capacity is 15000
and which can hold 80 items. Then, have the user input a weight for an item. The program should then add on an item
with that weight to biggie – and if the item is “rejected” because it is too heavy, the program should print out a
message that says FAIL.
6. Consider a class called Section, meant to model an individual section of a college course. The objects of the class
should have the following attribute variables:
• .capacity, representing the maximum number of participants that can be in the section (including the
instructor);
• .current num, representing the current number of participants (including the instructor);
• a list of strs named .participants, representing the current list of the names of the participants (including the
instructor);
and the following public members:
2
• a constructor which takes two arguments, the section capacity and the name of the instructor, which initializes the
three attribute variables – assume that the section starts out as containing only the instructor as participant;
• a function .enroll(), which takes one string named s as an (outside) argument. This function should attempt
to enroll a student with name s to the section: specifically, if the extra student doesn’t cause the section’s current
number of participants to exceed the capacity, the function should update the member variables appropriately, and
return True; otherwise, the function shouldn’t update any variables and return False.
a. Write the class definition for the class Section.
b. In client code, suppose that an int variable cap and a list of names called new students have already been
initialized. Write code that does the following: create a new Section object named ef3300, whose capacity is cap and
whose instructor is Evan. The program should then attempt to add all the students from new students to ef3300 –
and if the any of the students is “rejected” because the class is full, the program should print out a message that says
NOT ENROLLED.
c. The function iadd overloads the += operator in Python. Implement this operator so that if s is a Section and n
is an integer, then s += n increases the capacity of s.
3
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。