Apk31exp-5 OOPM
Apk31exp-5 OOPM
Apk31exp-5 OOPM
Grade: AA / AB / BB / BC / CC / CD /DD
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.
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.
CO2: Explore arrays, vectors, classes and objects in C++ and Java.
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;
We can access the data members simply by using the index of the element, just like we
access the elements in Arrays.
Vectors Constructors
Listed below are the multiple variations of vector constructors available to use:
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:
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.*;
} else {
return 0;
}
}
class Exp5 {
static Vector<Employee> employees = new Vector<Employee>();
break;
default:
System.out.println("Invalid choice. Try again.");
break;
}
} while (choice != 6);
sc.close();
}
Output:
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.
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
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");
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);
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");
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();