; H1 solution
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

H1 solution

VIEWS: 1 PAGES: 3

  • pg 1
									                                             CIS 415 F09
                                     Homework #2 Solution (40 points)

1. Define inheritance. (2 points).
   Inheritance is a way to form new classes using classes that have already been defined. The new
   classes take over attributes and behavior of the pre-existing classes. This allows reuse of
   existing code.
2. Define polymorphism. (2)
   Best definition is your textbook. A polymorphic variable or argument can take different
   classes.
3. Define dynamic binding. (2)

   It is identifying the method a message is intended for after determining what kind object is
   referenced during run time.

You can use a code from programs in bank.zip or write your own code based on the given
programs to help answering any of the following questions.

4. Give an example of a new statement in Java that illustrates instantiation? (2)
      new CheckingAccount ca = new CheckingAccount();
5. Give an example of a new statement in Java that illustrates polymorphism? (2)

       Account a = new CheckingAccount();

6. Give an example of a method/message that illustrates inheritance? (2)

       ca.deposit(1000);

7. Give an example of a new statement that illustrates dynamic binding? (2)

       none

8. Give an example of a method/message that illustrates dynamic binding? (2)
   Account a = new CheckingAccount();

   a.toString();

9. Can you have inheritance without polymorphism? – Yes(give example) /No (3)

   Yes

   savingsAccount2.deposit(50.00);   // deposit resides in parent but savings account does not morph

10. Can you have polymorphism without dynamic binding? – Yes(give example) /No (3)

   Yes

   Account a = new CheckingAccount();
                                                                                                       1
11. Can you have dynamic binding without polymorphism? – Yes(give example) /No (3)

   No

12. Can you have polymorphism without inheritance? – Yes(give example) /No (3)

   No

13. Does the Account class (Account.java) have to be declared as abstract? Yes/No, and why? (3)

   Yes, because it contains an abstract method interest().

14. Write code to call the static method getCount() from the outside of Account class.(2)

   int n = Account.getCount();

15. Which method(s) must be implemented in SavingAccount and Checking class? (2)

   interest()

16. toString() method is defined in both Account and CheckingAccount, what do we call this
    phenomenon? (2)

   override

17. Change the given Account class to an interface and name it AccountBehavior, and paste your
    AccountBehavior.java below? (3 points)

   public interface AccountBehavior
   {
        public abstract void interest();
        public abstract void deposit(double amount);
        public abstract double withdraw(double amount);
        public abstract double getBalance();
        public abstract String toString();
   }




                                                                                                  2
18. In order to execute the following code,

   CheckingAccount ca1 = new CheckingAccount();
   CheckingAccount ca2 = new CheckingAccount(100.00);

   Which constructor(s) defined in CheckingAccount class must be kept and which is(are) optional
   (after being eliminated, the given code still works)? And why?(4)
   mondatory
         public CheckingAccount(double balance)
         {
              super(balance);
        }

   optional
      public CheckingAccount()
      {
             super();
      }




                                                                                               3

								
To top