Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

Q1

A. Define a class called Employee with the following


i. define a java class Employee with the following data fields or parameters
emp_id,salary,name,address,department,email
public class Employee {
private int emp_id;
private double salary;
private String name;
private String address;
private String department;
private String email;

// Constructor
public Employee(int emp_id, double salary, String name, String address,
String department, String email) {
this.emp_id = emp_id;
this.salary = salary;
this.name = name;
this.address = address;
this.department = department;
this.email = email;
}

ii. make shared fields called requiredHours and employeeBonus and initialize them to be enterd by
the user in a static initializer
import java.util.Scanner;

public class Employee {


private static int requiredHours;
private static double employeeBonus;

// Static initializer
static {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the required hours: ");
requiredHours = scanner.nextInt();

System.out.print("Enter the employee bonus: ");


employeeBonus = scanner.nextDouble();
}

public static void main(String[] args) {


// Access requiredHours and employeeBonus here
System.out.println("Required Hours: " + requiredHours);
System.out.println("Employee Bonus: " + employeeBonus);
}
}
iii. make a no-argument constructor that initializes name to Sabonmaaikaci, emp_id to 000 and
salary to 200000.0

import java.util.Scanner;

public class Employee {


private int emp_id;
private double salary;
private String name

// Static initializer
// ...

// No-argument constructor
public Employee() {
this.name = "Sabonmaaikaci";
this.emp_id = 000;
this.salary = 200000.0;
}
}

iv. make a parameterized constructor for initializing emp_id,name and salary


public class Employee {
private int emp_id;
private double salary;
private String name;

// Parameterized constructor for emp_id, name, and salary


public Employee(int emp_id, String name, double salary) {
this.emp_id = emp_id;
this.name = name;
this.salary = salary;
}

public static void main(String[] args) {

// Create an instance using the parameterized constructor


Employee employee = new Employee(1001, "John Doe", 5000.0);
//employee.displayInformation();
}
}

v. make a parameterized constructor for initializing all the data members Note:use of constructor
call is mandatory
public class Employee {
private int emp_id;
private double salary;
private String name;
private String address;
private String department;
private String email;
private static int requiredHours;
private static double employeeBonus;
// Parameterized constructor for initializing all data members
public Employee(int emp_id, double salary, String name, String address,
String department, String email) {
this.emp_id = emp_id;
this.salary = salary;
this.name = name;
this.address = address;
this.department = department;
this.email = email;
}

public static void main(String[] args) {

// Create an instance using the parameterized constructor


Employee employee = new Employee(1001, 5000.0, "John Doe", "123 Main
St", "IT", "johndoe@example.com");
//employee.displayInformation();
}
}

vi. make a setter and a getter for emp_id

public class Employee {


private int emp_id;

// Setter for emp_id


public void setEmpId(int emp_id) {
this.emp_id = emp_id;
}

// Getter for emp_id


public int getEmpId() {
return emp_id;
}

Vii. make a setter and a getter for name


public class Employee {
private String name;

// Setter for name


public void setName(String name) {
this.name = name;
}

// Getter for name


public String getName() {
return name;
}

}
Viii. make computeOvertime int hoursWorked that compute overtime payments and add it on top of
salary. the overtime payment is 5000
public class Employee {
private double salary;
private double requiredHours;

public void computeOvertime(int hoursWorked) {


if (hoursWorked > requiredHours) {
double overtimePayment = (hoursWorked - requiredHours) * 5000;
salary += overtimePayment;
System.out.println("Overtime Payment: " + overtimePayment);
System.out.println("Updated Salary: " + salary);
} else {
System.out.println("No overtime worked.");
}
}

ix. make computeEmployeeBonus that computes bonus base on employeeBonus (a percentage) and
add it on top of salary
public class Employee {
private double salary;
private static double employeeBonus;

public void computeOvertime(int hoursWorked) {

public void computeEmployeeBonus() {


double bonus = salary * (employeeBonus / 100);
salary += bonus;
System.out.println("Employee Bonus: " + bonus);
System.out.println("Updated Salary: " + salary);
}
}

x. printEmployeeDetails method that prints string representation of an object of Employee class. the
printed string should contain the following each on a separate line emp_id, salary, name
, address , department , email
public class Employee {
private int emp_id;
private String name;
private double salary;
private String address;
private String department;
private String email;

public void printEmployeeDetails(int emp_id, String name, double salary,


String address, String department, String email) {

System.out.println("Employee ID: " + emp_id);


System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
System.out.println("Email: " + email);
}

public static void main(String[] args) {

// Create an instance of constructor


Employee employee = new Employee();

// Print employee details


employee.printEmployeeDetails(10,"Jujitsu", 5000.00,"Kofar dawanau",
"IT", "jujitsu@kaisen.com");
}
}

B. write a program to test Employee class


//if this is emplyee class
public class Employee {
private int emp_id;
private double salary;
private String name;
private String address;
private String department;
private String email;

public Employee(int emp_id, double salary, String name, String address, String department,
String email) {
this.emp_id = emp_id;
this.salary = salary;
this.name = name;
this.address = address;
this.department = department;
this.email = email;
}

public int getEmpId() {


return emp_id;
}

public void setEmpId(int emp_id) {


this.emp_id = emp_id;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public String getDepartment() {


return department;
}

public void setDepartment(String department) {


this.department = department;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public void displayInformation() {


System.out.println("\nEmployee ID: " + emp_id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
System.out.println("Email: " + email);
}
}

//below code can test employee class

public class TestEmployee {


public static void main(String[] args) {
Employee employee1 = new Employee(1001, 5000.0, "Jack de sparrow", "St Martin", "IT",
"captainjackdesparrow@caribbean.com");
Employee employee2 = new Employee(1002, 6000.0, "Galadima", "435 Maiduguri road",
"Sanitation", "legend@ksg.com");
// Display employee information
employee1.displayInformation();
employee2.displayInformation();

// Update employee information


employee1.setSalary(5500.0);
employee2.setName("awolowo ");

// Display updated employee information


employee1.displayInformation();
employee2.displayInformation();
}
}

C. create an interface called SpecialEmployeeInterface with specialAllowance method

public interface SpecialEmployeeInterface {


double specialAllowance();

// ............
}

//class that implements the SpecialEmployeeInterface:

public class RegularEmployee implements SpecialEmployeeInterface {


private double salary;

public RegularEmployee(double salary) {


this.salary = salary;
}

// Implementing the specialAllowance method as required by the interface


@Override
public double specialAllowance() {
// Implement the logic to calculate the special allowance for regular employees here
// For example, return a percentage of the salary.
return salary * 0.1; // 10% of the salary as special allowance
}

// Other methods and details of the RegularEmployee class...


}

D. create a subclass of Employee class called SpecialEmployeeClass that implements


SpecialEmployeeInterface interface and specialAllowance method compute allowance based on
salary as follows
12% of salary : for salary between 200,000 and 300,000 inclusive
10% of salary : for salary above 300,000 but less than 500,000
8% of salary : for salary above 500,000 inclusive

public class SpecialEmployeeClass extends Employee implements SpecialEmployeeInterface {


public SpecialEmployeeClass(int emp_id, double salary, String name, String address, String
department, String email) {
super(emp_id, salary, name, address, department, email);
}

// Implementing the specialAllowance method as required by the SpecialEmployeeInterface


@Override
public double specialAllowance() {
double salary = getSalary();
if (salary >= 200000 && salary <= 300000) {
return salary * 0.12; // 12% of the salary for salary between 200,000 and 300,000 inclusive
} else if (salary > 300000 && salary < 500000) {
return salary * 0.10; // 10% of the salary for salary above 300,000 but less than 500,000
} else if (salary >= 500000) {
return salary * 0.08; // 8% of the salary for salary above 500,000 inclusive
} else {
return 0; // No special allowance for salary below 200,000
}
}

// You can add other methods specific to SpecialEmployeeClass here if needed.


}

E. write a java program that declares a jagged array of 5 Employee objects arrays, containing
1,2,3,4,5, objects respectively, read all the required details from the user, compute bonus for each
object, and hence use the printEmployeeDetails() method to computeOvertime(int hoursWorked)
that computes overtime payments and add it on top of salary. The overtime payment is 5000.0 per
hour and print the details of each of the objects
import java.util.Scanner;
public class Employee {
private int emp_id;
private double salary;
private String name;
private String address;
private String department;
private String email;
private static int requiredHours = 40; // Assuming the required working hours per week is 40
private static double employeeBonus = 10; // Assuming the bonus percentage is 10%

// Constructor for initializing data members


public Employee(int emp_id, double salary, String name, String address, String department,
String email) {
this.emp_id = emp_id;
this.salary = salary;
this.name = name;
this.address = address;
this.department = department;
this.email = email;
}

// Method to compute overtime payment


public void computeOvertime(int hoursWorked) {
double overtimePayment = 0;
if (hoursWorked > requiredHours) {
int overtimeHours = hoursWorked - requiredHours;
overtimePayment = overtimeHours * 5000.0;
salary += overtimePayment;
}
System.out.println("Overtime Payment: " + overtimePayment);
}

// Method to compute employee bonus


public void computeEmployeeBonus() {
double bonus = salary * (employeeBonus / 100);
salary += bonus;
System.out.println("Employee Bonus: " + bonus);
}

// Method to print employee details


public void printEmployeeDetails() {
System.out.println("\nEmployee ID: " + emp_id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Address: " + address);
System.out.println("Department: " + department);
System.out.println("Email: " + email);
}

public static void main(String[] args) {


// Create a jagged array of Employee objects
Employee[][] employees = new Employee[5][];
Scanner scanner = new Scanner(System.in);

// Read details for each Employee object


for (int i = 0; i < employees.length; i++) {
System.out.println("\nEmployee Group " + (i + 1));
System.out.print("Enter the number of employees in group " + (i + 1) + ": ");
int numEmployees = scanner.nextInt();
employees[i] = new Employee[numEmployees];

for (int j = 0; j < numEmployees; j++) {


System.out.println("\nEmployee " + (j + 1));
System.out.print("Enter Employee ID: ");
int emp_id = scanner.nextInt();

System.out.print("Enter Employee Salary: ");


double salary = scanner.nextDouble();

System.out.print("Enter Employee Name: ");


scanner.nextLine(); // Consume the newline character
String name = scanner.nextLine();

System.out.print("Enter Employee Address: ");


String address = scanner.nextLine();

System.out.print("Enter Employee Department: ");


String department = scanner.nextLine();

System.out.print("Enter Employee Email: ");


String email = scanner.nextLine();

employees[i][j] = new Employee(emp_id, salary, name, address, department, email);


}
}

// Compute bonus and overtime for each Employee object and print details
for (int i = 0; i < employees.length; i++) {
for (int j = 0; j < employees[i].length; j++) {
System.out.println("\nEmployee Group " + (i + 1) + ", Employee " + (j + 1));
employees[i][j].computeEmployeeBonus();
employees[i][j].computeOvertime(45); // Assuming 45 hours worked for all employees
employees[i][j].printEmployeeDetails();
}
}

scanner.close();
}
}

You might also like