11.7 Problems

 < Day Day Up > 



11.7 Problems

  1. You are to design account objects for a bank application. The accounts in this bank contain a name (string) and balance data (float). The two types of accounts are checking and savings. A checking account is an account that also has a current check number (int), which is the number of the next check to be written. The savings account is an account that has a data field for the account and an interest rate used to calculate the amount of interest to add to the account each month. (Note that only data is given here, not the methods that will be needed for the accounts.)

    • For the checking and saving accounts, what type of design should be used? Are both accounts obviously "is-a" relationships, are both "has-a" relationships, is one an "is-a" relationship and one a "has-a" relationship, or can both be designed using either "is-a" or "has-a"? Justify your answer.

    • Implement the solution to this problem using both classification and composition. Give advantages and disadvantages for both solutions.

  2. Program11.3 and Program11.4 gave two different designs for a social club. Discuss these two designs. Is one superior to the other? Why? What types of problems would you expect from working with each of these designs?

  3. Program11.2 and Program11.5 showed two designs for employee objects, one using composition and one using classification. Change the definition of the Employee class by adding a vector of dependents to the class. The dependents should be objects of type Person, so write addDependent(Person p) and removeDependent(Person p) methods to add and remove dependents from the Employee object. Implement these methods for both the classification and composition solutions. For this problem, an employee can also be a dependent (for example, a husband and wife both work for the company). As an example, the following pseudo-code should work in your program. Discuss the advantages and disadvantages of each approach.

     Employee Cindy = new SalariedEmployee("Cindy," 2073.00f); Employee Roger = new HourlyEmployee("Roger," 12.23f, 40.00f); Cindy.addDependent(Roger); Roger.addDependent(Cindy); 

  4. The new Employee class (with dependents added) is to be used in a human resources (HR) system. The employee records are to be added to a hash table to be tracked by employee number. For example, Cindy's employee number is 123 and Roger's is 456. As part of the system, the HR department wants to know the family income paid to all employee families. This is done by the following pseudo-code:

     Enumeration e = employees.elements(); while(e.hasMoreElements) {   Employee emp = (Employee)e.nextElement();   totalPay = emp.calculatePay();   Enumeration e1 = emp.dependents.elements();   while(e1.hasMoreElements) {     Object o = e1.nextElement();     if (o instanceof Employee)      totalPay = totalPay + ((Employee)o).calculatePay();   } } 

    Assume that the history of Cindy and Roger objects is as follows:

     Employee cindy = new SalariedEmployee("Cindy," 2073.00f); employees.add("123," cindy); Employee roger = new HourlyEmployee("Roger," 12.23f, 40.00f); employees.add("456," roger); cindy.addDependent(roger); roger.addDependent(cindy); roger.changeToSalaried(1073.00f); 

    Implement two programs that store the employees in a hash table, one using the employee objects with classification, one using composition. Use the pseudo-code for Cindy and Roger above. Show that you have written a correct solution to this program by calculating the family income before and after Roger's pay status changes. Which solution was easiest to implement and verify?

  5. Are Exhibits 13 and 14 (Problem 5a: Java and Problem 5b: Java) both correct? If they are different, why are they different?

    Exhibit 13: Problem 5a: Java

    start example

     public class Problem5a implements Runnable {   SwapObject swapObject;   public Problem5a(SwapObject swapObject) {     this.swapObject = swapObject;   }   public void run() {     swapObject.swap();     System.out.println(swapObject.toString());   }   public static void main(String args[]) {     SwapObject swapObject = new SwapObject(5, 7);     (new Thread(new Problem5a(swapObject))).start();     try {       Thread.sleep((int)(Math.random() * 100));     } catch (InterruptedException e) {     }     swapObject.temp = 22;   } } class SwapObject {   int val1, val2, temp;   public SwapObject(int val1, int val2) {     this.val1 = val1;     this.val2 = val2;   }   public void swap() {     temp = val1;     try {       Thread.sleep((int)(Math.random() * 100));     } catch (InterruptedException e) {     }     val1 = val2;     val2 = temp;   }   public String toString() {     return ("val1 = "+ val1 + " val2 = " + val2);   } } 

    end example

    Exhibit 14: Problem5b: Java

    start example

     public class Problem5b implements Runnable {   SwapObject swapObject;   public Problem5b(SwapObject swapObject) {     this.swapObject = (SwapObject)swapObject.clone();   }   public void run() {     swapObject.swap();     System.out.println(swapObject.toString());   }   public static void main(String args[]) {     SwapObject swapObject = new SwapObject(5,7);     (new Thread(new Problem5b(swapObject))).start();     try {       Thread.sleep((int)(Math.random() * 100));     } catch (InterruptedException e) {     }     swapObject.temp = 22;   } } class SwapObject implements Clonable{   int val1, val2, temp;   public SwapObject(int val1, int val2) {     this.val1 = val1;     this.val2 = val2;   }   public Object clone() {     Object o = null;     try {       o = super.clone();     } catch (CloneNotSupportedException e) {       throw new RuntimeException("Invalid Cloning Operation");     }     return o;   }   public void swap() {     temp = val1;     try {       Thread.sleep((int)(Math.random() * 100));     } catch (InterruptedException e) {     }     val1 = val2;     val2 = temp;   }   public String toString() {     return ("val1 = " + val1 + " val2 = "+ val2);   } } 

    end example

  6. What types of problems (other than concurrent programming) can happen when association is used?

  7. Animated Swap Problem (in Section 11.4.5).

  8. The problem with using classification to design an operator node is that there was only a single instance of the operator object. In the following two programs, that restriction has been removed. Problem8.a gives the composition solution for the node, and Problem8.b gives a classification solution. For both of these, implement an OperatorNode, as in Program11.10 (Exhibits 11 and 12). Which was easier to implement? Why? Which design (if either) would force changes that could affect programs that already use Operator class objects?

  9. In this problem, you are going to model a simple bank with an ATM. The bank will have two kinds of accounts, checking and savings. These accounts both have the name of the customer, an account number, a balance in the account, and a vector of all transactions that have occurred on that account. Transactions are the things that change the account balance and will be discussed in more detail later. The savings account also has an interest rate to be used to calculate the monthly interest, and the checking account has a current check number, which is the number of the next check from that account. The designs of the methods required for each account are given as interfaces below (you do not have to use these interfaces in your program, as they are provided here to define the methods you will need). The bank will keep a database of accounts using any Java key map container object (for example, a Hashtable). The key will be the account number. When a transaction is to be processed the account is retrieved and the appropriate action applied to the object. (Note: The object does not have to be stored back into the container, but why not?) The bank will only be accessible by the interface below. This interface allows only transactions to be sent to the bank. Transactions represent actions to take on accounts. The accounts themselves should not be given any scope outside of the bank, but the bank should process transactions for each account. Transactions will contain the date and time the transaction was made and the account number. The three types of transactions are credit, debit, or balance.

A credit transaction decreases the balance of the account, and a debit increases the balance, so both the credit and debit transactions require an amount field. All three types of transactions return the balance of the account once the transaction is made. Finally, one or more ATMs send transactions to the bank. The ATM will not directly access the bank but should communicate with the bank through the Bank interface. The ATM will present a graphical user interface (GUI) to the customer that asks for account number, type of transaction, and amount. It will use this information to build the transaction and send the transaction to the bank. The bank will then pass the transaction to the account for processing, and return back to the ATM the new balance on the account. Note that each account should store all transactions that are made on that account. Design the bank in this problem. You can (and probably should) use Java objects to do so. For which objects do you use classification? For which objects do you use composition? Which objects represent aggregation vs. association? Finally, once your design is complete, implement your design.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net