Programming IT, Exercise book 2,
These exercises are designed to help you become familiar with creating and working with objects. As with the previous exercise book, work through at your own pace!
1. In the lectures we made a class to represent a person. Make your own Person class, including one additional attribute, or one additional method. All attributes should be private, and getters and setters should be included to allow attributes to be changed.
2. In a separate file (i.e. a separate class), write a main method that demonstrates the use of your Person class.
Copyright By PowCoder代写 加微信 powcoder
3. Give your Person class a toString method and use it in your main method.
2 Bank account
The aim of these tasks is to produce an object that will represent a bank account. You will also use your person class again.
1. Create a class that represents the skeleton of a bank account (call it BankAccount). It should attributes for:
a. The account holders name (name)
b. The account number (accountNumber)
c. The account balance (balance; how much money is left in the account)
d. A static int attribute called nextAccountNumber that is initially set to zero.
2. Create a constructor for the account that takes as an argument the account holders name. In the constructor set:
a. the name attribute to the name that has been passed in.
b. balance to 0.0
c. The accountNumber to the value of nextAccountNumber and then increment
nextAccountNumber by 1.
3. Create a getter (but not a setter) for the account number attribute.
4. Create another class, and in a main method make two bank account objects. Print
their account numbers. Are they what you expect? Can you explain why it¡¯s
happening? (hint: it¡¯s because the nextAccountNumber attribute is static)
5. Add methods to your class that allow other things to access the balance, deposit
funds and withdraw funds. In the latter two cases, don¡¯t forget to update the balance.
Also add a toString method that displays the name, account number, and balance.
6. Test the methods from 5 in your main method.
We will now see how to use your Person object and BankAccount objects together.
7. In your main method, create a Person (using the Person class you made in the previous section).
8. Modify your BankAccount object so that instead of storing a customer name, it stores a Person object instead (pass the object reference to the constructor). Some hints:
a. You¡¯ll need to change the type of the class attribute (variable) in BankAccount (the one that was name)
b. You¡¯ll need to change the arguments to the constructor
c. You¡¯ll need to change the toString method for BankAccount
9. Demonstrate the use of Person in BankAccount by creating two bank accounts for
the same person.
In the following, you will see how BankAccount objects can manipulate other BankAccount objects.
10. Create a new method called transferFunds in the BankAccount class. This method should take two arguments: an amount to transfer from the account, and another BankAccount object into which the funds should be transferred. Fill in the method with the necessary operations to withdraw the funds from one account, and deposit them in another.
3 Inheritance with People
1. Make a new class (Student) that inherits from Person. Add an additional attribute that will store the Student¡¯s GPA (a double), with associated getter and setter methods.
2. In Student, change one of the methods (maybe toString?) so that when the method is called it does something different for Student than it originally did for Person.
3. [optional] Make a second new class (Lecturer) that also inherits from Person. Add a salary attribute (private, with getters and setters).
4 Inheritance with BankAccount
The bank you are working for decides to bring in a new savings account. The difference is that savings account get paid interest and cannot go overdrawn (i.e. their balance cannot go below 0.0).
1. Create a new class that inherits from bank account.
2. Modify the withdraw method so that it doesn¡¯t allow withdrawals that will take the
balance below 0.0.
3. Add a new attribute of type double called interestRate that is set by the
constructor (check back to the slides for RecyclingBin about how you also invoke
the super constructor).
4. Add a new method that adds interest to the balance. Interest rates are typically
expressed as a percentage, so to compute the amount to be added, multiply the
balance by interestRate/100.0. E.g. if interestRate = 10.0, multiply by 0.1. Then add this value to balance.
Polymorphism
These exercises are not part of the examined material in this course, but if you¡¯re interested…(we¡¯ll do a lot of this in AP):
1. The savings account object you created in the previous exercise inherits from the bank account object. Try the following line:
BankAccount b = new SavingsAccount(
Where you should switch BankAccount and SavingsAccount for the names of your classes. What happens?
2. This is an example of polymorphism. Try it the other way around – it won¡¯t work. Objects can be referred to by references of their own type, or something higher up the hierarchy.
3. With your savings account being referred to by a bank account reference, try calling the method you wrote for computing interest. It won¡¯t work – you can only call the methods available to the class of the reference.
4. Change one of the methods in the savings account so that it is different to its counterpart in the bank account class (toString is a good candidate). Call this method from the bank account reference. Do you get the version of the method defined in bank account, or the one in savings account?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com