Question
Using Java programming knowledge to answer it. An Octopus card stores its balance on a chip. Its user can add any amount up to the
Using Java programming knowledge to answer it.
An Octopus card stores its balance on a chip. Its user can add any amount up to the limit of $1,000. When a user buys a card, s/he needs to pay $100 as the initial balance. A class Octopus models the Octopus card and the variable balance represents its balance.
(a) Write the getter/setter methods getBalance() and setBalance() of Octopus with suitable return types and parameters.
(b) Should the limit $1,000 be stored in a variable? If yes, should it be stored in a class variable or an instance variable? If no, why not store it?
(c) Write a method addAmount(double anAmount) to add an amount to the current balance. It returns -1 without adding the amount if the resulting balance exceeds 1000. Otherwise, it returns 0 with the amount added.
(d) Write an instance method subtractAmount(double anAmount) to subtract anAmount from the current balance. It returns -1 without subtracting the amount if the current balance is negative. Otherwise, it returns 0 with the amount substracted.
(e) When a user is taking KCR or MTR, the amount is not debited immediately when a user is going into the paid area through the turnstile. It is debited when the user goes out of the paid area.
(i) What extra information in the class Octopus is needed for this operation? Define a suitable data structure so that the correct debit operation can be carried out.
(ii) Implement the new instance methods recordSource(String source) and debit(String destination) to record the source station and debit the correct amount when the user goes out of the paid area of the destination station respectively.
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