Programming Activity – 10.4

Screen Shot 2017-06-05 at 上午12.30.33

Line29 super() explicitly calls the superclass BankAccount default constructor. the private field interestRate is set to a fixed value 0.03.

 

Screen Shot 2017-06-05 at 上午12.30.40

super(startBalance) explicitly calls the superclass BankAccound overloaded class with the required parameter startBalance. startInterestRate is also a required input of this overloaded class for calling the method setInterestRate.

Screen Shot 2017-06-05 at 上午12.30.47

Screen Shot 2017-06-05 at 下午3.07.14.png

The method applyInterest() calls the method deposit() in superclass.

Screen Shot 2017-06-05 at 上午12.30.57Screen Shot 2017-06-05 at 上午12.31.02

I created a new toString() method in this class but I can still access to the toString() method in the superclass by using super.toString().

I created a NumberFormat object for formatting the field interestRate by using the getPercentageInstance() method.

There is another way to write this:

NumberFormat percentage = new NumberFormat.getPercentInstance();

super.toString() + percentage.format(interestRate);

Screen Shot 2017-06-05 at 下午3.06.59.png

Real Life Application & Conclusion

This logarithm can be applied to bank system and any investment system. The inheritance function is really useful since there is no repetition of codes. The toString() is a new method too which shows the value stored in the system. And remember, when you create a new toString() in sub class, you can still use super.toString() to call the toString() in the super class.

留下评论