Problem:
Create a class called Employee that includes three pieces of information as instance variables
1. First name (type String)
2. Last name (type String)
3. Monthly salary (double).
Your class should have the following methods:
-Constructor that initializes the three instance variables.
-Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0.
Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again.
1. First name (type String)
2. Last name (type String)
3. Monthly salary (double).
Your class should have the following methods:
-Constructor that initializes the three instance variables.
-Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0.
Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again.
Output:
Not needed.
Solution:
public class Employee { private String firstName; private String lastName; private double monthlySalary; public Employee(String f, String l, double m){ firstName = f; lastName = l; if(m < 0){ // you can also use setMonthlySalary(m) monthlySalary =0; } else monthlySalary = m; } public String getFirstName() { return firstName; } public void setFirstName(String fname) { firstName = fname; } public String getLastName() { return lastName; } public void setLastName(String lname) { lastName = lname; } public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double m) { if(m < 0){ monthlySalary =0; } else monthlySalary = m; } public static void main(String[] args){ Scanner S = new Scanner(System.in); System.out.println("Enter the first name: "); String fname = S.next(); System.out.println("Enter the last name: "); String lname = S.next(); System.out.println("Enter the Salary: "); double sal = S.nextDouble(); Employee e =new Employee(fname,lname ,sal ); System.out.println("the yearly salary of "+e.getFirstName()+" " +e.getLastName()+" :"); System.out.println(e.getMonthlySalary()*12); double newsalary= e.getMonthlySalary()*0.1+e.getMonthlySalary(); e.setMonthlySalary(newsalary); System.out.println("the new yearly salary of "+ e.getFirstName()+" "+e.getLastName()+" :"); System.out.println(e.getMonthlySalary()*12); e.getMonthlySalary(); } }
No comments :
Post a Comment