Thursday, 28 March 2013

LAB 5 -exercise 1 - ArrayList -July 2013


Array List Exercise ( LAB 4 )
--------------------------------------------------------------------------------------------------------
1) Create 1 class and 2 subclasses of that class . Implement appropriate polymorphic method. the method should print out something.
2) Creating an ArrayList of the specific superclass of above
3) Add 5 Item/Element/object of superclass/subclasses into the above ArrayList
4) Check and print the size of ArrayList
5) Check and print all the item inside the array list.
6) Check Index of an Item in Java Arraylist
7) Retriev Item from arrayList in a loop ( without using iterator interface )
8) Check ArrayList for an Item
9) Check if ArrayList is Empty
.
10) Remove an Item from ArrayList
11) Copy data from one ArrayList to another ArrayList in Java
12) Replace an element at a particular index
13) Clear all data from ArrayList
14) Convert from ArrayList to Array in Java
15) Traverse ArrayList in Java using iterator

1 comment:


  1. /* Team
    * -------
    * teamname ;
    * teamposition ;
    * teampoint ;
    *
    * --------
    * win() ;
    * lost() ;
    * draw() ;
    *
    * EPLTeam
    * --------
    *
    * -------
    * win();
    * lost();
    * draws();
    *
    * MSLTeam
    * -------
    *
    * -------
    * win();
    * lost();
    * draws();
    *
    */

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.ListIterator;

    public class Football {

    public static void main (String args [] ) {

    System.out.println("Football Team");

    ArrayList teamlist = new ArrayList();

    Team epl1 = new EPLTeam("Man U");
    Team epl2 = new EPLTeam("Arsenal");
    Team epl3 = new EPLTeam("Cardiff");

    Team msl1 = new MSLTeam("Kelantan");
    Team msl2 = new MSLTeam("Selangor");

    teamlist.add(epl1);
    teamlist.add(0,epl2);
    teamlist.add(0,epl3);
    teamlist.add(1,msl1);
    teamlist.add(1,msl2);

    System.out.println(teamlist);
    System.out.println("Teamlist size : "+ teamlist.size());
    System.out.println("location of epl2 : "+ teamlist.indexOf(epl2) );

    System.out.println(teamlist);
    for ( int i =0 ; i < teamlist.size(); i++ ){

    System.out.println( teamlist.get(i).getname() );
    }

    System.out.println("Is it Empty ? : "+teamlist.isEmpty());
    System.out.println("Contain epl1 ? : "+teamlist.contains(epl1) );
    teamlist.remove(epl1);
    System.out.println("Contain epl1 ? : "+teamlist.contains(epl1) );
    System.out.println(teamlist);

    ArrayList list2 = new ArrayList();
    list2 = (ArrayList) teamlist.clone();
    System.out.println(teamlist);
    System.out.println(list2);
    teamlist.clear();
    System.out.println(teamlist);
    System.out.println(list2);
    // Remove an Item from ArrayList
    //Team[] rimi = new Team[ list2.size()];

    Object[] rimi = list2.toArray();

    Iterator listi ;

    listi = list2.iterator();

    while (listi.hasNext()){

    //list2.

    System.out.println( ((Team) listi.next()).getname() );
    System.out.println( ( listi.next()) );
    }

    // System.out.println("Array " +rimi);

    //epl1 =


    }


    }


    abstract class Team {

    String teamname ;
    int teamposition ;
    int teampoint ;

    public Team(String tname){
    teamname = tname;
    teamposition = 0 ;
    teampoint = 0;

    }

    public abstract void win() ;
    public abstract void lost() ;
    public abstract void draw() ;
    public String getname(){
    return teamname ;
    }
    }


    class EPLTeam extends Team {

    public EPLTeam(String tname ) {
    super(tname);
    }

    public void win() { teampoint = teampoint + 3; } ;
    public void lost(){ teampoint = teampoint ; } ;
    public void draw(){ teampoint = teampoint +1 ; } ;
    }

    class MSLTeam extends Team {

    public MSLTeam(String tname ) {
    super(tname);
    }

    public void win() { teampoint = teampoint + 5; } ;
    public void lost(){ teampoint = teampoint - 2 ; } ;
    public void draw(){ teampoint = teampoint + 3 ; } ;
    }

    ReplyDelete