Wednesday, 9 October 2013

Program to demonstrate Vectors

//Program to demonstrate Vectors
//Program by Ahlam Ansari
import java.util.Vector;
import java.util.Scanner;
public class VectorExample
{
    public static void main(String[] ahlam)
    {   
    Scanner sc=new Scanner(System.in);
    System.out.print("\nEnter the no of commodities you want to buy");
    int n=sc.nextInt();
        Vector<String> vObj=new Vector<String>(n,3);

     String s;
    for(int i=0;i<n;i++)
    {
        System.out.print("\nEnter the "+(i+1)+"commodities you want to buy");
        s=sc.next();
        vObj.add(s);
    }
        // add vector element at index
    System.out.print("\nWhat position you want to enter the element");
    int pos=sc.nextInt();   
    System.out.print("\nEnter the name of commodity you want to insert");
    s=sc.next();   
    vObj.add(pos, s);

        // vObj.size() inform number of elements in Vector
        System.out.println("You have "+vObj.size()+" commodities in you shopping cart");

        // get elements of Vector
        for(int i=0;i<vObj.size();i++)
        {
            System.out.println("Your shopping list contains "+(i+1)+" :"+vObj.get(i));
        }
    }
}

/*
C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>javac VectorExample.j
ava

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>java VectorExample

Enter the no of commodities you want to buy 6

Enter the 1commodities you want to buy ClockworkSeries

Enter the 2commodities you want to buyMortalInstruments

Enter the 3commodities you want to buyNightShadesTrilogy

Enter the 4commodities you want to buyDivergentSeries

Enter the 5commodities you want to buyTheVampiresAcadmy

Enter the 6commodities you want to buyTheLastWitchOfAradia

What position you want to enter the element 6

Enter the name of commodity you want to insert 50ShadesofGrey
You have 7 commodities in you shopping cart
Your shopping list contains 1 :ClockworkSeries
Your shopping list contains 2 :MortalInstruments
Your shopping list contains 3 :NightShadesTrilogy
Your shopping list contains 4 :DivergentSeries
Your shopping list contains 5 :TheVampiresAcadmy
Your shopping list contains 6 :TheLastWitchOfAradia
Your shopping list contains 7 :50ShadesofGrey

C:\Users\Ahlam\Google Drive\My Lectures\Fall\OOPM\Programs>
*/

No comments:

Post a Comment