Employee Name: FirstName LastName
Mobile No. 0512345678
Employee Salary: 2000
(write in java program)
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.
Get Answers For Free
Most questions answered within 1 hours.