0
6.8kviews
Write a program to create Vector objects with student names. Program should perform the following operations based on the choice:

i. Add Student name: To add new student name in the Vector.

ii. Remove Student name: to remove the student name if already exists else display appropriate message.

iii. Display- Display contents of Vector.

Marks: 10 M

Year: May 2015

1 Answer
0
212views
import java.io.*;
import java.util.Vector;
import java.util.*;
class Vector1
{
public static void main(String args[])
{

int choice=0;
DataInputStream in=new DataInputStream(System.in);
Vector v = new Vector();
String s,city;
try
{
    do
    {
        System.out.println("Select your choice:");
            System.out.println("1 - Add Element");
        System.out.println("2 - Remove Element");
        System.out.println("3 - View Elements");
        System.out.println("4 - Exit");
        choice=Integer.parseInt(in.readLine());

        switch(choice)
        {
            case 1: 
                System.out.println("Enter city name:");
                city=in.readLine(); 
                if(v.contains(city))
                System.out.println("City  name already exists");
                else
                {
                    v.addElement(city);
                    System.out.println("City name added");
                }
                break;

            case 2 : if(v.isEmpty())
                System.out.println("City name list is empty");
                else
                {
                    System.out.println("Enter city name:");
                    city=in.readLine(); 
                if(v.contains(city))
                {
                    v.removeElement(city);
                    System.out.println("City name removed");
                }
                else
                System.out.println("City name does not exist");
                }
                break;

            case 3 : if(v.isEmpty())
                System.out.println("City name list is empty");
                else 
                System.out.println("Vector : "+v.toString());
                break;
            case 4 : System.exit(0);
                    break;  
        }//end of switch
        System.out.println("Do you want to continue? Press y for Yes or Press N for No");
        s=in.readLine();

    }while(s.equals("y"));//end of do 
}//end of try
catch(Exception e)
{
    System.out.println("Exception caught:"+e);
}
}//end of main()

}//end of class
Please log in to add an answer.