Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

K. J.

Somaiya College of Engineering, Mumbai-77

BatchA2AA Roll No.:1601221031


AAA2
Experiment / assignment / tutorial No.05

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE :Vector

AIM: Create a class Employee which stores E-Name, E-Id and E-Salary of an
Employee. Use class Vector to maintain an array of Employee with respect to the E-
Salary. Provide the following functions
1) Create (): this function will accept the n Employee records in any order and will
arrange them in the sorted order.
2) Insert (): to insert the given Employee record at appropriate index in the vector
depending upon the E-Salary.
3) delete ByE-name( ): to accept the name of the Employee and delete the record
having given name
4) deleteByE-Id ( ): to accept the Id of the Employee and delete the record having given
E-Id.

Provide the following functions

1) boolean add(E e) : This method appends the specified element to the end of this
Vector.

2) void addElement(E obj) This method adds the specified component to the end
of this vector, increasing its size by one.

3) int lastIndexOf(Object o, int index) This method returns the index of the last
occurrence of the specified element in this vector, searching backwards from index, or
returns -1 if the element is not found.

4) void removeElementAt(int index)This method deletes the component at the


specified index.

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

Expected OUTCOME of Experiment:

CO2: Explore arrays, vectors, classes and objects in C++ and Java.

Books/ Journals/ Websites referred:


1. Ralph Bravaco , Shai Simoson , “Java Programing From the Group Up” Tata
McGraw-Hill.

2.Grady Booch, Object Oriented Analysis and Design .

Pre Lab/ Prior Concepts:

Vectors in Java are one of the most commonly used data structures. Similar to Arrays
data structures which hold the data in a linear fashion. Vectors also store the data in a
linear fashion, but unlike Arrays, they do not have a fixed size. Instead, their size can
be increased on demand.
Vector class is a child class of AbstractList class and implements on List interface. To
use Vectors, we first have to import Vector class from java.util package:
import java.util.Vector;

Access Elements in Vector:

We can access the data members simply by using the index of the element, just like we
access the elements in Arrays.

Example- If we want to access the third element in a vector v, we simply refer to it as


v[3].

Vectors Constructors

Listed below are the multiple variations of vector constructors available to use:

1. Vector(int initialCapacity, int Increment) – Constructs a vector with given


initialCapacity and its Increment in size.

2. Vector(int initialCapacity) – Constructs an empty vector with given


initialCapacity. In this case, Increment is zero.

3. Vector() – Constructs a default vector of capacity 10.

4. Vector(Collection c) – Constructs a vector with a given collection, the order of


the elements is same as returned by the collection’s iterator.

There are also three protected parameters in vectors

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

● Int capacityIncrement()- It automatically increases the capacity of the vector


when the size becomes greater than capacity.
● Int elementCount() – tell number of elements in the vector
● Object[] elementData() – array in which elements of vector are stored

Memory allocation of vectors:

Vectors do not have a fixed size, instead, they have the ability to change their size
dynamically. One might think that the vectors allocate indefinite long space to store
objects. But this is not the case. Vectors can change their size based on two fields
‘capacity’ and ‘capacityIncrement’. Initially, a size equal to ‘capacity’ field is allocated
when a vector is declared. We can insert the elements equal to the capacity. But as soon
as the next element is inserted, it increases the size of the array by size
‘capacityIncrement’. Hence, it is able to change its size dynamically.
For a default constructor, the capacity is doubled whenever the capacity is full and a
new element is to be inserted.

Methods of Vectors :

● Adding elements
● Removing elements
● Changing elements
● Iterating the vector

Algorithm:

1. Define the Employee class with instance variables for E-Name, E-Id, and E-
Salary, and implement the Comparable interface to compare Employee
objects based on their salary.
2. Define the main class with a static Vector to hold Employee objects, and a
Scanner object to read user input.
3. Implement the following methods:
a. create() method:
i. Prompt the user to enter the number of Employee records to be created.
ii. Loop through the number of Employee records and prompt the user to enter
the details of each Employee object (name, id, and salary).
iii. Create a new Employee object for each record and add it to the Vector.
iv. Sort the Vector using Collections.sort() method.
b. insert() method:

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

i. Prompt the user to enter the details of the new Employee object (name, id, and
salary).
ii. Create a new Employee object with the entered details.
iii. Use the binarySearch() method of Collections class to get the appropriate
index to insert the new Employee object based on its salary.
iv. Insert the new Employee object at the index using insertElementAt() method
of Vector class.
c. deleteByEname() method:
i. Prompt the user to enter the name of the Employee object to be deleted.
ii. Loop through the Vector to find the Employee object with the given name.
iii. If found, remove the Employee object using removeElementAt() method
of Vector class.
d. deleteByEid() method:
i. Prompt the user to enter the id of the Employee object to be deleted.
ii. Loop through the Vector to find the Employee object with the given id.
iii. If found, remove the Employee object using removeElementAt() method
of Vector class.
4. In the main() method, implement a do-while loop to display a menu of options
to the user and call the appropriate method based on the user's choice.
5. Exit the program when the user selects the "Exit" option.

Implementation details:

import java.util.*;

class Employee implements Comparable<Employee> {


String name;
int id;
double salary;

Employee(String name, int id, double salary) {


this.name = name;
this.id = id;
this.salary = salary;
}

public int compareTo(Employee e) {


if (salary < e.salary) {
return -1;
} else if (salary > e.salary) {
return 1;

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

} else {
return 0;
}
}

public String toString() {


return "Employee " + name + " with ID " + id + " has a salary of
" + salary;
}
}

class Exp5 {
static Vector<Employee> employees = new Vector<Employee>();

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n1. Create employee records");
System.out.println("2. Insert employee record");
System.out.println("3. Delete employee record by name");
System.out.println("4. Delete employee record by ID");
System.out.println("5. Display employee records");
System.out.println("6. Exit"); System.out.println("\
nEnter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
createEmployees();
break;
case 2:
insertEmployee();
break;
case 3:
deleteEmployeeByName();
break;
case 4:
deleteEmployeeByID();
break;
case 5:
displayEmployees();
break;
case 6:
System.exit(0);

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

break;
default:
System.out.println("Invalid choice. Try again.");
break;
}
} while (choice != 6);
sc.close();
}

static void createEmployees() {


Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of employees: ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("\nEnter details of employee " + (i+1) +
":");
System.out.print("Name: ");
String name = sc.next();
System.out.print("ID: ");
int id = sc.nextInt();
System.out.print("Salary: ");
double salary = sc.nextDouble();
employees.add(new Employee(name, id, salary));
}
Collections.sort(employees);
}

static void insertEmployee() {


Scanner sc = new Scanner(System.in);
System.out.println("\nEnter details of new employee:");
System.out.print("Name: ");
String name = sc.next();
System.out.print("ID: ");
int id = sc.nextInt();
System.out.print("Salary: ");
double salary = sc.nextDouble();
Employee newEmployee = new Employee(name, id, salary);
int index = Collections.binarySearch(employees, newEmployee);
if (index < 0) {
index = -index - 1;
}
employees.insertElementAt(newEmployee, index);
}

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

static void deleteEmployeeByName() {


Scanner sc = new Scanner(System.in);
System.out.print("\nEnter name of employee to be deleted: ");
String name = sc.next();
int index = -1;
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).name.equals(name)) {
index = i;
break;
}
}
if (index >= 0) {
employees.removeElementAt(index);
System.out.println("Employee " + name + "'s record has been
deleted.");
} else {
System.out.println("Employee " + name + "'s record is not
found.");
}
}

static void deleteEmployeeByID() {


Scanner sc = new Scanner(System.in);
System.out.print("\nEnter id of employee to be deleted: ");
int id = sc.nextInt();
int index = -1;
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).id == id) {
index = i;
break;
}
}
if (index >= 0) {
Employee deletedEmployee = employees.remove(index);
System.out.println("Employee " + deletedEmployee.name + "
with ID " + deletedEmployee.id + "'s record has been deleted");
} else {
System.out.println("Employee with " + id + "'s record is not
found.");
}
}

static void displayEmployees() {


if (employees.isEmpty()) {

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

System.out.println("\nNo employee records to display.");


} else {
System.out.println("\nEmployee records:");
for (Employee emp : employees) {
System.out.println(emp);
}
}
}
}

Output:

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

Conclusion:

Program for storing employee details such as name, id and salary using Vector to
maintain the array and functionalities such as create, insert, delete by name and delete
by id was implemented in Java.

Date: Signature of faculty in-charge

Post Lab Descriptive Questions

1) What is the output of the following Program

import java.util.*;
class demo2 {
public static void main(String[] args)
{
Vector v = new Vector(20);
v.addElement("Geeksforgeeks");
v.insertElementAt("Java", 2);
System.out.println(v.firstElement());
}
}
Output:

java.lang.ArrayIndexOutOfBoundsException: 2 > 1

2) Expain any 10 methods of Vector class in detail with the help of

example Ans:-

1. add(E e):-
This method is used to append the specified element to the end of a vector. Here, the
parameter e is the element to be added to the vector.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

2. addElement(E obj):-
This method is used to add the specified component to the end of a vector, increasing
its size by one. Here, the parameter obj is the component to be added to the vector.
Example:-
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
vector.addElement(30);

3. get(int index):-
This method is used to get the element at the specified index in a vector. Here, the
parameter index is the index of the element to be retrieved.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
String fruit = vector.get(1);

4. set(int index, E element):-


This method is used to set the element at the specified index in a vector to the specified
element. Here, the parameter index is the index of the element to be set, and element is
the new value of the element.
Example:-
Vector<Integer> vector = new Vector<>();
vector.add(10);
vector.add(20);
vector.add(30);
vector.set(1, 25);

5. remove(int index):-
This method is used to remove the element at the specified index in a vector. Here, the
parameter index is the index of the element to be removed.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

vector.remove(1);

6. removeElement(Object obj):
This method is used to remove the first occurrence of the specified element from a
vector. Here, the parameter obj is the element to be removed.
Example:-
Vector<Integer> vector = new Vector<>();
vector.add(10);
vector.add(20);
vector.add(30);
vector.removeElement(20);

7. clear():-
This method is used to remove all elements from a vector.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
vector.clear();

8. indexOf(Object o):-
This method is used to get the index of the first occurrence of the specified element in a
vector. Here, the parameter o is the element to be searched for.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
int index = vector.indexOf("Banana");

9. size():-
This method is used to get the number of elements in a vector.
Example:-
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
int size = vector.size();

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022


K. J. Somaiya College of Engineering, Mumbai-77

10. contains(Object o):


This method is used to check if a vector contains the specified element. Here, the
parameter o is the element to be searched for.
Example:-
Vector<Integer> vector = new Vector<>();
vector.add(10);
vector.add(20);
vector.add(30);
boolean contains = vector.contains(20);

Department of Computer Engineering

Page No OOPM Sem III/August - Nov 2022

You might also like