Constructor

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question

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.

:/
********************************************************************
**********

Welcome to GDB Online.


GDB online is an online compiler and debugger tool for C, C++,
Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C,
Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

********************************************************************
***********/
import java.util.Scanner; // Import the Scanner class
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();
}
}

Explanation:
In the above code, the Employee class encapsulates the private instance variables
employee_id, employee_name, and employee_salary. The getEmployeeId() and
getEmployeeName() methods are public getter methods that allow other classes to access
employee_id and employee_name values, respectively. The setEmployeeId() and
setEmployeeName() methods are public setter methods that allow other classes to modify
employee_id and employee_name values.

The getEmployeeSalary() method is another public getter method that returns


employee_salary. However, we have also added an additional method getFormattedSalary()
that returns a formatted string representation of the salary. This method provides a formatted
version of the salary value, adding a dollar sign and two decimal places.
Write a program to print the names of Teacher by creating a Teacher class. If no name is
passed while creating an object of Teacher class, then the name should be "No information",
otherwise the name should be equal to the String value passed while creating object of
Teacher class.

You might also like