0% found this document useful (0 votes)
78 views4 pages

Rectangle & CommissionEmployee Classes

The document describes creating a new method called CalculateCommission in the CommissionEmployee class. This method will calculate the commission amount based on the commissionRate variable. The CommissionEmployee class already contains private variables for grossSales and commissionRate, as well as getters and setters. The new CalculateCommission method will use these existing variables and methods to return the calculated commission.

Uploaded by

Anas Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views4 pages

Rectangle & CommissionEmployee Classes

The document describes creating a new method called CalculateCommission in the CommissionEmployee class. This method will calculate the commission amount based on the commissionRate variable. The CommissionEmployee class already contains private variables for grossSales and commissionRate, as well as getters and setters. The new CalculateCommission method will use these existing variables and methods to return the calculated commission.

Uploaded by

Anas Ansari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise 1:

Create a new class RectangleFromSimpleGeometricObject in example 1 and extend it with


SimpleGeometricObject class. Create an object for this class in the TestCircleRectangle class with a
constructor receiving one parameter of color . Add a method setWidthHeight() which accepts two
double parameters width and height. Add another method getWidthHeight which returns width and
height. Also call the setColor() method of the superclass from the constructor and call the getColor()
method in TestCircleRectangle class for RectangleFromSimpleGeometricObject class.

public class RectangleFromSimpleGeometricObject


extends SimpleGeometricObject {
private double width;
private double height;

public RectangleFromSimpleGeometricObject() {
}

public RectangleFromSimpleGeometricObject(
double width, double height) {
this.width = width;
this.height = height;
}

public RectangleFromSimpleGeometricObject(
double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

/** Return width */


public double getWidth() {
return width;
}

/** Set a new width */


public void setWidth(double width) {
this.width = width;
}

/** Return height */


public double getHeight() {
return height;
}

/** Set a new height */


public void setHeight(double height) {
this.height = height;
}

Lab 7
/** Return area */
public double getArea() {
return width * height;
}

/** Return perimeter */


public double getPerimeter() {
return 2 * (width + height);
}
}

Exercise 2:

Create a new method CalculateCommission in the CommissionEmployee class. This method calculates
the commission based on the value of commissionRate data member

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,
14: double sales, double rate )
15: {
16: // implicit call to Object constructor occurs here
17: firstName = first;
18: lastName = last;
19: socialSecurityNumber = ssn;
20: setGrossSales( sales ); // validate and store gross sales
21: setCommissionRate( rate ); // validate and store commission rate
22: } // end five-argument CommissionEmployee constructor
23:
24: // set first name
25: public void setFirstName( String first )
26: {
27: firstName = first;
28: } // end method setFirstName
29:
30: // return first name
31: public String getFirstName()
32: {
33: return firstName;
34: } // end method getFirstName
35:
36: // set last name
37: public void setLastName( String last )

Lab 7
38: {
39: lastName = last;
40: } // end method setLastName
41:
42: // return last name
43: public String getLastName()
44: {
45: return lastName;
46: } // end method getLastName
47:
48: // set social security number
49: public void setSocialSecurityNumber( String ssn )
50: {
51: socialSecurityNumber = ssn; // should validate
52: } // end method setSocialSecurityNumber
53:
54: // return social security number
55: public String getSocialSecurityNumber()
56: {
57: return socialSecurityNumber;
58: } // end method getSocialSecurityNumber
59:
60: // set commission employee's gross sales amount
61: public void setGrossSales( double sales )
62: {
63: grossSales = ( sales < 0.0 ) ? 0.0 : sales;
64: } // end method setGrossSales
65:
66: // return commission employee's gross sales amount
67: public double getGrossSales()
68: {
69: return grossSales;
70: } // end method getGrossSales
71:
72: // set commission employee's rate
73: public void setCommissionRate( double rate )
74: {
75: commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
76: } // end method setCommissionRate
77:
78: // return commission employee's rate
79: public double getCommissionRate()
80: {
81: return commissionRate;
82: } // end method getCommissionRate
83:
84: // calculate commission employee's pay
85: public double earnings()
86: {
87: return commissionRate * grossSales;
88: } // end method earnings
89:
90: // return String representation of CommissionEmployee object
91: public String toString()
92: {

Lab 7
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 CommissionEmployee

Lab 7

You might also like