Question
*** OBJECT-ORIENTED PROGRAMMING IN R Code in R6 to see. BankAccount
*** OBJECT-ORIENTED PROGRAMMING IN R
Code in R6 to see.
BankAccount <- R6Class( classname = "BankAccount", public = list( balance = 0, deposit = function(dep = 0) { self$balance <- self$balance + dep invisible(self) }, withdraw = function(draw) { self$balance <- self$balance - draw invisible(self) } ) )
my_account <- BankAccount$new() my_account$balance
my_account$ deposit(5)$ withdraw(10)$ balance my_account$balance
****** The above is written in R6 version, please work in S4.
Question.
Implement the bank account as an S4 class called `BankAccount4`. (i) In your class definition include a prototype that sets the balance to `NA_real_` (a missing value of type double). (ii) Write a helper that creates objects of class `BanKAccount4` and allows the user to set the initial balance, with default zero. Have the helper function coerce the balance to a double before creating the object. (iii) Write a `show`()` method that prints the object nicely. (iv) Define generics and methods for making deposits and withdrawls; because these methods modify the balance they should include calls to `validObject()`. (v) Use `methods()` to find all methods for objects of class `BankAccount4`.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started