You are on page 1of 4

Experiment No.

9
Aim : Create a vector Student with their name. Write a program to add, remove and
display students name from vector.
Name of the Student : Anjali Charudatta Shinde
Class : SE
Sem : 3
Batch : C-2
Roll No. : C25
Div : C

Code :
import java.util.*;
import java.io.*;

class StudentDET{
Vector<String> vector = new Vector<String>();
public void Choice(int choice){
if(choice==1){
String name;
int max_stud=10;
int i;
System.out.print("\nEnter the name of student: ");
Scanner scan_name = new Scanner(System.in);
name = scan_name.next();
vector.add(name);
System.out.println(name + " has been added!");
}else if(choice==2){
String student;
if (vector.isEmpty()){
System.out.print("Vector is Empty!\n");
}else{
int del;
System.out.println(vector);
System.out.print("Which Student do you want to delete: (int)
");
Scanner scan_del = new Scanner(System.in);
del = scan_del.nextInt();
student = vector.get(del-1);
int max_stud=10;
if(del>0 && del<=10){
vector.removeElementAt(del-1);
}
System.out.println(student + " has been removed!");
}
}else if(choice==3){
if (vector.isEmpty()){
System.out.print("Vector is Empty! \n");
}else{
System.out.println(vector);
}
}else if(choice==4){
System.out.println("\nPROGRAM TERMINATED!");
}else{
System.out.println("Enter a valid choice!");
}
}
}
class Exp9{
public static void main(String[] args){
int choice;
StudentDET methods = new StudentDET();
System.out.println("\n====Vector Problem====");
do{
System.out.println("\n1. Add a Student");
System.out.println("2. Remove a Student");
System.out.println("3. Display all Students");
System.out.println("4. Quit\n");
System.out.print("Enter your choice: ");
Scanner scan = new Scanner(System.in);
choice = scan.nextInt();
if(choice==1){
methods.Choice(choice);
}else if(choice==2){
methods.Choice(choice);
}else if(choice==3){
methods.Choice(choice);
}else if(choice==4){
methods.Choice(choice);
}else{
methods.Choice(choice);
}
}while(choice!=4);
}
}
Input and Output screenshots :

You might also like