blues
02-21-2005, 08:20 PM
Greetings.
I am trying to follow examples 10.12-10.17 in the fifth edition of Deitel's How to Program Java.
It starts out with the following code of using an abstract class. I have a basic understanding of what is happening.
public abstract class Employee
{
private String myFirstName;
private String myLastName;
private String mySocialSecurityNumber;
// constructor
public Employee( String first, String last, String ssn )
{
myFirstName = first;
myLastName = last;
mySocialSecurityNumber = ssn;
}
// set first name
public void setFirstName( String first )
{
myFirstName = first;
}
// return first name
public String getFirstName()
{
return myFirstName;
}
// set last name
public void setLastName( String last )
{
myLastName = last;
}
// return last name
public String getLastName()
{
return myLastName;
}
// set social security number
public void setSocialSecurityNumber( String number )
{
mySocialSecurityNumber = number; // should validate
}
// return social security number
public String getSocialSecurityNumber()
{
return mySocialSecurityNumber;
}
// return String representation of Employee object
public String toString()
{
return getFirstName() + " " + getLastName() + "\nSocial security number: " + getSocialSecurityNumber();
}
// abstract method overridden by subclasses
public abstract double earnings();
} // end abstract class Employee
Now the next section of code gets or retreives those classes (well, the only one I typed above).
// fig. 10.17: payroll system test
// employee hierarchy test program.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class PayrollSystemTest {
public static void main( String[] args )
{
DecimalFormat twoDigits = new DecimalFormat( "0.00 );
// create Employee array
Employee employees[] = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-1111", 800.00 );
// initialize array with Employees
employees[ 0 ] = new CommissionEmployee( "Sue", "Jones",
"222-22-2222", 10000, .06 );
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-111", 800.00 );
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-111", 800.00 );
String output = ""
// generically process each element in array employees
for ( int i = 0; i < employees.length; i++ ) {
output+= employees[ i ].toString();
// determine wheter element is a BasePlusCommissionEmployee
if ( employees[ i ] instanceof BasePlusCommissionEmployee ) {
// downcast Employee reference to
// BasePlusComissionEmployee reference
BasePlusComissionEmployee currentEmployee =
( BasePlusCOmissionEmployee ) employees[ i ];
double oldBaseSalary = currentEmployee.getBaeSalary();
output += "\nold base salary: $" + oldBaseSalary;
currentEmployee.setBaseSalary( 1.10 * oldBaseSalary;
output += "\nnew base salary with 10% increase is : $" +
currentEmployee.getaseSalary();
} // end if
output += "\nearned $" + employees[ i ].earnings() + "\n";
} // end for
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++ )
output += "\nEmployee " + j + " is a " +
employees[ j ].getClass().getName();
JOptionPane.showMessageDialog( null, output ); // display output
System.exit(0);
} // end main
} // end class PayrollSystemTest
My question is that how do you connect separate classes together to perform several functions inside a program?
Is it like writing a library package and then importing it?
I am trying to follow examples 10.12-10.17 in the fifth edition of Deitel's How to Program Java.
It starts out with the following code of using an abstract class. I have a basic understanding of what is happening.
public abstract class Employee
{
private String myFirstName;
private String myLastName;
private String mySocialSecurityNumber;
// constructor
public Employee( String first, String last, String ssn )
{
myFirstName = first;
myLastName = last;
mySocialSecurityNumber = ssn;
}
// set first name
public void setFirstName( String first )
{
myFirstName = first;
}
// return first name
public String getFirstName()
{
return myFirstName;
}
// set last name
public void setLastName( String last )
{
myLastName = last;
}
// return last name
public String getLastName()
{
return myLastName;
}
// set social security number
public void setSocialSecurityNumber( String number )
{
mySocialSecurityNumber = number; // should validate
}
// return social security number
public String getSocialSecurityNumber()
{
return mySocialSecurityNumber;
}
// return String representation of Employee object
public String toString()
{
return getFirstName() + " " + getLastName() + "\nSocial security number: " + getSocialSecurityNumber();
}
// abstract method overridden by subclasses
public abstract double earnings();
} // end abstract class Employee
Now the next section of code gets or retreives those classes (well, the only one I typed above).
// fig. 10.17: payroll system test
// employee hierarchy test program.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class PayrollSystemTest {
public static void main( String[] args )
{
DecimalFormat twoDigits = new DecimalFormat( "0.00 );
// create Employee array
Employee employees[] = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-1111", 800.00 );
// initialize array with Employees
employees[ 0 ] = new CommissionEmployee( "Sue", "Jones",
"222-22-2222", 10000, .06 );
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-111", 800.00 );
// initialize array with Employees
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-111", 800.00 );
String output = ""
// generically process each element in array employees
for ( int i = 0; i < employees.length; i++ ) {
output+= employees[ i ].toString();
// determine wheter element is a BasePlusCommissionEmployee
if ( employees[ i ] instanceof BasePlusCommissionEmployee ) {
// downcast Employee reference to
// BasePlusComissionEmployee reference
BasePlusComissionEmployee currentEmployee =
( BasePlusCOmissionEmployee ) employees[ i ];
double oldBaseSalary = currentEmployee.getBaeSalary();
output += "\nold base salary: $" + oldBaseSalary;
currentEmployee.setBaseSalary( 1.10 * oldBaseSalary;
output += "\nnew base salary with 10% increase is : $" +
currentEmployee.getaseSalary();
} // end if
output += "\nearned $" + employees[ i ].earnings() + "\n";
} // end for
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++ )
output += "\nEmployee " + j + " is a " +
employees[ j ].getClass().getName();
JOptionPane.showMessageDialog( null, output ); // display output
System.exit(0);
} // end main
} // end class PayrollSystemTest
My question is that how do you connect separate classes together to perform several functions inside a program?
Is it like writing a library package and then importing it?