Martin your friend has declared a class called Employee with the following members; Employee
ID, Employee Name, Basic Salary, Allowance, and Net Salary.
a) Write a code for a member function called get_input that will be used for data input.
(3 Marks)
b) Write a code for a member function called Calculate that will be used to calculate net
salary as the sum of basic salary and allowances.
(2 Marks)
c) Write a code for the default constructor that will assign values to the variables in the
Employee class.
(3 Marks)
d) Write a code for the default constructor.
(3 Marks)
e) Write a complete program that captures and includes all the details in (a-d) above.
Run the code and show your output.
import java.util.Scanner;
public class Employee {
private String id;
private String employeeName;
private double basicSalary;
private double allowance;
private double netSalary;
public Employee() {
id = "Unknown";
employeeName = "Unknown";
basicSalary = -1;
allowance = -1;
netSalary = -1;
}
public void getInput() {
Scanner in = new Scanner(System.in);
System.out.println("ID:");
id = in.nextLine();
System.out.println("Name:");
employeeName = in.nextLine();
System.out.println("Basic salary:");
basicSalary = Double.parseDouble(in.nextLine());
System.out.println("Allowance:");
allowance = Double.parseDouble(in.nextLine());
System.out.println("Net salary:");
netSalary = Double.parseDouble(in.nextLine());
}
public void calculate() {
netSalary = basicSalary + allowance;
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
employee.getInput();
employee.calculate();
}
}
Comments
Leave a comment