JAVA
Write a comment on each method
1.Write a class Employee that includes the following hidden (private) attributes: name (String), department (String) and salary (double). Create a setter and a getter for each of them. The setter of salary is override by another one that gets bonus parameter (as percentage). The class includes a static and public member count (int) that represents the number of created Employee objects that incremented automatically after each created one then create a getter for it.
2.Crate main classes that achieve the
following:
a. Create an array of 10 Employee objects, created
permanently, read by Scanner and read by JOptionPane, (one program
for each).
b. Access the objects for bonus using the formula:
salary = salary * bonus (e.g. bonus = 1.2)
c. Sort the objects by name and salary, (one
program for each)
d. Show the sorted Employee data using Command
Line, JOptionPane, (one program for each).
e. Access the number of objects by the count
member
Employee.java
package HomeworkLib3;
public class Employee {
private String name;
private String department;
private double salary;
static int count;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setDepartment(String dept){
department = dept;
}
public String getDepartment(){
return department;
}
public void setSalary(double salary){
this.salary = salary;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary, double
bonus){
this.salary = salary*bonus;
}
public static int getCount(){
return count;
}
}
MainSName.java
package HomeworkLib3;
import java.util.Scanner;
public class MainSName {
public static void main(String args[]){
Employee emp[] = new
Employee[10];
for(int i=0;i<10;i++)
emp[i] = new
Employee();
// reading from console
Scanner sc = new
Scanner(System.in);
for(int i=0;i<10;i++){
System.out.println("Enter employee name");
emp[i].setName(sc.nextLine());
System.out.println("Enter department");
emp[i].setDepartment(sc.nextLine());
System.out.println("Enter Salary");
emp[i].setSalary(Double.parseDouble(sc.nextLine()));
}
//accessing salary with bonus
for(int i=0;i<10;i++)
emp[i].setSalary(emp[i].getSalary(), i);
// sorting with name
for(int i=0;i<10;i++){
for(int
j=0;j<(9-i);j++){
if(emp[j].getName().compareToIgnoreCase(emp[j+1].getName()) >
0){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
// Printing all employees
details
for(int i=0;i<10;i++){
System.out.println("Employee "+i+":::");
System.out.println("Name "+emp[i].getName());
System.out.println("Department "+emp[i].getDepartment());
System.out.println("Salary "+emp[i].getSalary());
}
System.out.println("No.of employees
"+Employee.count);
}
}
MainSSalary.java
package HomeworkLib3;
import java.util.Scanner;
public class MainSSalary {
public static void main(String args[]){
Employee emp[] = new
Employee[10];
for(int i=0;i<10;i++){
emp[i] = new
Employee();
Employee.count++;
}
//reading from console
Scanner sc = new
Scanner(System.in);
for(int i=0;i<10;i++){
System.out.println("Enter employee name");
emp[i].setName(sc.nextLine());
System.out.println("Enter department");
emp[i].setDepartment(sc.nextLine());
System.out.println("Enter Salary");
emp[i].setSalary(Double.parseDouble(sc.nextLine()));
}
//accessing salary with bonus
for(int i=0;i<10;i++)
emp[i].setSalary(emp[i].getSalary(), i);
//sorting with salary
for(int i=0;i<10;i++){
for(int
j=0;j<(9-i);j++){
if(emp[j].getSalary() >
emp[j+1].getSalary()){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
//printing all the employee
details
for(int i=0;i<10;i++){
System.out.println("Employee "+i+":::");
System.out.println("Name "+emp[i].getName());
System.out.println("Department "+emp[i].getDepartment());
System.out.println("Salary "+emp[i].getSalary());
}
System.out.println("No.of employees
"+Employee.count);
}
}
MainOptionPane1.java
package HomeworkLib3;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MainOptionPane1 {
static JFrame f = new JFrame();
public static void main(String args[]){
Employee emp[] = new
Employee[10];
for(int i=0;i<10;i++){
emp[i] = new
Employee();
Employee.count++;
}
for(int i=0;i<10;i++){
emp[i].setName(JOptionPane.showInputDialog("Employee Name"));
emp[i].setDepartment(JOptionPane.showInputDialog("Employee
Department"));
emp[i].setSalary(Double.parseDouble(JOptionPane.showInputDialog("Employee
Salary")));
}
for(int i=0;i<10;i++)
emp[i].setSalary(emp[i].getSalary(), i);
for(int i=0;i<10;i++){
for(int
j=0;j<(9-i);j++){
if(emp[j].getName().compareToIgnoreCase(emp[j+1].getName()) >
0){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
String msg="";
for(int i=0;i<10;i++){
msg+="Name
"+emp[i].getName()+" Department:"+emp[i].getDepartment()+"
Salary:"+emp[i].getSalary()+"\n";
}
JOptionPane.showMessageDialog(f,
msg);
JOptionPane.showMessageDialog(f,
"No. of Employees "+Employee.count);
}
}
MainOptionPane2.java
package HomeworkLib3;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MainOptionPane2 {
static JFrame f;
public static void main(String args[]){
f = new JFrame();
Employee emp[] = new
Employee[10];
for(int i=0;i<10;i++){
emp[i] = new
Employee();
Employee.count++;
}
for(int i=0;i<10;i++){
emp[i].setName(JOptionPane.showInputDialog("Employee Name"));
emp[i].setDepartment(JOptionPane.showInputDialog("Employee
Department"));
emp[i].setSalary(Double.parseDouble(JOptionPane.showInputDialog("Employee
Salary")));
}
for(int i=0;i<10;i++)
emp[i].setSalary(emp[i].getSalary(), i);
for(int i=0;i<10;i++){
for(int
j=0;j<(9-i);j++){
if(emp[j].getSalary() >
emp[j+1].getSalary()){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
String msg="";
for(int i=0;i<10;i++){
msg+="Name
"+emp[i].getName()+" Department:"+emp[i].getDepartment()+"
Salary:"+emp[i].getSalary()+"\n";
}
JOptionPane.showMessageDialog(f,
msg);
JOptionPane.showMessageDialog(f,
"No. of Employees "+Employee.count);
}
}
MainSName.java --> Console reading & Sorting with Name
MainSSalary.java --> Console reading & Sorting with Salary
MainOptionPane1 --> reading via JOptionPane & Sorting with Name
MainOptionPane2 --> reading via JOptionPane & Sorting with Salary
JAVA Write a comment on each method 1.Write a class Employee that includes the following hidden...