Home /
Expert Answers /
Computer Science /
answer-in-javascript-please-the-goal-of-this-problem-is-to-use-prototypal-inheritance-in-javascr-pa269
(Solved):
Answer in JavaScript please.
The goal of this problem is to use prototypal inheritance in Javascr ...
Answer in JavaScript please.
The goal of this problem is to use prototypal inheritance in Javascript. Implement inheritance as described below- Create a function Activity that takes a single parameter omount (Number) and assigns it to member variable 'omount'. Add the following functions to the Activity prototype - 1. setAmount - This function takes a single parameter, value. - If the value is less than or equal to 0 , it returns false. - Otherwise, it assigns value to the member variable amount and returns true. 2. getAmount - This function returns the member variable amount value. Create a function Payment that - 1. inherits from parent Activity. 2. takes 2 parameters - amount (Number) and receiver (string). It assigns the parent's member variable 'amount', and self's member variable 'receiver' respectively. Add the following functions to Payment's existing prototype - 1. setReceiver - This function takes a single parameter and assigns it to the member variable 'receiver'. 2. getReceiver - This function returns the member variable 'receiver'value. Create a function Refund that - 1. inherits from parent Activity. 2. takes 2 parameters - amount (Number) and sender (string) and assigns the parent's member variable, 'amount', and self's member variable, 'sender'. Add below functions to Refund's existing prototype - 1. setSender - This function takes a single parameter and assigns it to the member variable sender. 2. getSender - This function returns the member variable sender. Implementation of the function will be tested by the provided code stub using several input files. Each input file contains parameters for the function calls. The result of their executions will be printed to the standard output by the provided code. In the case of a setAmount function call, if the value returned is false, the stubbed code prints 'Amount not updated'. If the value returned is true, it prints 'Amount updates to \( \langle \) value \( > \) '. - Input Format For Custom Testing The first line specifies the function name for which object needs to be created i.e. either Payment or Refund. The second line contains space-separated initial amount and receiver/sender values for initial object creation. The third line contains an integer to update the amount. The fourth line contains a string to update the receiver/sender value of the object. Sample Case 0 Sample Input For Custom Testing \begin{tabular}{lc} STDIN & Function \\ \hline Payment & rarr object to create \( = \) Payment \\ 5000 John & rarr \( \quad \) initial amount \( =5000 \), initial receiver = 'John' \\ 4000 & rarr update amount to 4000 \\ John B & rarr \end{tabular} Sample Output Payment object created with amount 5000 and receiver John Amount updated to 4000 Receiver updated to John B Payment object details - amount is 4000 and receiver is John B Payment.prototype has property setAmount: false Payment.prototype has property getAmount: false Payment.prototype has property setReceiver: true Payment.prototype has property getReceiver: true Explanation A Payment object is created with the amount as 5000 and receiver as 'John' (inputs from the second line). The third and fourth lines have updated amount and receiver values that are used to update the object's member variables using setAmount and setReceiver functions. Since setAmount returns true in this case, it prints 'Amount updated to 4000 '. The stub code then calls the getAmount and getReceiver functions and prints the values. The last 4 lines check if the object prototype chain is built correctly.