Create a class called Employee that includes three pieces ofinformation as insta
ID: 3787484 • Letter: C
Question
Create a class called Employee that includes three pieces ofinformation as instance variables a first name ( type String),a last name (type String), and a monthly salary(double). class should have a constructor thatinitializes the three instance variables. Provide a set and getmethod for each instance variable. If the monthly salary is notpositive, set it to 0.0. Write a test application namedEmployeeTest that demonstrates class Employee's capabilities.Create two Employee objects and display eash object's yearlysalary. Then give each Employee a 10% raise and display eachEmployee's yearly salary again. Create an HourlyEmployee class to store the employee data and calculatePay method. Add class variables and getters/setters for firstName, lastName, empNumber, hours and payRate. Add calculatePay method that returns the total including overtime pay.
Explanation / Answer
public class Employee {
String first_name,last_name; //initialising instance variables
double monthly_salary; //initialising instance variables
public Employee(String first_name, String last_name, double monthly_salary) { //constructor that sets the instance variables
this.first_name = first_name;
this.last_name = last_name;
if(monthly_salary>=0){
this.monthly_salary = monthly_salary; //if salary>0 it is set to same salary
}else{
this.monthly_salary =0; //if salary<0 it is set to 0
}
}
public String getFirst_name() {
return first_name;
}
public String getLast_name() {
return last_name;
}
public double getMonthly_salary() {
return monthly_salary;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public void setMonthly_salary(double monthly_salary) {
this.monthly_salary = monthly_salary;
}
}
public class EmployeeTest {
public static void main(String args[]){
//creating two employee objects
Employee e1=new Employee("Ramesh", "Namburi", 1000);
Employee e2=new Employee("Satya", "Venkata", 1000.201);
//Displays employees salaries
System.out.println("Yearly salary of "+ e1.getFirst_name()+" "+e1.getLast_name()+" is "+12*e1.getMonthly_salary());
System.out.println("Yearly salary of "+ e2.getFirst_name()+" "+e2.getLast_name()+" is "+12*e2.getMonthly_salary());
//increasing salary of each employee by 10%
e1.setMonthly_salary(e1.getMonthly_salary()*1.1);
e2.setMonthly_salary(e2.getMonthly_salary()*1.1);
System.out.println("Yearly salary of "+ e1.getFirst_name()+" "+e1.getLast_name()+" after 10% hike is "+12*e1.getMonthly_salary());
System.out.println("Yearly salary of "+ e2.getFirst_name()+" "+e2.getLast_name()+" after 10% hike is "+12*e2.getMonthly_salary());
}
}
public class HourlyEmployee {
//initialising instance variables
String firstName, lastName;
int empNumber;
double hours,payRate;
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 int getEmpNumber() {
return empNumber;
}
public void setEmpNumber(int empNumber) {
this.empNumber = empNumber;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public double getPayRate() {
return payRate;
}
public void setPayRate(double payRate) {
this.payRate = payRate;
}
public double calculatePay(){
return hours*payRate; //returns total pay
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.