Java Programming COMP-228
LAB ASSIGNMENT #1
>> Apply the naming conventions for variables, methods, classes, and packages:
- variable names start with a lowercase character for the first word and uppercase for every other word
- classes start with an uppercase character of every word
- packages use only lowercase characters
- methods start with a lowercase character for the first word and uppercase for every other word Java Programming COMP-228
Exercise #1: [5 marks]
Write a Java application using eclipse as IDE, that implements the following class(es) as per business requirements mentioned below:
Create a CommissionEmployee class (CommissionEmployee.java) that has the following instance variables:
- Employee ID, first name, last name, gross sales ( amount in dollars) and commission rate. Define their data types appropriately.
- Define only getters for employee ID, first name, last name. Ensure the proper ( no negative and null values ) data values entered by implementing data validations.
- Define getter and setter for gross sales and commission rate. Ensure the values for them should never be negative or zero.
- Commission rate should be between 0.1 and 1.0%. Set default value 0.1.
- Class should have defined two overloaded constructors:
o One for initializing all the instance data members/variables
o Second for initializing employee ID, first name, last name
- Define a public method - double earnings() which calculates employee’s commission ( commission rate * gross sales/100)
- Define a public method – String toString() which is used to display the object’s data
Create another BasePlusCommissionEmployee class (BasePlusCommissionEmployee.java) that has the following instance variables:
- Employee ID, First name, last name, base salary, gross sales ( amount in dollars) and commission rate. Define their data types appropriately.
- Define only getters for employee ID, first name, last name and base salary. Ensure the proper ( no negative and null values ) data values by implementing data validations.
- Use default value of 200.00 dollars for base salary for all the employees.
- Define getter and setter for gross sales and commission rate. Ensure the values for them should never be negative or zero.
- Commission rate should be between 0.1 and 1.0%. Set default value 0.1.
- Class should have defined two overloaded constructors:
o One for initializing all the instance data members
o Second for initializing employee ID, first name, last name, base salary only ( if you want to set it more than 200.00 dollars)
- Define a public method - double earnings() which calculates employee’s commission ( commission rate * gross sales/100 + base salary )
- Define a public method – String toString() which is used to display the object’s data
Create a HourlyEmployee class (HourlyEmployee.java) that has the following instance variables:
- Employee ID, first name, last name, total number of hours worked per week, and hourly rate ( amount in dollars). Define their data types appropriately.
- Define only getters for employee ID, first name, last name. Ensure the proper (no negative and null values ) data values by implementing data validations.
Java Programming COMP-228
- Define getters and setters for number of hours worked per week and hourly rate. Ensure the values for them should never be negative or zero.
- Hourly rate should be minimum 14.00 dollars per hour.
- Class should have defined two overloaded constructors:
o One for initializing all the instance data members/variables
o Second for initializing employee ID, first name, last name
- Define a public method - double earnings() which calculates employee’s commission (number of hours worked per week * commission rate)
- Define a public method – String toString() which is used to display the object’s data
Create a driver class EmployeeTest (EmployeeTest.java) which tests above classes by at least creating one object each of CommissionEmployee, BasePlusCommissionEmployee and HourlyEmployee classes.
Please find the answer below, have defined all the details in the comments. Code is developed as per the instructions are given in the question.
CommissionEmployee.java
//CommissionEmployee.java class public class CommissionEmployee { //private date members of the class private int employeeId; private String firstName; private String lastName; private double grossSales; //default value for the commission rate to 0.1 private double commissionRate = 0.1; //getter methods public int getEmployeeId() { return employeeId; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } //getter and setter for the grossSales and commissionRate public double getGrossSales() { return grossSales; } public void setGrossSales(double grossSales) { if(grossSales > 0) { this.grossSales = grossSales; } } public double getCommissionRate() { return commissionRate; } public void setCommissionRate(double commissionRate) { if(commissionRate > 0 && (commissionRate >= 0.1 && commissionRate<=1.0)) { this.commissionRate = commissionRate; } } //constructor 1 public CommissionEmployee(int employeeId, String firstName, String lastName, double grossSales, double commissionRate) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; if(grossSales > 0) this.grossSales = grossSales; if(commissionRate > 0 && (commissionRate >= 0.1 && commissionRate<=1.0)) this.commissionRate = commissionRate; } //constructor 2 public CommissionEmployee(int employeeId, String firstName, String lastName) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; } //earnings to calculate the earning of the employee public double earnings(){ double commission = 0; commission = (this.commissionRate*this.grossSales)/100.0; return commission; } //to string method to print the details @Override public String toString() { return "CommissionEmployee{" + "employeeId=" + employeeId + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", grossSales=" + grossSales + ", commissionRate=" + commissionRate + '}'; } }
BasePlusCommissionEmployee.java
public class BasePlusCommissionEmployee { //private date members of the class private int employeeId; private String firstName; private String lastName; //default value for the base salary private double baseSalary = 200.00; private double grossSales; //default value for the commission rate to 0.1 private double commissionRate = 0.1; //getters public int getEmployeeId() { return employeeId; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public double getBaseSalary() { return baseSalary; } //getter and setter for the grossSales and commissionRate public double getGrossSales() { return grossSales; } public void setGrossSales(double grossSales) { if(grossSales > 0) { this.grossSales = grossSales; } } public double getCommissionRate() { return commissionRate; } public void setCommissionRate(double commissionRate) { if(commissionRate > 0 && (commissionRate >= 0.1 && commissionRate<=1.0)) { this.commissionRate = commissionRate; } } //constructor 1 public BasePlusCommissionEmployee(int employeeId, String firstName, String lastName, double baseSalary, double grossSales, double commissionRate) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; if(baseSalary > 0) this.baseSalary = baseSalary; if(grossSales > 0) this.grossSales = grossSales; if(commissionRate > 0 && (commissionRate >= 0.1 && commissionRate<=1.0)) this.commissionRate = commissionRate; } //constructor 2 public BasePlusCommissionEmployee(int employeeId, String firstName, String lastName, double baseSalary) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; if(baseSalary > 200) this.baseSalary = baseSalary; } //earnings to calculate employee's earnings public double earnings(){ double commision = ((this.commissionRate * this.grossSales)/100.0) + this.baseSalary; return commision; } //tostring the print the object details @Override public String toString() { return "BasePlusCommissionEmployee{" + "employeeId=" + employeeId + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", baseSalary=" + baseSalary + ", grossSales=" + grossSales + ", commissionRate=" + commissionRate + '}'; } }
HourlyEmployee.java
public class HourlyEmployee { //private date members of the class private int employeeId; private String firstName; private String lastName; private double totalHoursWroked; //default value for the hourly rate private double hourlyRate = 14.00; //getters public int getEmployeeId() { return employeeId; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } //getters and setters public double getTotalHoursWroked() { return totalHoursWroked; } public void setTotalHoursWroked(double totalHoursWroked) { if(totalHoursWroked > 0) this.totalHoursWroked = totalHoursWroked; } public double getHourlyRate() { return hourlyRate; } public void setHourlyRate(double hourlyRate) { if(hourlyRate > 0) this.hourlyRate = hourlyRate; } //constructor 1 public HourlyEmployee(int employeeId, String firstName, String lastName, double totalHoursWroked, double hourlyRate) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; if(totalHoursWroked > 0) this.totalHoursWroked = totalHoursWroked; if(hourlyRate > 0) this.hourlyRate = hourlyRate; } //constructor 2 public HourlyEmployee(int employeeId, String firstName, String lastName) { if(employeeId > 0) this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; } //calculate the employee's earnings public double earnings(){ double commision = (this.totalHoursWroked * this.hourlyRate); return commision; } //print the object details @Override public String toString() { return "HourlyEmployee{" + "employeeId=" + employeeId + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", totalHoursWroked=" + totalHoursWroked + ", hourlyRate=" + hourlyRate + '}'; } }
EmployeeTest.java
public class EmployeeTest { public static void main(String[] args) { CommissionEmployee cme = new CommissionEmployee(1,"John","Jacob",100000,0.5); System.out.println("Commission Employee Details: "); System.out.println(cme); System.out.println("Commission Employee Earnings: " + cme.earnings()); System.out.println(); BasePlusCommissionEmployee bcme = new BasePlusCommissionEmployee(2,"Any","Kay",500,100000,0.5); System.out.println("Base plus Commission Employee Details: "); System.out.println(bcme); System.out.println("Base plus Commission Employee Earnings: " + bcme.earnings()); System.out.println(); HourlyEmployee he = new HourlyEmployee(3,"Neil","Smith",50,20); System.out.println("Hourly Employee Details: "); System.out.println(he); System.out.println("Hourly Employee Earnings: " + he.earnings()); } }
Output:
Get Answers For Free
Most questions answered within 1 hours.