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

Write a complete Java code that contain two classes. A GradeBook and a GradeBookTester. - The...
Write a complete Java code that contain two classes. A GradeBook and a GradeBookTester. - The GradeBook class contains the followings: - An array of grades (integer array) declared as a field. - A constructor that takes an integer value as parameter (len), and creates the grades array of size equals to len. - A method called fillArray that fills the grades array with random integers. Acceptable values are between 1 and 100 (inclusive) - A method called printStatistics that...
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.
Using Doubly Linked List, create a java code that does the following Without using LinkedList from...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL. ( as a METHOD...
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...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer...
Write a Java program/method that takes a LinkedList and returns a new LinkedList with the integer values squared and reversed. Example: if the LinkedList has (9, 5,4,6), then the returned list will have (36, 16,25,81). What is the time-complexity of your code? You must use listIterator for full credit. public LinkedList getReverseSquaredList (LinkedList list) { }
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 Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT