Question

In: Computer Science

Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList...

Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList are two different classes that are used to create the assignment.

Solutions

Expert Solution

Employee Class Code:


public class Employee {
        //attributes
        private String empCode;
        private String fname;
        private String lname;
        
        //Constructor
        public Employee(String empCode,String fname,String lname)
        {
                this.empCode=empCode;
                this.fname=fname;
                this.lname=lname;
        }
        
        //getter methods
        public String getEmpCode() {
                return empCode;
        }
        public String getFname() {
                return fname;
        }
        public String getLname() {
                return lname;
        }
        
        //Setter methods
        public void setEmpCode(String empCode) {
                this.empCode = empCode;
        }
        public void setFname(String fname) {
                this.fname = fname;
        }
        public void setLname(String lname) {
                this.lname = lname;
        }
        
        //toString method
        @Override
        public String toString() {
                return "Employee [empCode=" + empCode + "\tFirst Name=" + fname + "\tLast Name=" + lname + "]";
        }
}

EmployeeList Class Code:

import java.util.LinkedList;

public class EmployeeList {
        // LinkedList to hold Employees
        LinkedList<Employee> employees;
        // to hold number of employees in list
        int noOfEmployees;

        // Constructor to create EmployeeList
        public EmployeeList() {
                this.employees = new LinkedList<Employee>();
                this.noOfEmployees = 0;
        }

        // Method add employee to list
        public void addEmployee(Employee e) {
                employees.add(e);
                this.noOfEmployees++;
        }

        // Method to remove employee from list
        public void removeEmployee(Employee e) {
                employees.remove(e);
                this.noOfEmployees--;
        }

        // method to get number of employees in list
        public int getNumberOfEmployees() {
                return this.noOfEmployees;
        }

        // method to get all employee details as string
        public String getAllEmployees() {
                String s = "";
                for (Employee e : employees) {
                        s += e + "\n";
                }
                return s;
        }

        // Main method
        public static void main(String[] args) {

                // Creating EmployeeList
                EmployeeList list = new EmployeeList();

                // Creating Employee Objects
                Employee e1 = new Employee("emp1", "kowlutla", "Magali");
                Employee e2 = new Employee("emp2", "Hindu", "Maruvada");
                Employee e3 = new Employee("emp3", "Krishna", "Kalluri");
                Employee e4 = new Employee("emp4", "Roopa", "Banavath");
                Employee e5 = new Employee("emp5", "Deepu", "Magali");
                Employee e6 = new Employee("emp6", "Aruna", "Boya");

                // Adding Employees to list using addEmployee method
                list.addEmployee(e1);
                list.addEmployee(e2);
                list.addEmployee(e3);
                list.addEmployee(e4);
                list.addEmployee(e5);
                list.addEmployee(e6);

                // printing number of employees in list
                                System.out.println("Number of employees in list: " + list.getNumberOfEmployees());
                // getting all employee details
                System.out.println("Employee Details:");
                System.out.println(list.getAllEmployees());

                // removing employee from list
                list.removeEmployee(e5);

                // printing number of employees in list
                System.out.println("Number of employees in list After removing one employee: " + list.getNumberOfEmployees());
                // getting all employee details
                System.out.println("Employee Details: ");
                System.out.println(list.getAllEmployees());

        }
}

Output Of Above Code:

Number of employees in list: 6
Employee Details:
Employee [empCode=emp1  First Name=kowlutla     Last Name=Magali]
Employee [empCode=emp2  First Name=Hindu        Last Name=Maruvada]
Employee [empCode=emp3  First Name=Krishna      Last Name=Kalluri]
Employee [empCode=emp4  First Name=Roopa        Last Name=Banavath]
Employee [empCode=emp5  First Name=Deepu        Last Name=Magali]
Employee [empCode=emp6  First Name=Aruna        Last Name=Boya]

Number of employees in list After removing one employee: 5
Employee Details: 
Employee [empCode=emp1  First Name=kowlutla     Last Name=Magali]
Employee [empCode=emp2  First Name=Hindu        Last Name=Maruvada]
Employee [empCode=emp3  First Name=Krishna      Last Name=Kalluri]
Employee [empCode=emp4  First Name=Roopa        Last Name=Banavath]
Employee [empCode=emp6  First Name=Aruna        Last Name=Boya]

Images Of Code:

Image Of Employee Class Code;

Images Of EmployeeList Class Code:

Image Of Output:


Related Solutions

Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList...
Is List type is an interface in the Java collections framework? Classes Vector, ArrayList, and LinkedList are the same data structure but implement data storage in different ways. Classes, that implement Map interface in Java collections framework are used for storing what type of data? Declare and instantiate a list of elements of type String. Name this list myArray. what type of data structure is Stack? LinkedList data structure in Java collections is implemented as doubly linked lists. In PriorityQueue...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from this list, followed by values from list1, and then followed by values from list2. For example, if E is Integer type and this list has (5,3,1), list1 has (8, 10,12,14), and list2 has (22,23,24,25,26) in that order, then the returned...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Write a Java class called Employee (Parts of the code is given below), which has three...
Write a Java class called Employee (Parts of the code is given below), which has three private fields firstName (String), lastName (String) and yearsEmployed (double). Implement accessor/mutator methods for the private fields and toString() method, that returns a String representation, which contains employee name and years she was employed. public class Employee { private String firstName; ... } Write a client program called EmployeeClient that creates an instance of Employee class, sets values for the fields (name: John Doe, yearsEmployed:...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT