Developing software using object-oriented principles require a programmer to have a sound understanding of a number of important concepts:
- Object-Oriented Analysis: what is the problem to be solved?
- Object-Oriented Design: how are we going to solve the problem
- Object-Oriented Programming: actually implementing the solution using an Object-Oriented Programming Language
One good way to learn Object-Oriented Analysis and Design is to read Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development by Craig Larman. In it, the author explores ways to use the Unified Modeling Language (UML) and Design Patterns to create object-oriented software which are resilient to change and which have a sound architecture.
First exercise: Phone-Book
The first exercise is to Write a Java program which allows the user to manipulate a phone-book. The program should allow for the creation of new name/number pairs, searches and deletions (three use-cases which belong to the use-case diagram).
The software to be written needs to find solution for each of the three use-cases, needs to be object-oriented and each class written needs to be unit tested using the JUnit framework.
The steps to follow are:
- Draw the use-case diagram
- For each use-case, draw a sequence diagram
- From the sequence diagrams, derive a class diagram
- For each class identified, implement it in Java
- Immediately test each class using JUnit
- Write a test suite
- Write the main application providing a simple text interface for the application
Second exercise: Simple E-Commerce Application
Write a Java application which allows the user to add products to a shopping cart. The total cost (taking in account shipping rates should always be displayed). The application should ideally have a graphical interface.
The different products are books and CDs (Interestingly, both have now been replaced by their e-variants: e-books and music streaming using services such as Pandora and Spotify!). Any product has a name, a price and a shipping rate (which is dependent on the type of product). A book ships for Rs 200.00 and a CD ships for Rs 100.00.
The shopping cart should allow the user to add products to it and should know how to calculate its total price (which includes the prices and shipping rates of each product it contains).
To evaluate whether the shopping cart works correctly or not, it is important to build a main application. In the previous exercise, we built a text interface. For this e-commerce application, we are going to build a simple graphical user interface for it (as shown on the left).
The key elements are a set of buttons which when clicked on add the corresponding product to the initially empty shopping cart. The total price of the shopping cart is always shown on the status line (at the bottom of the application). We are going to use the Swing library to build the graphical interface. Swing is relatively straightforward if its essential concepts are understood, namely the JFrame, the JTextField, the JButton and, very importantly its method, addActionListener).