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...
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 ( )...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT