Class Diagrams • In UML class diagrams are used to represent classes and relationships between classes. • Each class is represented as a box divided into three parts specifying the: • Name of the class • Data elements of the class • Methods of the class
The student class Class name Data elements Methods
Associations • Associations represent interactions between objects • Examples: • A student registers for a course. • A customer places an order. • An employee has a particular medical scheme.
Example • A customer buys a product
Another Example • A student registers for a programme
Example 1 • Define the classes that you would use for the following system. Specify the data elements and methods of each class. You need to develop a system that keeps track of customers accounts. Information stored for each customer is the customer name, address and phone number. A customer may place one or more orders. Each order has an order number, date and amount and a description of the product to be shipped. Each product has a unique product number, description and price.
Example 2 • Define the classes that you would use for the following system. Specify the data elements and methods of each class. You are required to develop a GUI for an existing application. The main component of the GUI is a form. The form has a width, height, boarder style and background color. The user must be able to display or close a form. The user must also be able to minimize and maximize the form. The form contains labels and buttons. Labels have a size, shape, color and text. The programmer must be able to set the text, get the text, hide the label or display the label. Each button has a size, shape, colour, location and caption. The user should be able to click the button. A programmer must be able to hide, display, enable or disable the button.
Superclasses and Subclasses • Some classes can be defined as subclasses of other classes which we call superclasses. • Examples • Superclass: Person Subclass: Customer • Superclass: Birds Subclass: Penguins • A subclass inherits all the data elements and methods of its superclass. • In addition to these a subclass usually has data elements and methods of its own. • The relationship between subclasses and superclasses is referred to as inheritance. • A subclass is said to extend the superclass.
Inheritance Examples • Superclass: Student • Subclasses: GraduateStudent, UndergraduateStudent • Superclass: Shape • Subclasses: Circle, Triangle, Rectangle • Superclass Loan • Subclasses: CarLoan, HomeImprovementLoan, MortgageLoan
Example Information stored for each student in the database includes the student’s name, student number, address, matric points and the degree the student is registered for. The student’s majors and year of study is stored for each undergraduate student. For each postgraduate student the student’s subject area and supervisor are stored.
Exercise Draw a class diagram for the following system: Suppose that you are required to implement a system that stores information whenever a customer makes a purchase. The information that must be stored includes the customer ID number, name, address, telephone number and amount due for the purchase. If a customer pays cash for the goods purchased, then he or she receives a discount for the particular purchase. Thus, in this case the amount_due is calculated to be the cost of the goods minus a percentage (discount_percent) of the cost. For example, if the cost of the goods purchased is R100 and the discount_percent is 0.10 then the amount_due is R90. If the customer purchases goods on credit the amount due is the cost of the goods plus a percentage (add_on_percent) of the cost. The monthly_instalment is this value divided by 12. For example, if the cost of the goods is R200 and the add_on_percent is 0.2 then the amount_due is 200+200*0.2= R 240 and the monthly_instalment is R20.