Java Program to Show the use of Methods of Vector Class

This is a Java Program to illustrate the Use of Methods of Vector Class

Problem Description

We have to write a program in Java such that it illustrates the use of methods of vector class using an applet.

Expected Input and Output

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.

advertisement
advertisement
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"
Problem Solution

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.

advertisement
Program/Source Code

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.

  1. /*Java Program to Illustrate the Use of Methods of Vector Class using Applet*/
  2. import java.util.*;
  3. import java.applet.*;
  4. import java.awt.event.*;
  5. import java.awt.*;
  6. import javax.swing.*;
  7. public class Vector_Class extends Applet implements ActionListener
  8. {
  9.     TextField num;
  10.     Label list,out;
  11.     Vector<Integer> vec;
  12.     //Function to initialize the applet
  13.     public void init()
  14.     {
  15. 	setBackground(Color.white);
  16. 	setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  17. 	//Create a vector
  18. 	vec = new Vector<>();
  19. 	vec.add(10);
  20. 	vec.add(20);
  21. 	vec.add(30);
  22. 	vec.add(40);		
  23.     }
  24.     //Function to set the features for applet
  25.     public void start()
  26.     {
  27. 	//Display the vector list
  28. 	list= new Label();
  29. 	list.setText("Vector List : "+vec);
  30. 	this.add(list);
  31. 	//Create a text field for number input
  32. 	num = new TextField(10);
  33. 	this.add(num);
  34. 	//Display the method options
  35. 	Button add = new Button("Add");
  36. 	Button del = new Button("Delete");
  37. 	Button search = new Button("Search");
  38. 	Button cap = new Button("Capacity");
  39. 	add.addActionListener(this);
  40. 	del.addActionListener(this);
  41. 	search.addActionListener(this);
  42. 	cap.addActionListener(this);
  43. 	this.add(add);
  44. 	this.add(del);
  45. 	this.add(search);
  46. 	this.add(cap);
  47. 	//Create the output field
  48. 	out = new Label();
  49. 	this.add(out);
  50.     }
  51.     //Function to perform the selected option
  52.     public void actionPerformed(ActionEvent e)
  53.     {
  54. 	String button = e.getActionCommand();
  55. 	int number = Integer.valueOf(num.getText()); 
  56. 	if(button.equals("Add"))
  57. 	{
  58. 	    if(vec.add(number))
  59. 		out.setText(number+" added successfully");
  60. 	    else
  61. 		out.setText(number+" could not be added");	
  62. 	}			
  63. 	else if(button.equals("Delete"))
  64. 	{
  65. 	    if(vec.removeElement(number))
  66. 		out.setText(number+ " deleted successfully");
  67. 	    else
  68. 		out.setText(number+ " could not be deleted");
  69. 	}
  70. 	else if(button.equals("Search"))
  71. 	{
  72. 	    if(vec.contains(number))
  73. 		out.setText(number+" found at index "+vec.indexOf(number));
  74. 	    else
  75. 		out.setText(number+" not found");
  76. 	}
  77. 	else
  78. 	{
  79. 	    out.setText("Size of vector is "+vec.size());
  80. 	}
  81. 	list.setText("Vector is : "+vec);
  82.     }
  83. }
  84. /*
  85. <applet code = Vector_Class.class width=500 height=500>
  86. </applet>
  87. */

To compile and run the applet use the following commands :

>>>javac Vector_Class.java
>>>appletviewer Vector_Class.java
Program Explanation

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).

advertisement
Runtime Test Cases

Here’s the run time test cases for illustrating the methods of vector class.

Test case 1 – To Add an Item to the Vector
java-applet-vector-add-1

Test case 2 – To Add an Item to the Vector
java-applet-vector-add-2

Test case 3 – To Delete an Item from the Vector
java-applet-vector-delete-1

Test case 4 – To Delete an Item from the Vector
java-applet-vector-delete-2

Test case 5 – To Search an Item in the Vector
java-applet-vector-search-1

Test case 6 – To Search an Item in the Vector
java-applet-vector-search-2

Test case 7 – To Get the Capacity of the Vector
java-applet-vector-capacity-1

Test case 8 – To Get the Capacity of the Vector
java-applet-vector-capacity-2

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.