Uploaded by Yohannes Zinaw

biblestudiesbysteve com COSC 202013 CodeSamplesCh9 figure9 h

advertisement
Figure 9.4 ComissionEmployee.java
// Fig. 9.4: CommissionEmployee4.java
// CommissionEmployee4 class represents a commission employee.
public class CommissionEmployee4
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee4 (String first, String last, String ssn,
double sales, double rate)
{
// implicit call to Object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
setGrossSales (sales); // validate and store gross sales
setCommissionRate (rate);// validate and store commission rate
System.out.printf (
"\nCommissionEmployee4 constructor:\n%s\n", this);
} // end five-argument CommissionEmployee4 constructor
// set first name
public void setFirstName (String first)
{
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName ()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName (String last)
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName ()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber (String ssn)
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber ()
{
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// set gross sales amount
public void setGrossSales (double sales)
{
grossSales = (sales < 0.0) ? 0.0:
sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales ()
{
return grossSales;
} // end method getGrossSales
// set commission rate
public void setCommissionRate (double rate)
{
commissionRate = (rate > 0.0 && rate < 1.0) ? rate:
0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate ()
{
return commissionRate;
} // end method getCommissionRate
// calculate earnings
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public double earnings ()
{
return commissionRate * grossSales;
} // end method earnings
// return String representation of CommissionEmployee object
public String toString()
{
return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate );
} // end method toString
} // end class CommissionEmployee4
Figure 9.5 CommissionEmployeeTest.java
// Fig. 9.5: CommissionEmployeeTest.java
// Testing class CommissionEmployee.
public class CommissionEmployeeTest
{
public static void main( String args[] )
{
// instantiate CommissionEmployee object
CommissionEmployee employee = new CommissionEmployee(
"Sue", "Jones", "222-22-2222", 10000, .06 );
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// get commission employee data
System.out.println(
"Employee information obtained by get methods: \n" );
System.out.printf( "%s %s\n", "First name is",
employee.getFirstName() );
System.out.printf( "%s %s\n", "Last name is",
employee.getLastName() );
System.out.printf( "%s %s\n", "Social security number is",
employee.getSocialSecurityNumber() );
System.out.printf( "%s %.2f\n", "Gross sales is",
employee.getGrossSales() );
System.out.printf( "%s %.2f\n", "Commission rate is",
employee.getCommissionRate() );
employee.setGrossSales( 500 ); // set gross sales
employee.setCommissionRate( .1 ); // set commission rate
System.out.printf( "\n%s:\n\n%s\n",
"Updated employee information obtained by toString", employee );
} // end main
} // end class CommissionEmployeeTest
Employee information obtained by get methods:
First name is Sue
Last name is Jones
Social security number is 222-22-2222
Gross sales is 10000.00
Commission rate is 0.06
Updated employee information obtained by toString:
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
commission employee: Sue Jones
social security number: 222-22-2222
gross sales: 500.00
commission rate: 0.10
Figure 9.6 BasePlusCommissionEmployee.java
// Fig. 9.6: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class represents an employee that receives
// a base salary in addition to commission.
public class BasePlusCommissionEmployee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee( String first, String last,
String ssn, double sales, double rate, double salary )
{
// implicit call to Object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
setGrossSales( sales ); // validate and store gross sales
setCommissionRate( rate ); // validate and store commission rate
setBaseSalary( salary ); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// set gross sales amount
public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
// set commission rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// set base salary
public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end method setBaseSalary
// return base salary
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings
public double earnings()
{
return baseSalary + ( commissionRate * grossSales );
} // end method earnings
// return String representation of BasePlusCommissionEmployee
public String toString()
{
return String.format(
"%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
"base-salaried commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales, "commission rate", commissionRate,
"base salary", baseSalary );
} // end method toString
} // end class BasePlusCommissionEmployee
Figure 9.7 BasePlusCommissionEmployee.java
// Fig. 9.7: BasePlusCommissionEmployeeTest.java
// Testing class BasePlusCommissionEmployee.
public class BasePlusCommissionEmployeeTest
{
public static void main( String args[] )
{
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// instantiate BasePlusCommissionEmployee object
BasePlusCommissionEmployee employee =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
// get base-salaried commission employee data
System.out.println(
"Employee information obtained by get methods: \n" );
System.out.printf( "%s %s\n", "First name is",
employee.getFirstName() );
System.out.printf( "%s %s\n", "Last name is",
employee.getLastName() );
System.out.printf( "%s %s\n", "Social security number is",
employee.getSocialSecurityNumber() );
System.out.printf( "%s %.2f\n", "Gross sales is",
employee.getGrossSales() );
System.out.printf( "%s %.2f\n", "Commission rate is",
employee.getCommissionRate() );
System.out.printf( "%s %.2f\n", "Base salary is",
employee.getBaseSalary() );
employee.setBaseSalary( 1000 ); // set base salary
System.out.printf( "\n%s:\n\n%s\n",
"Updated employee information obtained by toString",
employee.toString() );
} // end main
} // end class BasePlusCommissionEmployeeTest
Employee information obtained by get methods:
First name is Bob
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Last name is Lewis
Social security number is 333-33-3333
Gross sales is 5000.00
Commission rate is 0.04
Base salary is 300.00
Updated employee information obtained by toString:
base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04
base salary: 1000.00
Figure 9.8 BasePlusCommissionEmployee2.java
// Fig. 9.8: BasePlusCommissionEmployee2.java
// BasePlusCommissionEmployee2 inherits from class CommissionEmployee.
public class BasePlusCommissionEmployee2 extends CommissionEmployee
{
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee2( String first, String last,
String ssn, double sales, double rate, double salary )
{
// explicit call to superclass CommissionEmployee constructor
super( first, last, ssn, sales, rate );
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
setBaseSalary( amount ); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee2 constructor
// set base salary
public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end method setBaseSalary
// return base salary
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings
public double earnings()
{
// not allowed: commissionRate and grossSales private in superclass
return baseSalary + ( commissionRate * grossSales );
} // end method earnings
// return String representation of BasePlusCommissionEmployee2
public String toString()
{
// not allowed: attempts to access private superclass members
return String.format(
"%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
"base-salaried commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales, "commission rate", commissionRate,
"base salary", baseSalary );
} // end method toString
} // end class BasePlusCommissionEmployee2
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Compile Time Errors
BasePlusCommissionEmployee2.java:34: commissionRate has private access in CommissionEmployee
return baseSalary + ( commissionRate * grossSales );
^
BasePlusCommissionEmployee2.java:34: grossSales has private access in CommissionEmployee
return baseSalary + ( commissionRate * grossSales );
^
BasePlusCommissionEmployee2.java:43: firstName has private access in CommissionEmployee
"base-salaried commission employee", firstName, lastName,
^
BasePlusCommissionEmployee2.java:43: lastName has private access in CommissionEmployee
"base-salaried commission employee", firstName, lastName,
^
BasePlusCommissionEmployee2.java:44: socialSecurityNumber has private access in CommissionEmployee
"social security number", socialSecurityNumber,
^
BasePlusCommissionEmployee2.java:45: grossSales has private access in CommissionEmployee
"gross sales", grossSales, "commission rate", commissionRate,
^
BasePlusCommissionEmployee2.java:45: commissionRate has private access in CommissionEmployee
"gross sales", grossSales, "commission rate", commissionRate,
^
7 errors
Figure 9.9 CommissionEmployee2
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// Fig. 9.9: CommissionEmployee2.java
// CommissionEmployee2 class represents a commission employee.
public class
{
protected
protected
protected
protected
protected
CommissionEmployee2
String
String
String
double
double
firstName;
lastName;
socialSecurityNumber;
grossSales; // gross weekly sales
commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee2( String first, String last, String ssn,
double sales, double rate )
{
// implicit call to Object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
setGrossSales( sales ); // validate and store gross sales
setCommissionRate( rate ); // validate and store commission rate
} // end five-argument CommissionEmployee2 constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// set gross sales amount
public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// set commission rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// calculate earnings
public double earnings()
{
return commissionRate * grossSales;
} // end method earnings
// return String representation of CommissionEmployee2 object
public String toString()
{
return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate );
} // end method toString
} // end class CommissionEmployee2
Figure 9.10 BasePlusCommissionEmployee3.java
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// Fig. 9.10: BasePlusCommissionEmployee3.java
// BasePlusCommissionEmployee3 inherits from CommissionEmployee2 and has
// access to CommissionEmployee2's protected members.
public class BasePlusCommissionEmployee3 extends CommissionEmployee2
{
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee3( String first, String last,
String ssn, double sales, double rate, double salary )
{
super( first, last, ssn, sales, rate );
setBaseSalary( salary ); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee3 constructor
// set base salary
public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end method setBaseSalary
// return base salary
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings
public double earnings()
{
return baseSalary + ( commissionRate * grossSales );
} // end method earnings
// return String representation of BasePlusCommissionEmployee3
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public String toString()
{
return String.format(
"%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
"base-salaried commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales, "commission rate", commissionRate,
"base salary", baseSalary );
} // end method toString
} // end class BasePlusCommissionEmployee3
Figure 9.11 BaseBlusCommissionEmployeeTest3.java
// Fig. 9.11: BasePlusCommissionEmployeeTest3.java
// Testing class BasePlusCommissionEmployee3.
public class BasePlusCommissionEmployeeTest3
{
public static void main( String args[] )
{
// instantiate BasePlusCommissionEmployee3 object
BasePlusCommissionEmployee3 employee =
new BasePlusCommissionEmployee3(
"Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
// get base-salaried commission employee data
System.out.println(
"Employee information obtained by get methods: \n" );
System.out.printf( "%s %s\n", "First name is",
employee.getFirstName() );
System.out.printf( "%s %s\n", "Last name is",
employee.getLastName() );
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
System.out.printf( "%s %s\n", "Social security number is",
employee.getSocialSecurityNumber() );
System.out.printf( "%s %.2f\n", "Gross sales is",
employee.getGrossSales() );
System.out.printf( "%s %.2f\n", "Commission rate is",
employee.getCommissionRate() );
System.out.printf( "%s %.2f\n", "Base salary is",
employee.getBaseSalary() );
employee.setBaseSalary( 1000 ); // set base salary
System.out.printf( "\n%s:\n\n%s\n",
"Updated employee information obtained by toString",
employee.toString() );
} // end main
} // end class BasePlusCommissionEmployeeTest3
Employee information obtained by get methods:
First name is Bob
Last name is Lewis
Social security number is 333-33-3333
Gross sales is 5000.00
Commission rate is 0.04
Base salary is 300.00
Updated employee information obtained by toString:
base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
commission rate: 0.04
base salary: 1000.00
Figure 9.12 CommissionEmployee3.java
// Fig. 9.12: CommissionEmployee3.java
// CommissionEmployee3 class represents a commission employee.
public class CommissionEmployee3
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee3( String first, String last, String ssn,
double sales, double rate )
{
// implicit call to Object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
setGrossSales( sales ); // validate and store gross sales
setCommissionRate( rate ); // validate and store commission rate
} // end five-argument CommissionEmployee3 constructor
// set first name
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// set gross sales amount
public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
// set commission rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end method setCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// calculate earnings
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee3 object
public String toString()
{
return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
"commission employee", getFirstName(), getLastName(),
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
"social security number", getSocialSecurityNumber(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
} // end method toString
} // end class CommissionEmployee3
Figure 9.13 BasePlusCommissionEmployee4.java
//
//
//
//
Fig. 9.13: BasePlusCommissionEmployee4.java
BasePlusCommissionEmployee4 class inherits from CommissionEmployee3 and
accesses CommissionEmployee3's private data via CommissionEmployee3's
public methods.
public class BasePlusCommissionEmployee4 extends CommissionEmployee3
{
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee4( String first, String last,
String ssn, double sales, double rate, double salary )
{
super( first, last, ssn, sales, rate );
setBaseSalary( salary ); // validate and store base salary
} // end six-argument BasePlusCommissionEmployee4 constructor
// set base salary
public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end method setBaseSalary
// return base salary
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings
public double earnings()
{
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee4
public String toString()
{
return String.format( "%s %s\n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary() );
} // end method toString
} // end class BasePlusCommissionEmployee4
Figure 9.14 BasePlusCommissionEmployeeTest4.java
// Fig. 9.14: BasePlusCommissionEmployeeTest4.java
// Testing class BasePlusCommissionEmployee4.
public class BasePlusCommissionEmployeeTest4
{
public static void main( String args[] )
{
// instantiate BasePlusCommissionEmployee4 object
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
BasePlusCommissionEmployee4 employee =
new BasePlusCommissionEmployee4(
"Bob", "Lewis", "333-33-3333", 5000, .04, 300 );
// get base-salaried commission employee data
System.out.println(
"Employee information obtained by get methods: \n" );
System.out.printf( "%s %s\n", "First name is",
employee.getFirstName() );
System.out.printf( "%s %s\n", "Last name is",
employee.getLastName() );
System.out.printf( "%s %s\n", "Social security number is",
employee.getSocialSecurityNumber() );
System.out.printf( "%s %.2f\n", "Gross sales is",
employee.getGrossSales() );
System.out.printf( "%s %.2f\n", "Commission rate is",
employee.getCommissionRate() );
System.out.printf( "%s %.2f\n", "Base salary is",
employee.getBaseSalary() );
employee.setBaseSalary( 1000 ); // set base salary
System.out.printf( "\n%s:\n\n%s\n",
"Updated employee information obtained by toString",
employee.toString() );
} // end main
} // end class BasePlusCommissionEmployeeTest4
Employee information obtained by get methods:
First name is Bob
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Last name is Lewis
Social security number is 333-33-3333
Gross sales is 5000.00
Commission rate is 0.04
Base salary is 300.00
Updated employee information obtained by toString:
base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04
base salary: 1000.00
Figure 9.15 CommissionEmployee4.java
// Fig. 9.15: CommissionEmployee4.java
// CommissionEmployee4 class represents a commission employee.
public class CommissionEmployee4
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// five-argument constructor
public CommissionEmployee4( String first, String last, String ssn,
double sales, double rate )
{
// implicit call to Object constructor occurs here
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
setGrossSales( sales ); // validate and store gross sales
setCommissionRate( rate ); // validate and store commission rate
System.out.printf(
"\nCommissionEmployee4 constructor:\n%s\n", this );
} // end five-argument CommissionEmployee4 constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// set gross sales amount
public void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end method setGrossSales
// return gross sales amount
public double getGrossSales()
{
return grossSales;
} // end method getGrossSales
// set commission rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end method setCommissionRate
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end method getCommissionRate
// calculate earnings
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end method earnings
// return String representation of CommissionEmployee4 object
public String toString()
{
return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
"commission employee", getFirstName(), getLastName(),
"social security number", getSocialSecurityNumber(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
} // end method toString
} // end class CommissionEmployee4
Figure 9.16 BasePlusCommissionEmploye5.java
// Fig. 9.16: BasePlusCommissionEmployee5.java
// BasePlusCommissionEmployee5 class declaration.
public class BasePlusCommissionEmployee5 extends CommissionEmployee4
{
private double baseSalary; // base salary per week
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// six-argument constructor
public BasePlusCommissionEmployee5( String first, String last,
String ssn, double sales, double rate, double salary )
{
super( first, last, ssn, sales, rate );
setBaseSalary( salary ); // validate and store base salary
System.out.printf(
"\nBasePlusCommissionEmployee5 constructor:\n%s\n", this );
} // end six-argument BasePlusCommissionEmployee5 constructor
// set base salary
public void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary;
} // end method setBaseSalary
// return base salary
public double getBaseSalary()
{
return baseSalary;
} // end method getBaseSalary
// calculate earnings
public double earnings()
{
return getBaseSalary() + super.earnings();
} // end method earnings
// return String representation of BasePlusCommissionEmployee5
public String toString()
{
return String.format( "%s %s\n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary() );
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
} // end method toString
} // end class BasePlusCommissionEmployee5
Figure 9.17 ConstructorTest.java
// Fig. 9.17: ConstructorTest.java
// Display order in which superclass and subclass constructors are called.
public class ConstructorTest
{
public static void main (String args [])
{
CommissionEmployee4 employee1 = new CommissionEmployee4 (
"Bob", "Lewis", "333-33-3333", 5000, .04);
System.out.println ();
BasePlusCommissionEmployee5 employee2 =
new BasePlusCommissionEmployee5 (
"Lisa", "Jones", "555-55-5555", 2000, .06, 800);
System.out.println ();
BasePlusCommissionEmployee5 employee3 =
new BasePlusCommissionEmployee5 (
"Mark", "Sands", "888-88-8888", 8000, .15, 2000);
} // end main
} // end class ConstructorTest
CommissionEmployee4 constructor:
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: 5000.00
commission rate: 0.04
CommissionEmployee4 constructor:
base-salaried commission employee: Lisa Jones
social security number: 555-55-5555
gross sales: 2000.00
commission rate: 0.06
base salary: 0.00
BasePlusCommissionEmployee5 constructor:
base-salaried commission employee: Lisa Jones
social security number: 555-55-5555
gross sales: 2000.00
commission rate: 0.06
base salary: 800.00
CommissionEmployee4 constructor:
base-salaried commission employee: Mark Sands
social security number: 888-88-8888
gross sales: 8000.00
commission rate: 0.15
base salary: 0.00
BasePlusCommissionEmployee5 constructor:
base-salaried commission employee: Mark Sands
social security number: 888-88-8888
gross sales: 8000.00
commission rate: 0.15
base salary: 2000.00
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Title
Output
Title
Output
Title
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Output
Title
Output
Title
Output
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Figure 9.x BasePlusCommissionEmployee.java
// Fig. 9.16: BasePlusCommissionEmployee5.java
// BasePlusCommissionEmployee5 class declaration.
public class BasePlusCommissionEmployee5 extends CommissionEmployee4
{
private double baseSalary; // base salary per week
// six-argument constructor
public BasePlusCommissionEmployee5 (String first, String last,
String ssn, double sales, double rate, double salary)
{
super (first, last, ssn, sales, rate);
setBaseSalary (salary); // validate and store base salary
System.out.printf (
"\nBasePlusCommissionEmployee5 constructor:\n%s\n", this);
} // end six-argument BasePlusCommissionEmployee5 constructor
// set base salary
public void setBaseSalary (double salary)
{
baseSalary = (salary < 0.0) ? 0.0:
salary;
} // end method setBaseSalary
// return base salary
public double getBaseSalary ()
{
return baseSalary;
} // end method getBaseSalary
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// calculate earnings
public double earnings ()
{
return getBaseSalary () + super.earnings ();
} // end method earnings
// return String representation of BasePlusCommissionEmployee5
public String toString ()
{
return String.format ("%s %s\n%s: %.2f", "base-salaried",
super.toString (), "base salary", getBaseSalary ());
} // end method toString
} // end class BasePlusCommissionEmployee5
Superclass Point.java
// Fig. 9.9: Point2.java
// Definition of class Point
public class Point {
protected int x, y; // coordinates of Point
// No-argument constructor
public Point()
{
// implicit call to superclass constructor occurs here
setPoint( 0, 0 );
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// constructor
public Point( int xCoordinate, int yCoordinate )
{
// implicit call to superclass constructor occurs here
setPoint( xCoordinate, yCoordinate );
}
// set
public
{
x =
y =
}
x and y coordinates of Point
void setPoint( int xCoordinate, int yCoordinate )
xCoordinate;
yCoordinate;
// get x coordinate
public int getX()
{
return x;
}
// get y coordinate
public int getY()
{
return y;
}
// convert into a String representation
public String toString()
{
return "[" + x + ", " + y + "]";
}
}
// end class Point
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Subclass Circle.java
// Fig. 9.8: Circle.java
// Definition of class Circle
public class Circle extends Point {
protected double radius;
Listen to Author
// inherits from Point
// no-argument constructor
public Circle()
{
// implicit call to superclass constructor occurs here
setRadius( 0 );
}
// constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor to set coordinates
super( xCoordinate, yCoordinate );
// set radius
setRadius( circleRadius );
}
// set radius of Circle
public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0.0 ? circleRadius : 0.0 );
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// get radius of Circle
public double getRadius()
{
return radius;
}
// calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
}
// convert the Circle to a String
public String toString()
{
return "Center = " + "[" + x + ", " + y + "]" +
"; Radius = " + radius;
}
}
// end class Circle
Inheritance Test Program
// Fig. 9.11: InheritanceTest.java
// Demonstrating the "is a" relationship
Listen to Author
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public class InheritanceTest {
// test classes Point and Circle
public static void main( String args[] )
{
Point point1, point2;
Circle circle1, circle2;
point1 = new Point( 30, 50 );
circle1 = new Circle( 2.7, 120, 89 );
String output = "Point point1: " + point1.toString() +
"\nCircle circle1: " + circle1.toString();
// use "is a" relationship to refer to a Circle
// with a Point reference
point2 = circle1;
// assigns Circle to a Point reference
output += "\n\nCircle circle1 (via point2 reference): " +
point2.toString();
// use downcasting (casting a superclass reference to a
// subclass data type) to assign point2 to circle2
circle2 = ( Circle ) point2;
output += "\n\nCircle circle1 (via circle2): " +
circle2.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" );
output += "\nArea of c (via circle2): " +
precision2.format( circle2.area() );
// attempt to refer to Point object with Circle reference
if ( point1 instanceof Circle ) {
circle2 = ( Circle ) point1;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
output += "\n\ncast successful";
}
else
output += "\n\npoint1 does not refer to a Circle";
JOptionPane.showMessageDialog( null, output,
"Demonstrating the \"is a\" relationship",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
// end class InheritanceTest
Figure 9.15, Cylinder.java
// Fig. 9.15: Cylinder.java
// Definition of class Cylinder
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
public class Cylinder extends Circle {
protected double height; // height of Cylinder
// no-argument constructor
public Cylinder()
{
// implicit call to superclass constructor here
setHeight( 0 );
}
// constructor
public Cylinder( double cylinderHeight, double cylinderRadius,
int xCoordinate, int yCoordinate )
{
// call superclass constructor to set coordinates/radius
super( cylinderRadius, xCoordinate, yCoordinate );
// set cylinder height
setHeight( cylinderHeight );
}
// set height of Cylinder
public void setHeight( double cylinderHeight )
{
height = ( cylinderHeight >= 0 ? cylinderHeight : 0 );
}
// get height of Cylinder
public double getHeight()
{
return height;
}
// calculate area of Cylinder (i.e., surface area)
public double area()
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
{
return 2 * super.area() +
2 * Math.PI * radius * height;
}
// calculate volume of Cylinder
public double volume()
{
return super.area() * height;
}
// convert the Cylinder to a String
public String toString()
{
return super.toString() + "; Height = " + height;
}
}
// end class Cylinder
Test the Cylinder Class
// Fig. 9.16: Test.java
// Application to test class Cylinder
Listen to Author
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
// Deitel packages
public class Test {
// test class Cylinder
public static void main( String args[] )
{
// create Cylinder
Cylinder cylinder = new Cylinder( 5.7, 2.5, 12, 23 );
DecimalFormat precision2 = new DecimalFormat( "0.00" );
// get coordinates, radius and height
String output = "X coordinate is " + cylinder.getX() +
"\nY coordinate is " + cylinder.getY() +
"\nRadius is " + cylinder.getRadius() +
"\nHeight is " + cylinder.getHeight();
// set coordinates, radius and height
cylinder.setHeight( 10 );
cylinder.setRadius( 4.25 );
cylinder.setPoint( 2, 2 );
// get String representation of Cylinder and calculate
// area and volume
output += "\n\nThe new location, radius " +
"and height of cylinder are\n" + cylinder +
"\nArea is " + precision2.format( cylinder.area() ) +
"\nVolume is " + precision2.format( cylinder.volume() );
JOptionPane.showMessageDialog( null, output,
"Demonstrating Class Cylinder",
JOptionPane.INFORMATION_MESSAGE );
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
System.exit( 0 );
}
}
// end class Test
Figure 9.17 Point.java with finalize
// Fig. 9.17: Point.java
// Definition of class Point
public class Point extends Object {
protected int x, y; // coordinates of the Point
Listen to Author
// no-argument constructor
public Point()
{
x = 0;
y = 0;
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
System.out.println( "Point constructor: " + this );
}
// constructor
public Point( int xCoordinate, int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
System.out.println( "Point constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Point finalizer: " + this );
}
// convert Point into a String representation
public String toString()
{
return "[" + x + ", " + y + "]";
}
}
// end class Point
Figure 9.18 Circle.java with finalize
// Fig. 9.18: Circle.java
// Definition of class Circle
Create PDF in your applications with the Pdfcrowd HTML to PDF API
Listen to Author
PDFCROWD
public class Circle extends Point {
protected double radius;
// inherits from Point
// no-argument constructor
public Circle()
{
// implicit call to superclass constructor here
radius = 0;
System.out.println( "Circle constructor: " + this );
}
// Constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( xCoordinate, yCoordinate );
radius = circleRadius;
System.out.println( "Circle constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Circle finalizer: " + this );
super.finalize(); // call superclass finalize method
}
// convert the Circle to a String
public String toString()
{
return "Center = " + super.toString() +
"; Radius = " + radius;
}
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
}
// end class Circle
Figure 9.19 Testing Use of finalize
/ Fig. 9.9: Test.java
// Demonstrate when superclass and subclass
// constructors and finalizers are called.
public class Test {
Listen to Author
// test when constructors and finalizers are called
public static void main( String args[] )
{
Circle circle1, circle2;
circle1 = new Circle( 4.5, 72, 29 );
circle2 = new Circle( 10, 5, 5 );
circle1 = null;
circle2 = null;
// mark for garbage collection
// mark for garbage collection
System.gc();
// call the garbage collector
}
}
// end class Test
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Create PDF in your applications with the Pdfcrowd HTML to PDF API
PDFCROWD
Download