2009:January:16(Fri)

CS 163-01 Exercise-Set #1

Due Friday, January 23, at Lecture


    You can do the work for all of these exercises in teams of (at most) two.


  1. [8 points]   Suppose the variable r is defined as follows:
    JCheckBoxMenuItem x = new JCheckBoxMenuItem("Enable/disable something");
    Then give the value of the condition inside the parentheses of each of the following "if( )"s, true or false. Check Java documentation for the inheritance patterns.
    1. if ( x instanceof JCheckBoxMenuItem )   . . .
    2. if ( x instanceof MenuElement )   . . .
    3. if ( x instanceof JTextField )   . . .
    4. if ( x instanceof AbstractButton )   . . .
    5. if ( x instanceof JComponent )   . . .
    6. if ( x instanceof JMenu )   . . .
    7. if ( x instanceof Container )   . . .
    8. if ( x instanceof Object )   . . .


  2. Consider the following Java application program and the demonstration of its use and output:
    class base {
        private int  m1() { return 30; }
        public int  m2() { return 40; }
        public int  m3() { return  m1() + m2(); }
        }
    
    class derived extends base {
        public int  m2() { return 50; }
        }
    
    public class bd_demo {
        public static void main(String[] args) {
            base b = new base();
            derived d = new derived();
            System.out.println("b.m1(): " + b.m1());
            System.out.println("d.m1(): " + d.m1());
            System.out.println("b.m2(): " + b.m2());
            System.out.println("d.m2(): " + d.m2());
            System.out.println("b.m3(): " + b.m3());
            System.out.println("d.m3(): " + d.m3());
            b = d;
            System.out.println("after b=d, b.m1(): " + b.m1());
            System.out.println("after b=d, b.m2(): " + b.m2());
            System.out.println("after b=d, b.m3(): " + b.m3());
            }
        }
    
    
    > javac bd_demo.java
    > java bd_demo
    b.m1(): 30
    d.m1(): 30
    b.m2(): 40
    d.m2(): 50
    b.m3(): 70
    d.m3(): 80
    after b=d, b.m1(): 30
    after b=d, b.m2(): 50
    after b=d, b.m3(): 80
    > 
    
    The following terms describe relevant concepts:
    1. dynamic dispatch
    2. inheritance
    3. composition
    4. information hiding
    5. encapsulation
    6. polymorphism
    7. overriding
    8. static binding
    1.   [1 point]   Which of the above-listed terms best describes derived object d's possession of a method m1()?
    2.   [1 point]   Which of the above-listed terms best describes the capability of class derived to `extend' class base yet have its own version of method m2()?
    3.   [1 point]   Which of the above-listed terms best describes the capability of storing a derived object (d) in base variable b?
    4.   [1 point]   Which of the above-listed terms best describes the way the invocation b.m2() sometimes does the m2() of class base and other times does the m2() of class derived?
    5.   [1 point]   Which of the above-listed terms best describes the way the invocation of m2() inside m3() sometimes does the m2() of class base and other times does the m2() of class derived?


    1.   [12 points]  
      A bank maintains the accounts for a number of customers. The basic data describing each account consists of the account number, the customer's name, and the current balance. Write a class to implement such a bank account. The constructor method should accept as parameters the account number, name, and initial balance.
      You should provide methods to deposit an amount of money, to withdraw an amount of money, and to get the current balance. (For this exercise, you do not need to ensure that the initial balance and deposits are positive etc.) You should have encapsulation as usual.
      Among various types of account that the bank offers its customer, two are a money-market account and a `CD' (certificate of deposit) savings account. At the end of each month interest is calculated on the amount in either of these two types of account. This interest is added to the account's balance. But one difference between these two types of account is that for the CD account, the interest-rate is set when the account is created, while for the money-market account the interest-rate is specified each month. Another difference is that the CD account disallows withdrawals. Write classes to describe each of these further types of account, making maximum use of inheritance while retaining strong encapsulation (so not using protected (nor nothing) for field(s) i.e. instance-variable(s) of the basic account-class). Each of these particular account classes should provide a method that is invoked to calculate the interest; for the money-market account, this method should accept the current month's interest-rate as an argument. The CD account should override the basic account's withdrawal method, doing nothing to the balance but outputting an error-message via System.out.println().
      Acknowledgement: This exercise is derived from "Java for Students" by Douglas Bell and Mike Parr.

    2.   [7 points]   Write a Java application that in main() does the following:
      • creates a money-market account, storing it in a money-market variable. (You can choose the initial balance.)
      • creates a CD account, storing it in a CD account variable. (You can choose the initial balance and the interest-rate.)
      • invokes the interest-accruing method of each of these accounts. (You can choose the interest-rate for the money-market account.)
      • creates an array or ArrayList<> of two basic bank-accounts and stores those two accounts in it.
      • uses System.out.println() to output the balance of each element of the array or ArrayList<> of bank-accounts.
      • stores those two accounts from the array or ArrayList<> — not from the original two variables above — back in a new money-market account variable (different from the original one above) and a new CD account variable (different from the original one above). Remember to cast.
      • invokes the interest-accruing method of each of these accounts.


Copyright © 2009 by Hugh McGuire