Lab 2

Interface and Implementation

We’ve reviewed two key concepts of OO: abstraction and encapsulation. You’ve learned how to use a Java interface and how to write a Java (concrete) class to provide a specific implementation. This lab will give you some hands-on exercises based on these concepts.

The following is a list of goals to be achieved

-  Be able to correctly use interfaces and implementing classes in declaring and assigning values to variables.

-  Be familiar with Java syntax for specifying and implementing an interface.

-  Understand the UML notations for interface and implementation.

-  Know how to write an inner class when needed.

1.  UML notations for interface and implementation

Consider the Money class as described in class. This class implements the Comparable interface, so that it needs to provide a compareTo method. Other members of the class are shown in the code stub below:

public class Money implements Comparable {

private double value;

public Money(double value) {

...

}

public Money add(Money amount) {

...

}

public String toString() {

return "$" + value;

}

public int compareTo(Object obj) {

...

}

}

Draw appropriate UML notations to specify this design.

2.  Code and test the Money class

Create a new (lab2) project and add the BankAccount class into your project. Then add the Money class and complete the missing code. Test your code with the BankAccountTest class. [Modify the test class to add an account with your name.]

3.  Implementing an interface using an inner class

Uncomment the code segments that are currently commented in the BankAccountTest class. Test it again and explain how an inner class is used.

4.  Implementing the authenticate method

Complete the authenticate method such that a dialog box will be popped up to prompt the user for a password. The method will return true only when correct a password is provided. Modify the test class to test this function.

5.  Using interfaces and their implementing classes

Work on exercise R9.3 in your text. [Optionally, you may write code to test your answers.]

Deliverables:

Submit your diagram, code, and answers for by Tuesday, Sept 13.

Also, maintain your course portfolio and be prepared for a code review towards the end of the semester.