You are on page 1of 6

NAME- Megha Saxena

Registration Number- 20BIT0366


Java Programming
Lab- 33+34
Assignment 5

1. Develop a Java program to implement the following:


a. Create a class to represent an employee details like id, name, salary and dept.
b. Design methods to get input, print details.
c. Design a test class to create few objects belong to the above class and store them in an
arraylist.
d. Find count of employee in each department.
e. Finally print details of all employees and count of each department.
CODE:
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main


{
public static void main(String[] args)
{
Employee[] employees = {
new Employee("1928", "Megha", 51000, "IT"),
new Employee("1029", "Rahul", 71600, "IT"),
new Employee("1192", "Kaavya", 35187.5, "Sales"),
new Employee("2912", "Rishabh", 47100.77, "Marketing"),
new Employee("9182", "Lavanya", 62001, "IT"),
new Employee("9182", "Suzy", 32001, "Sales"),
new Employee("8391", "Bianca", 42361.4, "Marketing")};

List<Employee> list = Arrays.asList(employees);

// display all Employees


System.out.println("Complete Employee list:");
list.stream().forEach(System.out::println);

System.out.printf("%nCount of Employees by department:%n");


Map<String, Long> employeeCountByDepartment =
list.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
TreeMap::new, Collectors.counting()));
employeeCountByDepartment.forEach(
(department, count) -> System.out.printf(
"%s has %d employee(s)%n", department, count));

}
}
class Employee
{
private String firstName;
private String id;
private double salary;
private String department;

public Employee(String firstName, String id,


double salary, String department)
{
this.firstName = firstName;
this.id = id;
this.salary = salary;
this.department = department;
}

public void setFirstName(String firstName)


{
this.firstName = firstName;
}

public String getFirstName()


{
return firstName;
}

public void setLastName(String id)


{
this.id = id;
}

public String getid()


{
return id;
}

public void setSalary(double salary)


{
this.salary = salary;
}

public double getSalary()


{
return salary;
}

public void setDepartment(String department)


{
this.department = department;
}

public String getDepartment()


{
return department;
}

@Override
public String toString()
{
return String.format("%-8s %-8s %8.2f %s",
getFirstName(), getid(), getSalary(), getDepartment());
}
}
OUTPUT:

2. Design a java program to implement the queue data structure through array list along with
menu for insertion, deletion and traverse.
CODE:
class Queue {
private static int front, rear, capacity;
private static int queue[];

Queue(int c)
{
front = rear = 0;
capacity= c;
queue = new int[capacity];
}

// inserting an element
static void queueEnqueue(int data)
{
if (capacity == rear) {
System.out.printf("\n Queue is full \n");
return;
}

else {
queue[rear] = data;
rear++;
}
return;
}

// delete an element
static void queueDequeue()
{

if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}

// shift all the elements from index 2 till rear


else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}

// store 0 at rear indicating there's no element


if (rear < capacity)
queue[rear] = 0;

// decrement rear
rear--;
}
return;
}

// print queue elements


static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}

// traverse front to rear and print elements


for (i = front; i < rear; i++) {
System.out.printf(" %d ", queue[i]);
}
return;
}

// print front of queue


static void queueFront()
{
if (front == rear) {
System.out.printf("\nQueue is Empty\n");
return;
}
System.out.printf("\nFront Element is: %d", queue[front]);
return;
}
}

public class TestingQueue{

// Driver code
public static void main(String[] args)
{

Queue q = new Queue(10);

q.queueDisplay();

q.queueEnqueue(1);
q.queueEnqueue(2);
q.queueEnqueue(3);
q.queueEnqueue(4);
q.queueEnqueue(5);
q.queueEnqueue(6);
q.queueEnqueue(7);
q.queueEnqueue(8);
q.queueEnqueue(9);
q.queueEnqueue(10);

q.queueDisplay();

q.queueEnqueue(11);

q.queueDisplay();

q.queueDequeue();

q.queueDequeue();
System.out.printf("\n\nafter two node deletion\n\n");

q.queueDisplay();

q.queueFront();
}
}
OUTPUT:

You might also like