Employee.java class Employee { //members of class to hold firstname,lastname and
ID: 3793848 • Letter: E
Question
Employee.java
class Employee
{
//members of class to hold firstname,lastname and salary
private String firstname;
private String lastname;
private double salary;
//default constructor
Employee()
{
firstname="";
lastname="";
salary=0;
}
//constructor with parameters
Employee(String fname, String lname,double sal)
{
firstname=fname;
lastname=lname;
salary=sal;
}
//accessors and mutators
//method that sets the firstname
public void setFirst(String fname)
{
firstname=fname;
}
//method that returns the firstname
public String getFirst()
{
return firstname;
}
//method that sets lastname
public void setLast(String lname)
{
lastname=lname;
}
//method that returns lastname
public String getLast()
{
return lastname;
}
//method that sets salary
public void setSalary(double sal)
{
salary=sal;
}
//method that returns salary
public double getSalary()
{
return salary;
}
//method that calculates and returns year to date salary
//returns the running total of the salary upto specified month
public double ytdSalary(int mo)
{
return salary*mo;
}
}
Please write a class for this java program and save it as employee.class
Explanation / Answer
Hi Friend, I have implemented a Test Program for give code.
Please let me know in case of any issue.
public class EmployeeTest {
public static void main(String[] args) {
// creating Employee object
Employee emp = new Employee();
// setting all information
emp.setFirst("Pravesh");
emp.setLast("Kumar");
emp.setSalary(12345.6);
// printing all inforamtion
System.out.println("First Name: "+emp.getFirst());
System.out.println("Last Name: "+emp.getLast());
System.out.println("Salary : "+emp.getSalary());
System.out.println("Total grossfor month : "+emp.ytdSalary(12));
}
}
/*
Sample run:
First Name: Pravesh
Last Name: Kumar
Salary : 12345.6
Total grossfor month : 148147.2
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.