A class is often used to define a set of data called a record. Arrays can hold only one type of data. A good example of a set of records is the phone book, for each entry there is a name, address and phone number. Each set of name,address and phone number is a record, each part of a record, for example the name, is called a field.
In Java a class can used to make records of data items that have different record types. For example a set of data could be:
Employee Name (stored as String)
Employee Number (stored as int) and
Hourly Wage (stored as float)
The following class is an example of a record. The class stores six data members, There are get methods for all six but only set methods for four as regular pay and overtime pay can only be changed by using the calcPay() method. This is an example of data hiding. Read the class and make sure you understand the function of each method.
// The "HourlyEmployee" class by R. Parteno (April 2002)
// This class is an employee pay record for hourly employees.
// The calcPay() method calculates regular pay and overtime pay
import java.io.*;
public class HourlyEmployee {
private String name;
private long empNum;
private double hourlyRate;
private double hoursWorked;
private double regularPay;
private double overtimePay;
private final long recLen = 80;
// constructors - first with parameters and second without)
public HourlyEmployee (String name, long empNum, double hourlyRate, double hoursWorked) {
this.name = name;
this.empNum = empNum;
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
calcPay ();
}
public HourlyEmployee () {
name = "";
empNum = 0;
hourlyRate = 0;
hoursWorked = 0;
}
public void setName (String name) {name = name; }
public void setEmpNum (long empNum) { empNum = empNum; }
public void setHourlyRate (double hourlyRate) {hourlyRate = hourlyRate; }
public void setHoursWorked (double hoursWorked) {hoursWorked = hoursWorked; }
public void calcPay () {
if (hoursWorked > 40) {
regularPay = 40 * hourlyRate;
overtimePay = (hoursWorked - 40) * hourlyRate * 1.5;
}
else {
regularPay = hoursWorked * hourlyRate;
overtimePay = 0;
}
}
public String getName () { return name; }
public long getEmpNum () { return empNum; }
public double getHourlyRate () { return hourlyRate; }
public double getHoursWorked () { return hoursWorked; }
public double getRegularPay () { return regularPay; }
public double getOvertimePay () { return overtimePay; }
The HourlyEmployee class can now be used in any other class.
// The "UseHourlyEmployeeSample1" class.
public class UseHourlyEmployeeSample1 {
public static void main (String[] args) {
HourlyEmployee hrEmp=new HourlyEmployee("Rinaldi",123,10.50,45);
System.out.println("The regular pay for "+hrEmp.getName()+" is "+hrEmp.getRegularPay());
System.out.println("The overtime pay for "+hrEmp.getName()+" is "+hrEmp.getOvertimePay());
} // main method
} // UseHourlyEmployeeSample1 class
It is possible to have an array of any object. The following class demonstrates the use of an array of records.
// The "UseHourlyEmployeeSample2" class.
public class UseHourlyEmployeeSample2 {
public static void main (String [] args) {
HourlyEmployee hrEmp [] = new HourlyEmployee [3];
for (int ctr = 0 ; ctr < 3 ; ctr++) {
hrEmp [ctr] = new HourlyEmployee ();
System.out.print ("Enter an employee name: ");
hrEmp [ctr].setName (ReadLib.readString ());
System.out.print ("Enter an employee number: ");
hrEmp [ctr].setEmpNum (ReadLib.readLong());
System.out.print ("Enter hours worked: ");
hrEmp [ctr].setHoursWorked (ReadLib.readDouble ());
System.out.print ("Enter hourly rate: ");
hrEmp [ctr].setHourlyRate (ReadLib.readDouble ());
hrEmp[ctr].calcPay();
System.out.println ("The regular pay for " + hrEmp [ctr].getName () + " is " + hrEmp [ctr].getRegularPay ());
System.out.println ("The overtime pay for " + hrEmp [ctr].getName () + " is " + hrEmp [ctr].getOvertimePay ());
}
} // main method
} // UseHourlyEmployeeSample2 class