Introduction to Computer Science II Homework 3 Due by 5:45pm on Tuesday, January 28 Reading Read chapter 8, and the week 2 lecture notes. Problems Solve the following by implementing the corresponding...



Introduction to Computer Science II


Homework 3


Due by 5:45pm on Tuesday, January 28




Reading

Read chapter 8, and the week 2 lecture notes.



Problems

Solve the following by implementing the corresponding functions inhomework3.pyandincrement.py(for Problem 3). When done, submit that file throughD2L.





1.
[Lab]Implement classSavingsAccountas a subclass of the classBankAccountyou developed in homework assignment 2. Your implementation should override the inherited construct method __init__() and add an additional method addInterest() using the following specs:


  • __init__(): takes as input the interest rate and the balance (default value 0).

  • addInterest(): computes and adds the interest to the balance



Usage:
>>> s = SavingsAccount(3, 100)
>>> s.balance()
100
>>> s.addInterest()
>>> s.balance()
103.0
>>> s = SavingsAccount(3)
>>> s.deposit(1000)
>>> s.addInterest()
>>> s.balance()
1030.0






2.
[Lab]As noted in class, there are some problems with the implementation of the classBankAccount, and they are illustrated here:

>>> x = BankAccount(-700)
>>> x.balance()
-700
>>> x.withdraw(70)
>>> x.balance()
-770
>>> x.deposit(-7)
>>> x.balance()
Balance: -777

The problems are: (1) a bank account with a negative balance can be created, (2) the withdrawal amount is greater than the balance, and (3) the deposit amount is negative. Modify the code for theBankAccountclass so that aValueErrorexception is thrown for any of these violations, together with an appropriate message:'Illegal balance','Overdraft', or'Negative deposit'. For example:

Usage:
>>> x = BankAccount2(-700)
Traceback (most recent call last):
...
ValueError: Negative balance
>>> x = BankAccount2(700)
>>> x.withdraw(800)
Traceback (most recent call last):
...
ValueError: Overdraft
>>> x.deposit(-100)
Traceback (most recent call last):
...
ValueError: Negative deposit






3.
[Lab]Implement inincrement.pya GUI that has two buttons:





The button labeledIncrementwill increment an integer n and the button labeled Show will show the value ofnin a popup window. Hint: Makena global variable.

Usage:
After starting the GUI, if you were to click theShowbutton, a popup window will appear showing the initial value ofn:



ClickOK. Now suppose you click theIncrementbutton once and then ckick Show. You will see another popup window:



ClickOK. Now suppose you click theIncrementbutton three times and then ckick Show. You will see another popup window:









4.
In problem 2, a genericValueErrorexception is raised if any of the three violations occur. It would be more useful if a more specific, user-defined exception is raised instead. Define new exception classesNegativeBalanceError,OverdraftError, andDepositErrorthat would be raised instead. In addition, the informal string representation of the exception object should contain the balance that would result from the negative balance account creation, the overdraft, or the negative deposit. For example, when trying to create a bank account with a negative balance, the error message should include the balance that would result if the bank account creation was allowed:

>>> x = BankAccount3(-5)
Traceback (most recent call last):
...
NegativeBalanceError: Account created with negative balance -5

When a withdrawal results in a negative balance, the error message should also include the balance that would result if the withdrawal was allowed:

>>> x = BankAccount3(5)
>>> x.withdraw(7)
Traceback (most recent call last):
...
OverdraftError: Operation would result in negative balance -2

If a negative deposit is attempted, the negative deposit amount should be included in the error message:

>>> x.deposit(-3)
Traceback (most recent call last):
...
DepositError: Negative deposit -3

Finally, reimplement the classBankAccountto use these new exception classes instead ofValueError.



5.
Develop a classTextfilethat provides methods to analyze a text file. The class Textfile will support a constructor that takes as input a file name (as a string) and instantiates aTextfileobject associated with the corresponding text file. The Textfile class should support methodsnchars(),nwords(), andnlines()that return the number of characters, words, and lines, respectively, in the associated text file. The class should also support methodsread()andreadlines()that return the content of the text file as a string or as a list of lines, respectively, just as we would expect for file objects. Finally, the class should support methodgrep()that takes a target string as input and searches for lines in the text file that contain the target string. The method returns the lines in the file containing the target string; in addition, the method should print the line number, where line numbering starts with 0.


Test your implementation using the test fileraven.txt.

Usage:
>>> t = Textfile('raven.txt')
>>> t.nchars()
6299
>>> t.nwords()
1125
>>> t.nlines()
126
>>> print(t.read())
Once upon a midnight dreary, while I pondered weak and weary, ... Shall be lifted - nevermore!
>>> t.grep('nevermore')
75: Of `Never-nevermore.`
89: She shall press, ah, nevermore!
124: Shall be lifted - nevermore!






6.
Add methodwords()to classTextfile. It takes no input and returns a list, without duplicates, of words in the file.

Usage:
>>> t = Textfile('raven.txt')
>>> t.words()
['Ah', 'Aidenn', 'And', 'As', 'Back', 'Be', 'Bird', 'But', 'By', 'Caught', 'Clasp', 'Darkness',
...
'wondering', 'word', 'wore', 'wrought', 'yet', 'yore', 'you', 'your']




7.
Add methodoccurrences()to classTextfile. It takes no input and returns a dictionary mapping each word in the file (the key) to the number of times it occurs in the file (the value).

Usage:
>>> t = Textfile('raven.txt')
>>> t.occurences()
{'Once': 1, 'upon': 4, 'a': 15, 'midnight': 1, 'dreary': 1, 'while': 1, 'I': 32, 'pondered': 1,
...
'streaming': 1, 'throws': 1, 'shadow': 2, 'lies': 1, 'floating': 1, 'Shall': 1, 'lifted': 1}



Jan 27, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here