You are on page 1of 6

Object Oriented Programming (CMM504) Laboratory #4: Abstract Class & Interface

1. Aims

To use abstract classes as the basis of one or more inheritance hierarchies. To use Java interface to emulate multiple inheritance. To use UML class diagram to visualise a class hierarchy.

2. Outcomes In completing this exercise, you should be able to: Define abstract classes in Java. Define interfaces in Java. Understanding simple UML class diagrams that describe relationships among classes. 3. A Payroll Application In this part of the exercise, we model employees in a company and the payroll that stores all employees. 3.1. Our Domain Model Our domain model is shown in the following UML class diagram:
PayrollApp +main(:String[]) Employee #name,#job:String +Employee(:String,:String) +getJob():String +getName():String +matchName(:String):boolean +toString():String +getMonthlyWage():double

Payroll -employees:Employee[] +Payroll(:int) +add(:Employee):boolean +getWages():double

ContractEmployee SalariedEmployee -annualSalary:double +SalariedEmployee(:String, :String,:double) +toString():String +setAnnualSalary(:double) +getAnnualSalary():double +getMonthlyWage():double -hourlyRate:double -hoursWorked:int +ContractEmployee(:String, :String,:double,:int) +toString():String +setRate(:double) +getRate():double +setHoursWorked(:int) +getHoursWorked():int +getMonthlyWage():double

Questions: How many classes do we have?

Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011

Page 1/6 The Robert Gordon University

How many is/are abstract? (Note: Abstract class names are in italic.) How many is/are concrete? What is the relationship between: PayrollApp and Payroll? Payroll and Employee? Employee and SalariedEmployee? Employee and ContractEmployee? Is there any abstract method in any class? Which and where? (Note: Abstract method names are in italic.) Is there any class-level (i.e. static) method in any class? Which and where? In which class will our program execution start? Why?

3.2. Modelling an Employee Our abstract class Employee models an employee in a company (notice the use of italic in the UML class diagram). Download Employee.java from Campus Moodle and save it into your working directory. You better keep it in a different folder/directory from the Employee class in Lab#3. 2. Compile Employee.java.
1.

Questions: How many attributes does Employee have? Are they object-level or class-level? Why are they modelled this way? How many methods does Employee have? Are they modelled as object-level or class-level methods? Why? How many of these methods is/are constructor? How many of these methods are abstract? Why is Employee called an abstract class? Why do we have to model Employee as an abstract class? (In other words, why not a concrete class?) 3.3. Modelling a Salaried Employee A salaried employee is similar to an employee, except that a salaried employee is paid according to his/her annual salary. Consult the UML diagram to define a concrete class SalariedEmployee which inherits from the Employee abstract class (defined in section 3.2). Here are some extra details/ hints:

In the constructor of SalariedEmployee, you may invoke the superclass constructor by super(name,job) to fill in the inherited attributes or access them directly (as they are protected).
Page 2/6 The Robert Gordon University

Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011

The monthly wage of a SalariedEmployee is calculated by dividing his/her annual salary by 12.0. In the toString() method, you may use super.toString() to invoke the superclass toString() method and append the extra attribute values. Alternatively, you can access the protected attributes directly or use the inherited accessor methods getName() and getJob().

Questions: According to the UML diagram, which class is the superclass of SalariedEmployee? Excluding the inherited attributes, how many extra attribute(s) does SalariedEmployee have? Is it (or Are they) modelled as private/protected/public attribute(s)? Why? Excluding the inherited methods, how many extra methods does SalariedEmployee have? How many of these are constructor? For each of these methods, do you think it should be modelled as an object-level or class-level method? Why? SalariedEmployee defines the body of some abstract methods. What are they? Why do we model SalariedEmployee as a concrete class? Why not an abstract class? 3.4. Modelling a Contract Employee A contract employee is similar to an employee, except that a contract employee is paid according to his/her hourly rate and hours worked. Consult the UML diagram to define a concrete class ContractEmployee which inherits from abstract class Employee. Here are some extra details/ hints:

In the constructor of ContractEmployee, you may use super(name,job) to invoke the constructor of your superclass to fill in the name and job attribute values. Alternatively, you can access these protected attributes directly. In the toString() method, override the toString() method in Employee so that it returns a String including the name, job, hourly rate and hours worked of the ContractEmployee object. The monthly wage of a ContractEmployee is calculated by multiplying his/her hourly rate by the hours worked.

Questions: How many extra attributes does ContractEmployee have? Are they private/protected/public? Why? How any of these attributes are inherited from the superclass? How many methods are there in ContractEmployee?
Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011 Page 3/6 The Robert Gordon University

How many of them are constructor? How many abstract methods does ContractEmployee have? How many abstract methods in Employee are defined in ContractEmployee? How many methods in Employee are overridden in ContractEmployee? Why do we model ContractEmployee as an concrete class instead of an abstract class?

3.5. Modelling the Payroll A payroll relates to a number of employees (salaried/ contracted).

Download the Payroll.java file from Campus Moodle and save it into your working space.

Questions: How many attribute(s) does Payroll have? How many method(s) does Payroll have? What operation each method model? What input parameter does each method take? Does the method return any value? Why? How does Payroll relate to Employee? (e.g. Is it a one-toone/one-to-many/many-to-many relationship?) Why is the relationship modelled this way? From the view point of the Payroll: Does it matter if an employee is a SalariedEmployee or ContractEmployee object? Why? How is this possible in term of programming? 3.6. Our Payroll Application Our payroll application uses all classes defined above to create a Payroll object which relates to a few Employee objects. In a new file PayrollApp.java, develop a new PayrollApp class that uses and tests all the above-developed classes. The structure of your PayrollApp may look like this:
public class PayrollApp { public static void main(String argv[]) { Payroll p=new Payroll(10); //create a payroll of 10 spaces // //create some employees and store them into payroll // p.add(new SalariedEmployee("John Smith","carpenter",12000.0)); p.add(new ContractEmployee("Teddy Blair","plumber",3.5,5)); // //calculate and print total monthly wage // System.out.println("Total monthly wage:"+p.getWages()); } //end method main } //end class PayrollApp

Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011

Page 4/6 The Robert Gordon University

Compile and test your payroll application. Create some extra employees with the following details and check your result: Gordon Black works as an electrician for 5 an hours. He worked 50 hours in the month. David Campbell is a manager with an annual salary of 36000.

4. Our Domain Model of Shapes In this exercise, we model 2D shapes which inherit properties from two different ancestors. As Java only supports single inheritance (where a subclass can only have one superclass), we have to use an interface to emulate multiple inheritance. Our domain model is shown in the following UML class diagram:
ShapeApp +main(:String[]) <<interface>> Shape2D +perimeter():double +area():double <<interface>> java.lang.Comparable +compareTo(:Object):boolean

Rectangle -width,height:double +Rectangle(:double,:double) +perimeter():double +area():double +compareTo(:Object):boolean

Circle -radius:double +Circle(:double) +perimeter():double +area():double +compareTo(:Object):boolean

Questions: What does the dotted line with hollow triangle mean in the UML diagram? How many classes/interfaces are there in our design? How many are defined in the standard Java library? How many are user-defined? 4.1. The Shape2D Interface Our Shape2D interface defines methods that an object has to fulfill without implementing the actual methods.

Download Shape2D.java from Campus Moodle and save it into your working space.

Questions: How many attribute does Shape2D have? How many method does Shape2D define? How many of these methods are abstract? Why is Shape2D modelled as an interface?
Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011 Page 5/6 The Robert Gordon University

Can it be modelled as an abstract class? Can it be modelled as a concrete class?

4.2. The Circle Class In our model, we want our Circle class to implement our Shape2D interface (so that it supports the perimeter() and area() methods) and also the java.lang.Comparable interface (so that it supports the compareTo() method).

Download the incomplete Circle.java class from Campus Moodle. Complete the implementation of the Circle class.

Here are some details that you may need: Your Circle class must implement all abstract methods declared in the Shaped2D interface. Your Circle class must also implement all abstract methods declared in the java.lang.Comparable interface. i.e. the int compareTo(Object o) method. compareTo()returns +1 if the area difference is positive, 0 when the areas are the same, -1 when negative. 4.3. The Rectangle Class Define a Rectangle class which implements the Shape2D interface and the java.lang.Comparable interface. Here are some details that you may want to know: Similar to Circle, your Rectangle class should implement all abstract methods declared in both Shape2D and java.lang.Comparable interface. The area of a rectangle is calculated by multiplying its width by its height. The perimeter of a rectangle is 2*(width+height). 4.4. The ShapeApp Class The following ShapeApp class uses the above-developed interfaces and classes. Compile and execute. Check the result.
public class TestShape2D { public static void main(String argv[]) { Shape2D c,r; c=new Circle(10.0); r=new Rectangle(5.0,6.0); System.out.println("circle:"); System.out.println("perimeter:"+c.perimeter()+" area:"+c.area()); System.out.println("rectangle:"); System.out.println("perimeter:"+r.perimeter()+" area:"+r.area()); System.out.println("rectangle compared to circle:"+((Rectangle)r).compareTo(c)); }//end main } //end class TestShape2D

Object Oriented Programming (CMM504) Laboratory #4 K. Hui 2010-2011

Page 6/6 The Robert Gordon University

You might also like