Question

Create a class called Employee that should include four pieces of information as instance variables—a firstName...

  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

(write in java program)

Homework Answers

Answer #1

The JAVA code is :


public class Employee {

        // declaring the value 
        private String firstName, lastName, mobileNumber;
        private int salary;
        
        //using getters and setters to get and set the values
        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) {
                this.mobileNumber = mobileNumber;
        }
        public int getSalary() {
                return salary;
        }
        public void setSalary(int salary) {
                this.salary = salary;
        }
        // using parameterized constructor to initiallize the values 
        public Employee(String firstName, String lastName, String mobileNumber, int salary) {
                
                this.firstName = firstName;
                this.lastName = lastName;
                this.mobileNumber = mobileNumber;
                this.salary = salary;
        }
        
        // Using toString() to print the output
        
        @Override
        public String toString() {
                
                if(this.getSalary()> 0) { // checking if salary is greater than zero or not
                        
                        if(this.getMobileNumber().startsWith("05") && this.getMobileNumber().length() == 10) { // checking if mobile number starts with 05 and has 10 digit
                                return "Employee Name : "+this.getFirstName() +" "+this.getLastName()+"\n"+"Mobile Number : "+this.getMobileNumber()
                                +"\n"+"Employee Salary : "+ this.getYearSalary();
                                
                        }else { // Printing the error details to user
                                return "Error : Mobile Number must be of 10 digits and starts with 05 ";
                        }
                        
                }else {// Printing the error details to user
                        return "Error : Salary must be greater than zero";
                }
        }
        // Implementing the getYearSalary method
        public double getYearSalary() {
                if(this.getSalary() > 0) {
                        return this.getSalary()*12;
                }else {
                        
                        return 0.0;
                }
        }
        
        public static void main(String args[]) {
                // Creating the employee object and assigning the value
                Employee e = new Employee("Jhon","Doe","05987645",10);

                // Printing the Employee data
                System.out.println(e.toString());
                
        }

}

Note : There can be many other ways to slove this problem ,this is one of the approach.

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
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
This should be a simple code for beginning Java; Create a Student class with four instance...
This should be a simple code for beginning Java; Create a Student class with four instance variables, a default constructor, a 4-arg constructor, and getters and setters for each instance variable.
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...