This is a Java Program to illustrate the Use of Methods of Vector Class
We have to write a program in Java such that it illustrates the use of methods of vector class using an applet.
For illustrating methods of vector class, we can have the following different sets of input and output.
1. To Add an Item to the Vector List:
When the user enters any item and selects the Add option, then it is expected that the item gets added to the vector list. For example, the vector list is [10, 20, 30, 40] If you want to add a new item "15". It is excepted that 15 will be updated in vector list like [10, 20, 30, 40, 15] If you want to add a new item "35". It is excepted that 15 will be updated in vector list like [10, 20, 30, 40, 15, 35]
2. To Delete an Item from the Vector List: When the item is present is the vector.
When the user enters any item and selects the Delete option, then it is expected that the item gets deleted from the vector list. For example, the vector list is [10, 20, 30, 40, 15, 35] Suppose if you want to delete "40" from the list. It will delete and display message like "deleted successfully"
3. To Delete an Item from the Vector List: When the item is not present in the vector.
When the user enters any item and selects the Delete option, then it is expected that appropriate error message is displayed. For example, the vector list is [10, 20, 30, 15, 35] Suppose if you want to delete "200" from the list. If the number is not on the list. It displays a message like "could not be deleted"
4. To Search an Item in the Vector List: When the item is present is the vector.
When the user enters any item and selects the Search option, then it is expected that the index of item is displayed. For example, the vector list is [10, 20, 30, 15, 35] Suppose if you want to search the "15" element from the list. Then excepted output will be "15 found at index 3"
5. To Search an Item in the Vector List: When the item is not present in the vector.
When the user enters any item and selects the Search option, then it is expected that appropriate error message is displayed. For example, the vector list is [10, 20, 30, 15, 35] Suppose if you want to search the "175" element from the list. If the number is not on the list. It displays a message like "175 not found"
6. To Get the Capacity of the Vector List:
When the user selects the Capacity option, then it is expected that the size of the vector list is diplayed. For example, the vector list is [10, 20, 30, 15, 35] Then excepted output will be "size of vector is 5"
1. Create a vector and add few items to it.
2. Create two text field for displaying the vector and getting input from user respectively .
3. Create buttons for options – Add, Delete, Search and Capacity and add it to the frame.
4. Create a output text field.
5. Perform the operations using methods of the Vector class.
Here is source code of the Java Program to illustrate the use of methods of vector class using applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/*Java Program to Illustrate the Use of Methods of Vector Class using Applet*/
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Vector_Class extends Applet implements ActionListener
{
TextField num;
Label list,out;
Vector<Integer> vec;
//Function to initialize the applet
public void init()
{
setBackground(Color.white);
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
//Create a vector
vec = new Vector<>();
vec.add(10);
vec.add(20);
vec.add(30);
vec.add(40);
}
//Function to set the features for applet
public void start()
{
//Display the vector list
list= new Label();
list.setText("Vector List : "+vec);
this.add(list);
//Create a text field for number input
num = new TextField(10);
this.add(num);
//Display the method options
Button add = new Button("Add");
Button del = new Button("Delete");
Button search = new Button("Search");
Button cap = new Button("Capacity");
add.addActionListener(this);
del.addActionListener(this);
search.addActionListener(this);
cap.addActionListener(this);
this.add(add);
this.add(del);
this.add(search);
this.add(cap);
//Create the output field
out = new Label();
this.add(out);
}
//Function to perform the selected option
public void actionPerformed(ActionEvent e)
{
String button = e.getActionCommand();
int number = Integer.valueOf(num.getText());
if(button.equals("Add"))
{
if(vec.add(number))
out.setText(number+" added successfully");
else
out.setText(number+" could not be added");
}
else if(button.equals("Delete"))
{
if(vec.removeElement(number))
out.setText(number+ " deleted successfully");
else
out.setText(number+ " could not be deleted");
}
else if(button.equals("Search"))
{
if(vec.contains(number))
out.setText(number+" found at index "+vec.indexOf(number));
else
out.setText(number+" not found");
}
else
{
out.setText("Size of vector is "+vec.size());
}
list.setText("Vector is : "+vec);
}
}
/*
<applet code = Vector_Class.class width=500 height=500>
</applet>
*/
To compile and run the applet use the following commands :
>>>javac Vector_Class.java >>>appletviewer Vector_Class.java
1. To create a vector use Vector and specify the type of data it contains.
2. To add an item to the vector use (Vector Name).add(item).
3. To delete an item from the vector use (Vector Name).removeElement(item).
4. To search an item in the vector use (Vector Name).contains(item).
5. To get the capacity of vector use (Vector Name).size(). This function returns the size of the vector.
6. To get the index of an item in the vector use (Vector Name).indexOf(item).
Here’s the run time test cases for illustrating the methods of vector class.
Test case 1 – To Add an Item to the Vector
Test case 2 – To Add an Item to the Vector
Test case 3 – To Delete an Item from the Vector
Test case 4 – To Delete an Item from the Vector
Test case 5 – To Search an Item in the Vector
Test case 6 – To Search an Item in the Vector
Test case 7 – To Get the Capacity of the Vector
Test case 8 – To Get the Capacity of the Vector
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books
- Check Programming Books
- Apply for Computer Science Internship