Question

Java Programming COMP-228 LAB ASSIGNMENT #1 >> Apply the naming conventions for variables, methods, classes, and...

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a class hierarchy to be used in a university setting. The classes are as follows:...
Create a class hierarchy to be used in a university setting. The classes are as follows:  The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last...
Data structures in java Implement the following classes: 1. class Course that includes three instance variables:...
Data structures in java Implement the following classes: 1. class Course that includes three instance variables: private String Name; // the course name private int ID; // the course ID private Course next; // the link Your class should have the following:  A constructor that initializes the two instance variables id and name.  Set and get methods for each instance variable. 2. class Department that includes three instance variables: private String deptName; private Course head, tail; Your class...
java CLASS DESIGN GUIDELINES 1. Cohesion • [✓] A class should describe a single entity, and...
java CLASS DESIGN GUIDELINES 1. Cohesion • [✓] A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. • [✓] A single entity with many responsibilities can be broken into several classes to separate the responsibilities. 2. Consistency • [✓] Follow standard Java programming style and naming conventions. Choose informative names for classes, data fields, and methods. A popular style is to place the data declaration before the constructor...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
Use a few sentences to describe the problem given below . Also, Identify the nouns and...
Use a few sentences to describe the problem given below . Also, Identify the nouns and verbs used in the below project descriptions.  Identified nouns, list the ones that are necessary to define variables in your program. For each variable, specify its name, data type, and what information it is used to store. Write the pseudo code algorithm (i.e. algorithm steps) to solve this problem. (For the base salaries and commission rates use constants instead to keep these values. Use the...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal...
CSC 322 Systems Programming Fall 2019 Lab Assignment L1: Cipher-machine Due: Monday, September 23 1 Goal In the first lab, we will develop a system program (called cipher-machine) which can encrypt or decrypt a code using C language. It must be an errorless program, in which user repeatedly executes pre-defined commands and quits when he or she wants to exit. For C beginners, this project will be a good initiator to learn a new programming language. Students who already know...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT