Book 4 Abstract Classes and Interface Exercises

4.1 Developing an employee class

Background

In this exercise we want you to take an abstract class which we have defined for you and develop two classes. The abstract class represents the basic building block for employees in a personnel database. The code is shown below:

abstract class Employee {
private String name, address;
protected int basicSalary;

public String getName(){
return name;
}

public String getAddress(){
return address;
}

public int getBasicSalary(){
return basicSalary;
}

public void setAddress(String add){
address = add;
}

public void setName(String nm){
name = nm;
}

public void setBasicSalary(int  sal){
basicSalary = sal;
}
public  abstract int getMonthlySalary();

}

The class contains three instance variables which hold the name, address and basic yearly salary of an employee.

Aim of this exercise

The aim of this exercise is to give you practice in generating concrete classes from an abstract class.

Difficulty

This exercise is of difficulty level 2.

If you have difficulty with this exercise have a look at our hint.

Procedure

There are a number of steps to this exercise:

What you should have learned

You should have learned how to extend an abstract class and develop two concrete classes which inherit from this class.

Once you have completed this exercise have a look at our solution.

4.2 Using interfaces in a collection class

Background

In this exercise we require you to develop an interface and then use it in a class which describes a collection of integers.

Aim of this exercise

The aim of this exercise is to give you practice in developing an interface and then using inheritance to employ this interface.

Difficulty

This exercise is of difficulty level 3.

If you have difficulty with this exercise have a look at our hint.

Procedure

There are a number of steps to this exercise:

public class IntCollection implements Operations{ 
private int size, noOfItems; 
private int[] intArray;

public IntCollection(int sz){
// Code for single argument cosntructor
}

public IntCollection(){
// Code for no-argument constructor, assume 100
// items
}

public int getNoOfItems(){
// Code for getNoOfItems
}

public boolean isIn(int val){
// Code for isIn
}

public void insert(int val){
// Code for insert
}

}



There is no need to program a very complicated search; a simple linear one would do.

public static void main (String[] args){

IntCollection in = new IntCollection(50);
in.insert(99);
in.insert(1);
in.insert(2);
in.insert(9);
in.insert(44);
in.insert(200);
if(in.isIn(44)) System.out.println("Ok 1");
if(in.isIn(99))
System.out.println("Ok 2");
if(in.isIn(200)) System.out.println("Ok 3");
if(in.isIn(9)) System.out.println("Ok 4");
if(!in.isIn(33))System.out.println("Ok 5");
if(!in.isIn(441))System.out.println("Ok 6");
 }



What you should have learned

You should have learned to write an interface and use it within a class.

It is worth making an important point about this exercise. Some of you may be wondering what the point of an interface in this exercise is. For example, the same effect could have been achieved by inheriting from the class IntCollection and adding the two methods insert and isIn to this new sub-class. Certainly, the same effect would have been achieved; however, if we were in a software project and we had asked programmers to implement a number of sub-classes with these methods in, we would have had to carry out extensive quality assurance procedures, such as technical reviews, which checked that they had done what was expected. When we use the keyword implements we tell the programmer what is expected: that methods from the interface are to be coded and, moreover, if the programmer does not carry out this instruction a Java error is created by the compiler. In this context the compiler enforces a quality check which would have been expensive otherwise.

Once you have completed this exercise have a look at our solution.