Please I need an explanation of the code package lap; class Employee { private String firstName; private String lastName; private double monthlySalary = 0.0; public Employee(String firstName, String lastName, double monthlySalary) { this.firstName = firstName; this.lastName = lastName; if (monthlySalary >= 0) { this.monthlySalary = monthlySalary;
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Please I need an explanation of the code
package lap;
class Employee {
private String firstName;
private String lastName;
private double monthlySalary = 0.0;
public Employee(String firstName, String lastName, double monthlySalary) {
this.firstName = firstName;
this.lastName = lastName;
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setMonthlySalary(double monthlySalary) {
if (monthlySalary >= 0) {
this.monthlySalary = monthlySalary;
}
}
public double getMonthlySalary() {
return monthlySalary;
}
public double getYearlySalary(){
return monthlySalary*12;
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee aptab = new Employee("Aptab", "Sk", 15000);
Employee aslam = new Employee("Aslam", "Md", 20000);
System.out.print( aptab.getFirstName() + " " + aptab.getLastName() + "'s monthly salary is " + aptab.getMonthlySalary());
System.out.println(" & yearly salary is "+ aptab.getYearlySalary());
System.out.print(aslam.getFirstName() + " " +aslam.getLastName() + "'s monthly salary is " + aslam.getMonthlySalary());
System.out.println(" & yearly salary is "+aslam.getYearlySalary());
}
}
Step by step
Solved in 3 steps with 3 images