Question

In: Computer Science

Question 1 (Employee): Base Class Information                                   

Question 1 (Employee): Base Class Information                                                          10 Points

  1. Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int).
  2. Your class (Employee) should have a full argument constructor that initializes the four instance variables.
  3. Provide a set and a get method for each instance variable. The validation for each attribute should be like below:
    1. mobileNumber should be started from “05” and the length will be limited to 10 digits.
    2. salary should be greater than zero.
  4. In addition, provide a method named getYearlySalary() that calculates the yearly salary (i.e., multiplies the salary by 12), then returns the amount as a double value.
  5. The toString() print the following information

Employee Name: FirstName LastName

Mobile No. 0512345678

Employee Salary: 2000

Solutions

Expert Solution

Since you have not provided the language, I am providing the code in Java.

CODE

class Employee {
    private String firstName, lastName, mobileNumber;
    private int salary;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        if (mobileNumber.length() == 10 && mobileNumber.startsWith("05")) {
            this.mobileNumber = mobileNumber;
        }
        throw new IllegalArgumentException("Invalid mobile number....");
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        if (salary > 0) {
            this.salary = salary;
        }
        throw new IllegalArgumentException("Invalid salary....");
    }

    public double getYearlySalary() {
        return 12.0 * salary;
    }

    @Override
    public String toString() {
        return "Employee Name: " + firstName + " " + lastName + "\n" +
                "Mobile No,: '" + mobileNumber + '\'' +
                "Employee Salary: " + salary;
    }
}

Related Solutions

Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Part I: The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class.
  Part I:   The Employee Class You are to first update the Employee class to include two virtual functions. This will make the Employee class an abstract class. * Update the function display() to be a virtual function     * Add function, double weeklyEarning() to be a pure virtual function. This will make Employee an abstract class, so your main will not be able to create any Employee objects. Part II: The Derived Classes Add an weeklyEarning() function to each...
Q1(a) A class named “Employee” holds information like employee code, name,gender, year of joining. Write a...
Q1(a) A class named “Employee” holds information like employee code, name,gender, year of joining. Write a program in C++ to create three objects of employee and enter some data into it through setters. Make getters and setters for all employee information. Then ask the user to enter current year. Display the names of those employees whose tenure is 2 or more than 2 years according to the given current year only using getters (b)A class named “Employee” holds information like...
Write a C++ program where class 1 and class 2 (which is a base class) should...
Write a C++ program where class 1 and class 2 (which is a base class) should have a derived class (class 4 and class 5). Each of the derived classes should include at least 1 variable, 2 functions (one will be showing the function overriding case, the second one will be showing the function overloading case), 2 constructors (with default values, with parameters). Then class 3 (composition) (to relate class 1 and class 2) should include 3 variables (first variable...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
Java question, not sure how to do this... You need to create a base class that...
Java question, not sure how to do this... You need to create a base class that should have the following functionality. - Calculate avg. Students grade. Input parameter to this method is an array that shows grades for at least ten courses. You need to create a child class that inherits the base class. The child class should have a method to calculate max grade from 10 courses. You need to write a demo class. The demo class should have...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate);...
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate); protected: string getName() const; float getPayRate() const; float pay(float hoursWorked) const; private: string name; float payRate; }; Definitions for some of the methods follow: Employee::Employee(string theName, float thePayRate) { name = theName; payRate = thePayRate; } float Employee::pay(float hoursWorked) const { return hoursWorked * payRate; } Manager Class: #include "employee.h" class Manager : public Employee { public: Manager(string theName, float thePayRate, bool isSalaried); protected:...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2)...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2) When designing a class hierarchy involving classes A and B, what are the two questions that must be asked to determine whether class B should be derived from class A?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT