LAB06-Department of Computer Science
LAB06-Department of Computer Science
Objectives:
1. Oveloading
Overloading means having more than one constructor in a class or having more than one method with the same
name in a class. In the case of constructor, the purpose of overloading is to allow the user to have as man
y
options as possible when creating an object of the class,thus making the class more flexible to use.
In the case of methods, overloading alows the same name to be used for methods that performs similar tasks –
Imagine having two plus operators, one for integer addition and another for double addition!
The condition for overloading is that the overloaded methods and/or constructors must have different
signatures. The signature of a constructor is determined b y the number, the type and the order of its
parameters.
Example 1: The following example implements the Emplo yee class. Notice how the constructors are
overloaded. Also notice the overloading of the deductions methods.
public class Employee1 {
private int iDNumber;
private String name;
private double salary;
The following shows how the constructors and the overloaded methods in the above example ma
y be used:
import java.util.Scanner;
public class TestEmployee1 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
String name;
double salary;
//if we do not know the salary, we can use one of the following constructors
Employee1 emp2 = new Employee1(number, name);
//or Employee1 emp2 = new Employee1(name, number);
emp2.setSalary(emp1.getSalary());
emp1.deductions(50);
emp2.deductions(60, 40);
emp1.printDetails();
emp2.printDetails();
}
}
To test the above class, you need to run the fileTestEmployee2.java which is the same asTestEmployee1.java
except of the creation of objects of typeEmployee2 class instead of objects of classEmployee1
Example 3: The file Employee3.java is the same as Employee2.java except for the replacement of the
printDetails() method with toString() method.
public String toString() {
return "\nID Number: "+iDNumber+"\nName: "+name+"\nSalary:"+salary;
}
The file TestEmployee3.java accordingly modifiesTestEmplyee2.java to print the two employee objects by
implicitly calling thetoString() method as follows.
. . . .
System.out.println(emp1);
System.out.println(emp2);
. . .
4. Assignment.
(i) Three private instance variables: name (String), email (String), and gender
(char of either 'm' or 'f').
(ii) One constructor to initialize name, email and gender with given values.
(iii) Public getters and setters: getName(), getEmail(), setEmail(), and
getGender().
There are no setters for name and gender, as these attributes cannot be
changed.
(iv) A toString() method that returns "author-name (gender) at email", eg, "Aliyu
Garba (m) at [email protected]".
(b) Write a test program called TestAuthor to test the constructor and the public
methods.
Try changing the email of an author.
a) add methods getLength(), getWidth() and getHeight() that returns the length,
width and height of the box object.
b) add another constructor which receives only the length. It then calls the other
constructor supplying this length for all of length, width and height (i.e. it forms
a cube).
c) add another constructor that receives another Box object as parameter and
then uses its length, width and height to initialize the current box object (i.e it
creates a box object with same dimension as the one it receives as parameter)
d) add a toString() method that returns the langth, width, and height as a string
that prints on one line as shown in the figure below.
The class BoxDemo.java should be modified to create three Box objects using each of
the three constructors and then prints it (Note: no need to read input). Use the first
box object to create the second.