Wednesday, September 4, 2019

Creating a Vector Student with their name.

Add, Remove & Display student name from Vector

Vector implements a dynamic array. It is similar to ArrayList, but with two differences −
·        Vector is synchronized.
·        Vector contains many legacy methods that are not part of the collections framework.
Vector proves to be very useful if you don't know the size of the array in advance or you just need one that can change sizes over the lifetime of a program.
Following is the list of constructors provided by the vector class.
Vector( )
n  creates a default vector, which has an initial size of 10.
Vector(int size)
n  creates a vector whose initial capacity is specified by size
Vector(int size, int incr)
n  creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward
Vector(Collection c)
creates a vector that contains the elements of collection c.
Vector defines three protected data member:
§  int capacityIncreament: Contains the increment value.
§  int elementCount: Number of elements currently in vector stored in it.
§  Object elementData[]: Array that holds the vector is stored in it.
Methods of Vector class
·         addElement(Object)
Adds the specified component to the end of this vector, increasing its size by one.
·         capacity()
Returns the current capacity of this vector.
·         clone()
Returns a clone of this vector.
·         contains(Object)
Tests if the specified object is a component in this vector.
·         copyInto(Object[])
Copies the components of this vector into the specified array.
·         elementAt(int)
Returns the component at the specified index.
·         elements()
Returns an enumeration of the components of this vector.
·         ensureCapacity(int)
Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.
·         firstElement()
Returns the first component of this vector.
·         indexOf(Object)
Searches for the first occurence of the given argument, testing for equality using the equals method.
·         indexOf(Object, int)
Searches for the first occurence of the given argument, beginning the search at index, and testing for equality using the equals method.
·         insertElementAt(Object, int)
Inserts the specified object as a component in this vector at the specified index.
·         isEmpty()
Tests if this vector has no components.
·         lastElement()
Returns the last component of the vector.
·         lastIndexOf(Object)
Returns the index of the last occurrence of the specified object in this vector.
·         lastIndexOf(Object, int)
Searches backwards for the specified object, starting from the specified index, and returns an index to it.
·         removeAllElements()
Removes all components from this vector and sets its size to zero.
·         removeElement(Object)
Removes the first occurrence of the argument from this vector.
·         removeElementAt(int)
Deletes the component at the specified index.
·         setElementAt(Object, int)
Sets the component at the specified index of this vector to be the specified object.
·         setSize(int)
Sets the size of this vector.
·         size()
Returns the number of components in this vector.
·         toString()
Returns a string representation of this vector.
·         trimToSize()
Trims the capacity of this vector to be the vector's current size.

Algorithm:
  1. start
  2. create an object of Vector class
  3. add five student names to Vector using addElement() method of Vector class
  4. select option from menu
  5. if option is , then add new student name to Vector using addElement() method of Vector class
  6. if option is 2, then remove specific element from Vector using removeElement() method of Vector class
  7. if option is 3, then display all Vector elements using Enumeration class methods nextElement() and hasMoreElements().
  8. stop

   Program :-

import java.util.Vector;
import java.util.*;

class c
{
public static void main(String[] args) throws Exception
{
Scanner s = new Scanner(System.in);
Vector  v=new Vector  ();
 int i,j=0,n;
do
{
System.out.println("enter choice"+"\n1. Add"+"\n2. Remove"+"\n3. Display" +"\n4. Exit");
n=s.nextInt();
switch(n)
{
case 1:System.out.println("enter name of student");
          v.add(j,s.next());
          System.out.println("==================\n");
        j++;
          for(i=0;i<v.size();i++)
          {
          System.out.println((i+1)+" "+v.get(i));
          }
          System.out.println("");   
          break;
case 2:System.out.println("enter the no of Student ");
          v.remove((s.nextInt()-1));
          System.out.println("==================\n");
       for(i=0;i<v.size();i++)
       {
       System.out.println((i+1)+" "+v.get(i));
       }
          System.out.println(""); 
       break;
case 3:System.out.println("==================\n");
          for(i=0;i<v.size();i++)
       {
       System.out.println((i+1)+" "+v.get(i));
       }
          System.out.println(""); 
       break;
}      
}while(n!=4);
}
}


Output:-

enter choice
1. Add
2. Remove
3. Display
4. Exit
1
enter name of student
Chetan
==================

1 Chetan

enter choice
1. Add
2. Remove
3. Display
4. Exit
1
enter name of student
Mayank
==================

1 Chetan
2 Mayank

enter choice
1. Add
2. Remove
3. Display
4. Exit
2
enter the no of Student
2
==================

1 Chetan

enter choice
1. Add
2. Remove
3. Display
4. Exit
==================


        Observations :-
Here we observed that how child class inherit  methods and properties of parent class.

Conclusion:
We implemented different types of Inheritance.


2 comments: