← Back to Encapsulation
Question 578
Immutable Money Type
Immutable Money Type
TheMoneyclass stores tworeadonlyfields,amount(int) andcurrency(string), set in the constructor. Add a methodAdd(int extra)that returns a NEWMoneywithamount + extraand the same currency (it must not modify the current object), and a methodDisplay()that returns the amount and currency separated by a single space. Do NOT write aMainmethod. Fornew Money(10, "USD").Add(5).Display()the program prints the output shown in Expected Output.
Expected Output:
15 USD= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Encapsulation
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cw, cr, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.