Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Write a menu-driven progroam to insert and delete various objects in a vector of the specified size.*/
- import java.util.Vector;
- import java.util.Scanner;
- class Employee{
- int id;
- }
- class Student{
- int roll_no;
- }
- class Practice{
- public static void main(String[] args){
- Vector v = new Vector(20);
- Scanner sc = new Scanner(System.in);
- while(true){
- System.out.print("\nThe vector is ");
- if(v.size() == 0) System.out.print("Empty!");
- else for(int i = 0; i< v.size(); i++)
- System.out.print(" " + v.elementAt(i)+ " ");
- System.out.println("\n\n1. Insert 2. Delete 3. Exit");
- int choice = sc.nextInt();
- switch(choice){
- case 1: System.out.println("Insert a 1. Integer 2. Double 3. Float 4. String 5. Array of Strings 6. Employee Object 7. Student Object.");
- int type = sc.nextInt();
- switch(type){
- case 1: System.out.print("Enter the integer: ");
- v.addElement(new Integer(sc.nextInt())); break;
- case 2: System.out.print("Enter the double: ");
- v.addElement(new Double(sc.nextDouble())); break;
- case 3: System.out.print("Enter the float: ");
- v.addElement(new Float(sc.nextFloat())); break;
- case 4: System.out.print("Enter the String: ");
- v.addElement(sc.next()); break;
- case 5: System.out.print("Enter no. of strings: ");
- String[] string = new String[sc.nextInt()];
- for(String i : string) i = sc.next();
- for(String i : string) v.addElement(i); break;
- case 6: System.out.print("Enter the ID of employee: ");
- Employee em = new Employee();
- v.addElement(em.id = sc.nextInt()); break;
- case 7: System.out.print("Enter the roll no: ");
- Student st = new Student();
- v.addElement(st.roll_no = sc.nextInt()); break;
- default:System.out.println("Invalid choice!");
- } break;
- case 2: System.out.println("Enter the element's index: ");
- v.removeElementAt(sc.nextInt()); break;
- case 3: return;
- default: System.out.println("Wrong Choice!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement