CS61A Lab 6

news/2024/11/25 7:38:24/

更好的阅读体验

Lab 6: Object-Oriented Programming lab06.zip

What Would Python Do?

These questions use inheritance. For an overview of inheritance, see the inheritance portion of Composing Programs.

Q1: WWPD: Classy Cars

Below is the definition of a Car class that we will be using in the following WWPD questions.

Note: The Car class definition can also be found in car.py.

class Car:num_wheels = 4gas = 30headlights = 2size = 'Tiny'def __init__(self, make, model):self.make = makeself.model = modelself.color = 'No color yet. You need to paint me.'self.wheels = Car.num_wheelsself.gas = Car.gasdef paint(self, color):self.color = colorreturn self.make + ' ' + self.model + ' is now ' + colordef drive(self):if self.wheels < Car.num_wheels or self.gas <= 0:return 'Cannot drive!'self.gas -= 10return self.make + ' ' + self.model + ' goes vroom!'def pop_tire(self):if self.wheels > 0:self.wheels -= 1def fill_gas(self):self.gas += 20return 'Gas level: ' + str(self.gas)

For the later unlocking questions, we will be referencing the MonsterTruck class below.

Note: The MonsterTruck class definition can also be found in car.py.

 class MonsterTruck(Car):size = 'Monster'def rev(self):print('Vroom! This Monster Truck is huge!')def drive(self):self.rev()return super().drive()

You can find the unlocking questions below.

Use Ok to test your knowledge with the following “What Would Python Display?” questions:

python3 ok -q wwpd-car -u✂️

Important: For all WWPD questions, type Function if you believe the answer is <function...>, Error if it errors, and Nothing if nothing is displayed.

>>> deneros_car = Car('Tesla', 'Model S')
>>> deneros_car.model______
>>> deneros_car.gas = 10
>>> deneros_car.drive()______
>>> deneros_car.drive()______
>>> deneros_car.fill_gas()______
>>> deneros_car.gas______
>>> Car.gas______
>>> deneros_car = Car('Tesla', 'Model S')
>>> deneros_car.wheels = 2
>>> deneros_car.wheels______
>>> Car.num_wheels______
>>> deneros_car.drive()______
>>> Car.drive()______
>>> Car.drive(deneros_car)______
>>> deneros_car = MonsterTruck('Monster', 'Batmobile')
>>> deneros_car.drive()______
>>> Car.drive(deneros_car)______
>>> MonsterTruck.drive(deneros_car)______
>>> Car.rev(deneros_car)______

What would Python display? If you get stuck, try it out in the Python
interpreter!>>> from car import *
>>> deneros_car = Car('Tesla', 'Model S')
>>> deneros_car.model
? Model S
-- Not quite. Try again! --? 'Model S'
-- OK! -->>> deneros_car.gas = 10
>>> deneros_car.drive()
? Tesla Model S goes vroom!
-- Not quite. Try again! --? 'Tesla Model S goes vroom!'
-- OK! -->>> deneros_car.drive()
? 'Cannot drive'
-- Not quite. Try again! --? 'Cannot drive!'
-- OK! -->>> deneros_car.fill_gas()
? 'Gas level: 20'
-- OK! -->>> deneros_car.gas
? 20
-- OK! -->>> Car.gas
? 30
-- OK! -----------------------------------------------------------------------
Car > Suite 1 > Case 2
(cases remaining: 2)What would Python display? If you get stuck, try it out in the Python
interpreter!>>> from car import *
>>> deneros_car = Car('Tesla', 'Model S')
>>> deneros_car.wheels = 2
>>> deneros_car.wheels
? 2
-- OK! -->>> Car.num_wheels
? 4
-- OK! -->>> deneros_car.drive() # Type Error if an error occurs and Nothing if nothing is displayed
? 'Cannot drive'
-- Not quite. Try again! --? 'Cannot drive!'
-- OK! -->>> Car.drive() # Type Error if an error occurs and Nothing if nothing is displayed
? Nothing
-- Not quite. Try again! --? Error
-- OK! -->>> Car.drive(deneros_car) # Type Error if an error occurs and Nothing if nothing is displayed
? 'Cannot drive!'
-- OK! -----------------------------------------------------------------------
Car > Suite 1 > Case 3
(cases remaining: 1)What would Python display? If you get stuck, try it out in the Python
interpreter!>>> from car import *
>>> deneros_car = MonsterTruck('Monster', 'Batmobile')
>>> deneros_car.drive() # Type Error if an error occurs and Nothing if nothing is displayed
(line 1)? Vroom! This Monster Truck is huge!
(line 2)? 'Monster Batmobile goes vroom!'
-- OK! -->>> Car.drive(deneros_car) # Type Error if an error occurs and Nothing if nothing is displayed
? 'Monster Batmobile goes vroom!'
-- OK! -->>> MonsterTruck.drive(deneros_car) # Type Error if an error occurs and Nothing if nothing is displayed
(line 1)? Vroom! This Monster Truck is huge!
(line 2)? 'Monster Batmobile goes vroom!'
-- OK! -->>> Car.rev(deneros_car) # Type Error if an error occurs and Nothing if nothing is displayed
? Vroom! This Monster Truck is huge!
-- Not quite. Try again! --? Error
-- OK! -----------------------------------------------------------------------
OK! All cases for Car unlocked.

Parsons Problems

To work on these problems, open the Parsons editor:

python3 parsons

Q2: Cool Cats

The Cat class models a cat: you can find the implementation below. Now, you will implement NoisyCat; NoisyCats are very similar to Cats, but talks twice as much. However, in exchange for such great powers, it gives up one of its initial lives.

Use superclass methods wherever possible.

class Cat:def __init__(self, name, owner, lives=9):self.is_alive = Trueself.name = nameself.owner = ownerself.lives = livesdef talk(self):return self.name + ' says meow!'class NoisyCat(Cat):""">>> my_cat = NoisyCat("Furball", "James")>>> my_cat.name'Furball'>>> my_cat.is_aliveTrue>>> my_cat.lives8>>> my_cat.talk()'Furball says meow! Furball says meow!'>>> friend_cat = NoisyCat("Tabby", "James", 2)>>> friend_cat.talk()'Tabby says meow! Tabby says meow!'>>> friend_cat.lives1"""def __init__(self, name, owner, lives=9):Cat.__init__(self, name, owner, lives)self.lives -= 1def talk(self):words = Cat.talk(self)words = words + ' ' + wordsreturn words

Coding Practice

Q3: Cat Adoption

So far, you’ve implemented the NoisyCat based off of the Cat class. However, you now want to be able to create lots of different Cats!

Build on the Cat class from the earlier problem by adding a class method called adopt_a_cat. This class method allows you to create Cats that can then be adopted.

Specifically, adopt_a_cat should return a new instance of a Cat whose owner is owner.

This Cat instance’s name and number of lives depends on the owner. Its name should be chosen from cat_names (provided in the skeleton code), and should correspond to the name at the index len(owner) % (modulo) the number of possible cat names. Its number of lives should be equal to len(owner) + the length of the chosen name.

class Cat:def __init__(self, name, owner, lives=9):self.is_alive = Trueself.name = nameself.owner = ownerself.lives = livesdef talk(self):return self.name + ' says meow!'@classmethoddef adopt_a_cat(cls, owner):"""Returns a new instance of a Cat.This instance's owner is the given owner.Its name and its number of lives is chosen programaticallybased on the spec's noted behavior.>>> cat1 = Cat.adopt_a_cat("Ifeoma")>>> isinstance(cat1, Cat)True>>> cat1.owner'Ifeoma'>>> cat1.name'Felix'>>> cat1.lives11>>> cat2 = Cat.adopt_a_cat("Ay")>>> cat2.owner'Ay'>>> cat2.name'Grumpy'>>> cat2.lives8"""cat_names = ["Felix", "Bugs", "Grumpy"]"*** YOUR CODE HERE ***"cat_name = cat_names[len(owner) % 3]lives = len(owner) + len(cat_name)return cls(cat_name, owner, lives)

Use Ok to test your code:

python3 ok -q Cat.adopt_a_cat✂️

Accounts

Let’s say we’d like to model a bank account that can handle interactions such as depositing funds or gaining interest on current funds. In the following questions, we will be building off of the Account class. Here’s our current definition of the class:

class Account:"""An account has a balance and a holder.>>> a = Account('John')>>> a.deposit(10)10>>> a.balance10>>> a.interest0.02>>> a.time_to_retire(10.25) # 10 -> 10.2 -> 10.4042>>> a.balance               # balance should not change10>>> a.time_to_retire(11)    # 10 -> 10.2 -> ... -> 11.0408080325>>> a.time_to_retire(100)117"""max_withdrawal = 10interest = 0.02def __init__(self, account_holder):self.balance = 0self.holder = account_holderdef deposit(self, amount):self.balance = self.balance + amountreturn self.balancedef withdraw(self, amount):if amount > self.balance:return "Insufficient funds"if amount > self.max_withdrawal:return "Can't withdraw that amount"self.balance = self.balance - amountreturn self.balance

Q4: Retirement

Add a time_to_retire method to the Account class. This method takes in an amount and returns how many years the holder would need to wait in order for the current balance to grow to at least amount, assuming that the bank adds balance times the interest rate to the total balance at the end of every year.

    def time_to_retire(self, amount):"""Return the number of years until balance would grow to amount."""assert self.balance > 0 and amount > 0 and self.interest > 0"*** YOUR CODE HERE ***"cnt = 0deposit = self.balance * ((1 + self.interest) ** cnt)while deposit < amount:cnt = cnt + 1deposit = self.balance * ((1 + self.interest) ** cnt)return cnt

Use Ok to test your code:

python3 ok -q Account✂️

Q5: FreeChecking

Implement the FreeChecking class, which is like the Account class from lecture except that it charges a withdraw fee after 2 withdrawals. If a withdrawal is unsuccessful, it still counts towards the number of free withdrawals remaining, but no fee for the withdrawal will be charged.

Hint: Don’t forget that FreeChecking inherits from Account! Check the Inheritance section in Topics for a refresher.

class FreeChecking(Account):"""A bank account that charges for withdrawals, but the first two are free!>>> ch = FreeChecking('Jack')>>> ch.balance = 20>>> ch.withdraw(100)  # First one's free'Insufficient funds'>>> ch.withdraw(3)    # And the second17>>> ch.balance17>>> ch.withdraw(3)    # Ok, two free withdrawals is enough13>>> ch.withdraw(3)9>>> ch2 = FreeChecking('John')>>> ch2.balance = 10>>> ch2.withdraw(3) # No fee7>>> ch.withdraw(3)  # ch still charges a fee5>>> ch.withdraw(5)  # Not enough to cover fee + withdraw'Insufficient funds'"""withdraw_fee = 1free_withdrawals = 2"*** YOUR CODE HERE ***"def __init__(self, Account):Account.__init__(self)def withdraw(self, amount):if amount + self.withdraw_fee > self.balance:return "Insufficient funds"if amount > self.max_withdrawal:return "Can't withdraw that amount"if self.free_withdrawals > 0: self.free_withdrawals -= 1self.balance = self.balance - amountif self.free_withdrawals <= 0:self.balance = self.balance - self.withdraw_feereturn self.balance

Use Ok to test your code:

python3 ok -q FreeChecking✂️

Submit

Make sure to submit this assignment by running:

python3 ok --submit

http://www.ppmy.cn/news/4337.html

相关文章

数据聚合、数据同步

文章目录数据聚合Bucket聚合语法聚合结果排序限定聚合范围Metric聚合语法RestAPI实现聚合数据同步发送MQ消息接收MQ消息数据聚合 Bucket聚合语法 GET /hotel/_search {"size": 0, // 设置size为0&#xff0c;结果中 不包含文档&#xff0c;只包含聚合结果~"…

行为树 --- [7] BehaviorTree.CPP 4.x版本的编译及使用

根据BehaviorTree.CPP的官方介绍&#xff0c;3.x版本已经不再维护了&#xff0c;建议使用4.x版本&#xff0c; 4.x版本和3.x版本的区别可以看这里 — https://www.behaviortree.dev/migration 本文介绍4.x版本的编译及使用&#xff0c;环境是Debian 10.5&#xff0c;编译器是…

<Linux进程通信之管道>——《Linux》

目录 一、进程通信 1.进程间通信介绍 2.进程间通信目的 3.进程间通信发展 4.进程间通信分类 二、管道 1.什么是管道 2.匿名管道 3.用fork来共享管道原理 4.站在文件描述符角度-深度理解管道​编辑 5.编程模拟实现父子进程在管道读写通信​编辑 6.进程控制&#xff…

nodejs+vue校园用车辆校车管理系统

本项目的应用场景描述如下&#xff1a;为减少学生等待校车的时间&#xff0c;合理安排校车调度&#xff0c;设计并开发一个校车预约系统&#xff0c;系统由手机端、服务器端、车载刷卡端三部分组成。学生通过手机应用&#xff08;或微信应用&#xff09;查看校车运行时段&#…

将 Bean 注入 Spring 容器的方法

这里写目录标题1. Configuration Bean2. Componet ComponentScan3. Import 注解导入3.1 Import 直接导入类3.2 Import ImportSelector接口3.3 Import DeferredImportSelector接口3.4 Import ImportBeanDefinitionRegistrar接口4. FactoryBean接口5. BeanDefinitionRegistr…

三层-生成树-ospf-速端口思科练习实验

♥️作者&#xff1a;小刘在这里 ♥️每天分享云计算网络运维课堂笔记&#xff0c;疫情之下&#xff0c;你我素未谋面&#xff0c;但你一定要平平安安&#xff0c;一 起努力&#xff0c;共赴美好人生&#xff01; ♥️夕阳下&#xff0c;是最美的&#xff0c;绽放&#xff0c;…

大数据呀大数据

大数据 啥是大数据? 我自己的一些经历–>>在做代码开发之前,一直觉得大数据这件事是高大上的一门学问,门槛很高,而且我上大学的时候这个词才刚刚在众多互联网记住中展露头角; 我的个人经历–>> 我上大学时那时候安卓的版本才到安卓4.4,在智能手机出来普及以前,各…

json对象数组集合-第10讲

假设我们有如下是一组数据&#xff1a; { "my_array": [{ "name": "www", "age": 120 }, { "name": "wkf&q…