From b9a77bac662b0654ea99d7d54fe68b915a470827 Mon Sep 17 00:00:00 2001 From: Mauricio Bengochea Torres Date: Fri, 5 Sep 2025 15:49:22 +0200 Subject: [PATCH] oop lab extra answers to lab-python-oop.ipynb --- lab-python-oop.ipynb | 278 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 258 insertions(+), 20 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..b089ed5 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,85 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 Number: 1\n", + "Account 2 Number: 2\n", + "Account 1 Balance: 1000\n", + "Account 2 Balance: 500\n", + "Account 1 Balance: 800\n", + "Account 2 Balance: 400\n" + ] + } + ], "source": [ - "# your code goes here\n", - "\n" + "class BankAccount:\n", + " account_count = 0\n", + "\n", + " def __init__(self):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = 0\n", + "\n", + " def deposit(self, amount):\n", + " self.balance += amount\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > self.balance:\n", + " print(\"Insufficient funds\")\n", + " else:\n", + " self.balance -= amount\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n", + "\n", + "# Test the class\n", + "account1 = BankAccount()\n", + "account2 = BankAccount()\n", + "\n", + "print(\"Account 1 Number:\", account1.get_account_number())\n", + "print(\"Account 2 Number:\", account2.get_account_number())\n", + "\n", + "account1.deposit(1000)\n", + "account2.deposit(500)\n", + "\n", + "print(\"Account 1 Balance:\", account1.get_balance())\n", + "print(\"Account 2 Balance:\", account2.get_balance())\n", + "\n", + "account1.withdraw(200)\n", + "account2.withdraw(100)\n", + "\n", + "print(\"Account 1 Balance:\", account1.get_balance())\n", + "print(\"Account 2 Balance:\", account2.get_balance())" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "BankAccount.__init__() takes 1 positional argument but 2 were given", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[6], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m account1 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m 4\u001b[0m account2 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m500\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccount 1 balance:\u001b[39m\u001b[38;5;124m\"\u001b[39m, account1\u001b[38;5;241m.\u001b[39mget_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", + "\u001b[1;31mTypeError\u001b[0m: BankAccount.__init__() takes 1 positional argument but 2 were given" + ] + } + ], "source": [ "# Testing the BankAccount class\n", "# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n", @@ -117,12 +181,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 Number: 3\n", + "Account 2 Number: 4\n", + "Account 1 Interest Rate: 0.01\n", + "Account 2 Interest Rate: 0.05\n", + "Account 1 Balance: 1000\n", + "Account 2 Balance: 500\n", + "Account 1 Balance after interest: 1010.0\n", + "Account 2 Balance after interest: 525.0\n", + "Account 1 Balance: 810.0\n", + "Account 2 Balance: 425.0\n" + ] + } + ], "source": [ - "# your code goes here" + "class SavingsAccount(BankAccount):\n", + " def __init__(self, interest_rate=0.01):\n", + " super().__init__()\n", + " self.interest_rate = interest_rate\n", + "\n", + " def add_interest(self):\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate\n", + "\n", + "\n", + "# Test the class\n", + "account1 = SavingsAccount()\n", + "account2 = SavingsAccount(0.05)\n", + "\n", + "print(\"Account 1 Number:\", account1.get_account_number())\n", + "print(\"Account 2 Number:\", account2.get_account_number())\n", + "\n", + "print(\"Account 1 Interest Rate:\", account1.get_interest_rate())\n", + "print(\"Account 2 Interest Rate:\", account2.get_interest_rate())\n", + "\n", + "account1.deposit(1000)\n", + "account2.deposit(500)\n", + "\n", + "print(\"Account 1 Balance:\", account1.get_balance())\n", + "print(\"Account 2 Balance:\", account2.get_balance())\n", + "\n", + "account1.add_interest()\n", + "account2.add_interest()\n", + "\n", + "print(\"Account 1 Balance after interest:\", account1.get_balance())\n", + "print(\"Account 2 Balance after interest:\", account2.get_balance())\n", + "\n", + "account1.withdraw(200)\n", + "account2.withdraw(100)\n", + "\n", + "print(\"Account 1 Balance:\", account1.get_balance())\n", + "print(\"Account 2 Balance:\", account2.get_balance())" ] }, { @@ -151,12 +271,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", + "account = SavingsAccount(0.02)\n", + "account.deposit(100)\n", + "\n", + "# Deposit $50 into the savings account\n", + "account.deposit(50)\n", + "\n", + "# Withdraw $25 from the savings account\n", + "account.withdraw(25)\n", + "\n", + "# Add interest to the savings account (use the default 0.01)\n", + "account.add_interest()\n", + "\n", + "# Print the current balance and interest rate of the savings account\n", + "print(\"Current balance:\", account.get_balance())\n", + "print(\"Interest rate:\", account.get_interest_rate())" ] }, { @@ -189,12 +333,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Transaction count: 4\n", + "Deducted $4 in fees\n", + "Balance after deducting fees: 146\n", + "Transaction count after resetting: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "class CheckingAccount(BankAccount):\n", + " def __init__(self, transaction_fee=1):\n", + " super().__init__()\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " def deposit(self, amount):\n", + " super().deposit(amount)\n", + " self.transaction_count += 1\n", + "\n", + " def withdraw(self, amount):\n", + " super().withdraw(amount)\n", + " self.transaction_count += 1\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees > self.balance:\n", + " print(\"Insufficient balance to deduct fees\")\n", + " else:\n", + " self.balance -= total_fees\n", + " print(f\"Deducted ${total_fees} in fees\")\n", + " self.transaction_count = 0\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count\n", + "\n", + "\n", + "# Test the class\n", + "account = CheckingAccount()\n", + "\n", + "account.deposit(100)\n", + "account.withdraw(50)\n", + "account.deposit(200)\n", + "account.withdraw(100)\n", + "\n", + "print(\"Transaction count:\", account.get_transaction_count())\n", + "account.deduct_fees()\n", + "print(\"Balance after deducting fees:\", account.get_balance())\n", + "account.reset_transactions()\n", + "print(\"Transaction count after resetting:\", account.get_transaction_count())" ] }, { @@ -234,18 +431,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deducted $6 in fees\n", + "Current balance: 544\n", + "Transaction count: 0\n", + "Deducted $4 in fees\n", + "Current balance: 665\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# Create a new checking account with a balance of 500 dollars and a transaction fee of 2 dollars\n", + "account = CheckingAccount(transaction_fee=2)\n", + "account.deposit(500)\n", + "\n", + "# Deposit 100 dollars into the account\n", + "account.deposit(100)\n", + "\n", + "# Withdraw 50 dollars from the account\n", + "account.withdraw(50)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "account.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count\n", + "print(\"Current balance:\", account.get_balance())\n", + "print(\"Transaction count:\", account.get_transaction_count())\n", + "\n", + "# Deposit 200 dollars into the account\n", + "account.deposit(200)\n", + "\n", + "# Withdraw 75 dollars from the account\n", + "account.withdraw(75)\n", + "\n", + "# Deduct the transaction fees from the account\n", + "account.deduct_fees()\n", + "\n", + "# Get the current balance and transaction count again\n", + "print(\"Current balance:\", account.get_balance())\n", + "print(\"Transaction count:\", account.get_transaction_count())" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -259,7 +497,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,