In: Computer Science
1) Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide a 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, do not set its value.
2) Create an app named EmployeeLinkedList that stores a collection of Employee objects in a LinkedList<Employee>. Test the app by creating five Employee objects and adding the Employees to the LinkedList<Employee> , then re-access the LinkedList<Employee> to display each Employee object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.
Provide a working java code and also display the output. NEED IT ASAP
Code :
public class EmployeeLinkedList
{
Employee head; // head of list
/* Linked list Employee*/
class Employee
{
String fName;
String lName;
double data;
Employee next;
// Constructor
Employee(String fN, String lN, double d) {
fName = fN;
lName = lN;
data = d;
next = null;
}
}
/* Inserts a new Node at front of the list. */
public void push(String new_fN, String new_lN, double new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Employee new_node = new Employee(new_fN, new_lN, new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* This function raises each salary by 10% */
public void salRaise()
{
Employee tnode = head;
while (tnode != null)
{
tnode.data *= 1.1 ;
tnode = tnode.next;
}
}
/* This function prints contents of linked list starting from
the given node */
public void printList()
{
Employee tnode = head;
while (tnode != null)
{
System.out.print(tnode.fName + " " + tnode.lName + " ");
System.out.printf("%.2f\n", tnode.data);
tnode = tnode.next;
}
}
/* Driver program to test above functions. */
public static void main(String[] args)
{
/* Start with the empty list */
EmployeeLinkedList llist = new EmployeeLinkedList();
// Insert at the linked list
llist.push("Chandler", "Bing", 100000);
llist.push("Joey", "Tribbiani",200000);
llist.push("Ross", "Geller",300000);
llist.push("Pheobe", "Buffay",400000);
llist.push("Rachel", "Green",500000);
System.out.println("\n\nCreated Linked list is: ");
llist.printList();
System.out.println("\n\nGiving everyone a raise of 10%");
llist.salRaise();
System.out.println("\n\nUpdated Linked list is: ");
llist.printList();
}
}
Output Screenshot: