Skip to content

Commit 43b2ef7

Browse files
committed
New
1 parent 02a96a7 commit 43b2ef7

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Fig 9.8 BasePlusCommissionEmployee.java
2+
// private superclass members cannot be accessed in a subclass
3+
4+
public class BasePlusCommissionEmployee extends CommissionEmployee
5+
{
6+
private double baseSalary; // base salary per week
7+
8+
// six-argument constructor
9+
public BasePlusCommissionEmployee(String first, String last, String ssn, double sales, double rate, double salary)
10+
{
11+
// explicit call to superclass CommissionEmployee constructor
12+
super(first, last, ssn, sales, rate);
13+
14+
setBaseSalary(salary); // validate and store base salary
15+
// end six-argument BasePlusCommissionEmployee constructor
16+
}
17+
18+
// set base salary
19+
public void setBaseSalary(double salary)
20+
{
21+
if (salary >=0.0)
22+
baseSalary = salary;
23+
else
24+
throw new IllegalArgumentException("Base salary must be >= 0.0");
25+
} // end method setBaseSalary
26+
27+
// return base salary
28+
public double getBaseSalary()
29+
{
30+
return baseSalary;
31+
} // end method getBaseSalary
32+
33+
// calculate earnings
34+
@Override // indicates that this method overrides a superclass method
35+
public double earnings()
36+
{
37+
// not allowed: commissionRate and grossSale private in superclass
38+
return baseSalary + (getCommissionRate() * getGrossSales());
39+
} // end method earnings
40+
41+
// return String representation of BasePlusCommissionEmployee
42+
@Override // indicate that this method overrides a superclass method
43+
public String toString()
44+
{
45+
// not allowed: attempts to access private superclass members
46+
return String.format("%s %s\n%s: %.2f", "base-salaried",
47+
super.toString(), "base salary", getBaseSalary());
48+
}
49+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Fig. 9.7: BasePlusCommissionEmployeeTest.java
2+
// BasePlusCommissionEmployee class test program
3+
4+
public class BasePlusCommissionEmployeeTest
5+
{
6+
public static void main(String[] args)
7+
{
8+
// instantiate BasePlusCommissionEmployee object
9+
BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000, .04, 300);
10+
11+
// get base-salaried commission employee data
12+
System.out.println(
13+
"Employee information obtained by get methods: \n");
14+
System.out.printf("%s %s \n", "First name is", employee.getFirstName());
15+
System.out.printf("%s %s \n", "Last name is", employee.getLastName());
16+
System.out.printf("%s %s\n", "Social security number is", employee.getSocialSecurityNumber());
17+
System.out.printf("%s %.2f\n", "Gross sales is", employee.getGrossSales());
18+
System.out.printf("%s %.2f\n", "Commission rate is", employee.getCommissionRate());
19+
System.out.printf("%s %.2f\n", "Base salary is", employee.getBaseSalary());
20+
21+
employee.setBaseSalary(1000); // set base salary
22+
23+
System.out.printf("\n%s:\n\n%s\n",
24+
"Update employee information obtained by toString", employee);
25+
System.out.println(employee.getFirstName().getClass());
26+
}
27+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Fig. 9.10: CommissionEmployee.java
2+
// CommissionEmployee class uses methods to manipulate its
3+
// private instance variables
4+
public class CommissionEmployee extends Object
5+
{
6+
private String firstName;
7+
private String lastName;
8+
private String socialSecurityNumber;
9+
private double grossSales; // gross weekly sales
10+
private double commissionRate; // commission percentage
11+
12+
// five-argument constructor
13+
public CommissionEmployee( String first, String last, String ssn, double sales, double rate)
14+
{
15+
// implicit call to Object constructor occurs here
16+
firstName = first;
17+
lastName = last;
18+
socialSecurityNumber = ssn;
19+
setGrossSales(sales); // validate and store gross sales
20+
setCommissionRate(rate); // validate and store commission rate
21+
} // end five-argument CommissionEmployee constructor
22+
23+
// set first name
24+
public void setFirstName(String first)
25+
{
26+
firstName = first; // should validate
27+
} // end method setFirstName
28+
29+
// return first name
30+
public String getFirstName()
31+
{
32+
return firstName;
33+
} // end method getFirstName
34+
35+
public void setLastName(String last)
36+
{
37+
lastName = last; // should validate
38+
} // end method setLastName
39+
40+
// return last name
41+
public String getLastName()
42+
{
43+
return lastName;
44+
} // end method getLastName
45+
46+
// set social security number
47+
public void setSocialSecurityNumber(String ssn)
48+
{
49+
socialSecurityNumber = ssn; // should validate
50+
} // end method setSocialSecurityNumber
51+
52+
// return social security number
53+
public String getSocialSecurityNumber()
54+
{
55+
return socialSecurityNumber;
56+
} // end method getSocialSecurityNumber
57+
58+
// set gross sales amount
59+
public void setGrossSales(double sales)
60+
{
61+
if (sales >= 0.0)
62+
grossSales = sales;
63+
else
64+
throw new IllegalArgumentException(
65+
"Gross sales must be >= 0.0");
66+
} // end method setGrossSales
67+
68+
// return gross sales amount
69+
public double getGrossSales()
70+
{
71+
return grossSales;
72+
} // end method getGrossSales
73+
74+
// set commission rate
75+
public void setCommissionRate(double rate)
76+
{
77+
if (rate > 0.0 && rate < 1.0)
78+
commissionRate = rate;
79+
else
80+
throw new IllegalArgumentException(
81+
"Commission rate must be > 0.0 and < 1.0" );
82+
} // end method setCommissionRate
83+
84+
// return commission rate
85+
public double getCommissionRate()
86+
{
87+
return commissionRate;
88+
} // end method getCommissionRate
89+
90+
// calculate earnings
91+
public double earnings()
92+
{
93+
return getCommissionRate() * getGrossSales();
94+
} // end method earnings
95+
96+
// return String representation of CommissionEmployee object
97+
@Override // indicates that this method overrides a superclass method
98+
public String toString()
99+
{
100+
return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
101+
"commission employee", getFirstName(), getLastName(),
102+
"social security number", getSocialSecurityNumber(),
103+
"gross sales", getGrossSales(),
104+
"commission rate", getCommissionRate());
105+
} // end method toString
106+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Fig. 10.1 PolymorphismTest.java
2+
// Assigning superclass and subclass references to superclass and
3+
// subclass variables
4+
5+
public class PolymorphismTest
6+
{
7+
public static void main(String[] args)
8+
{
9+
// assign superclass reference to superclass variable
10+
CommissionEmployee commissionEmployee = new CommissionEmployee(
11+
"Sue", "Jones", "222-22-2222", 10000, .06);
12+
13+
// assign subclass reference to subclass variable
14+
BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee(
15+
"Bob", "Lewis", "333-33-3333", 5000, .04, 300);
16+
17+
// invoke toString on superclass object user superclass variable
18+
System.out.printf("%s %s:\n\n%s\n\n",
19+
"Call CommissionEmployee's toString with superclass reference ",
20+
"to superclass object", commissionEmployee.toString());
21+
22+
// invoke toString on subclass object using subclass variable
23+
System.out.printf("%s %s:\n\n%s\n\n",
24+
"Call BasePlusCommissionEmployee's toString with subclass",
25+
"reference to subclass object",
26+
basePlusCommissionEmployee.toString());
27+
28+
// invoke toString on subclass object using superclass variable
29+
CommissionEmployee commissionEmployee2 = basePlusCommissionEmployee;
30+
System.out.printf("%s %s:\n\n%s\n",
31+
"Call BasePlusCommissionEmployee's toString with superclass",
32+
"reference to subclass object", commissionEmployee2.toString());
33+
} // end main
34+
} // end class PolymorphismTest

0 commit comments

Comments
 (0)