Collections and Iterators

 

// Student.java

 

import javax.swing.JOptionPane;

 

public class Student implements Comparable

{

    private String firstName;

    private String lastName;

    private String SID;

   

    public Student(String firstName, String lastName, String SID)

    {

        this.firstName = firstName;

        this.lastName = lastName;

        this.SID = SID;

    }

   

    public Student()

    {

        firstName = JOptionPane.showInputDialog("Enter first name");

        lastName = JOptionPane.showInputDialog("Enter last name");

        SID = JOptionPane.showInputDialog("Enter SID");

    }

   

    public String toString()

    {

        return "Name: " + firstName + " " + lastName +

               " SID: " + SID;

    }

   

    public String getFirstName()

    {

        return firstName;

    }

   

    public String getLastName()

    {

        return lastName;

    }

   

    public String getSID()

    {

        return SID;

    }

   

    public int compareTo(Object o)

    {

        Student s2 = (Student) o; 

        String s1Name = getLastName() + " " + getFirstName();

        String s2Name = s2.getLastName() + " " + s2.getFirstName();

        return s1Name.compareToIgnoreCase(s2Name);       

    }

}


// UndergradStudent.java

 

import javax.swing.JOptionPane;

 

public class UndergradStudent extends Student

{

    private String highschool;

   

    public UndergradStudent()

    {

        super();

        highschool = JOptionPane.showInputDialog("Enter high school");      

    }

   

    public UndergradStudent(String firstName, String lastName, String SID,

                       String highschool)

    {

        super(firstName, lastName, SID);

        this.highschool = highschool;

    }

   

    public String toString()

    {

        return super.toString() + " from " + highschool;

    }

}

 

 

// GradStudent.java

 

import javax.swing.JOptionPane;

 

public class GradStudent extends Student

{

    private String undergradDegree;

   

    public GradStudent()

    {

        super();

        undergradDegree = JOptionPane.showInputDialog("Enter degree");

    }

   

    public GradStudent(String firstName, String lastName, String SID,

                       String undergradDegree)

    {

        super(firstName, lastName, SID);

        this.undergradDegree = undergradDegree;

    }

   

    public String toString()

    {

        return super.toString() + " with degree of " + undergradDegree;

    }

}

 


 

// ex1.java

// array of Object

 

public class ex1

{

    public static void main(String[] args)

    {

        Student[] studentArray = new Student[4];

        studentArray[0] = new GradStudent("Mary", "Poppins", "11",

                                          "BS in CS");

        studentArray[1] = new GradStudent("Peter", "Pan", "22",

                                           "BS in Math");

        studentArray[2] = new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High");

        studentArray[3] = new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High");

        for (int i = 0; i < studentArray.length; i++)

        {

            System.out.println(studentArray[i]);

            // automatic call to toString()

        }

    }

}


// ex2.java

// ArrayList

 

import java.util.ArrayList;

 

public class ex2

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        for (int i = 0; i < students.size(); i++)

        {

            System.out.println(students.get(i));

            // automatic call to Object's toString()

        }

    }

}


// ex3.java

// Simple Iteration with ListIterator

 

import java.util.ArrayList;

import java.util.ListIterator;

 

public class ex3

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        ListIterator li;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        while (li.hasNext())

        {

            Student current = (Student)li.next();

            System.out.println(current);

        }

    }

}


// ex4.java    Use ListIterator

import java.util.ArrayList;

import java.util.ListIterator;

import javax.swing.JOptionPane;

 

public class ex4

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        // go to first Student

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            if (choice.equalsIgnoreCase("Next"))

            {

                if (li.hasNext())

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else

                    System.out.println("No more");

            }

            if (choice.equalsIgnoreCase("Previous"))

            {

                if (li.hasPrevious())

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

                else

                    System.out.println("No more");

            }

            // see what happens if you do Next, then Previous

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous");           

        }

    }

  }


// ex5.java

// catch Exceptions

 

import java.util.ArrayList;

import java.util.ListIterator;

import java.util.NoSuchElementException;

import javax.swing.JOptionPane;

 

public class ex5

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous");           

        }

    }

}


// ex6.java

// Add with List Iterator

 

import java.util.ArrayList;

import java.util.ListIterator;

import java.util.NoSuchElementException;

import javax.swing.JOptionPane;

 

public class ex6

{

    public static void main(String[] args)

    {

       

        ArrayList students = new ArrayList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                li.add(input);

                System.out.println(input + " added");

            }

 


            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                li.add(input);

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                 "Add Grad Student");        

        }

    }  

}


// ex7.java

// Remove with ListIterator

 

import java.util.ArrayList;

import java.util.ListIterator;

import java.util.NoSuchElementException;

import javax.swing.JOptionPane;

 

public class ex7

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

          

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student\n" +

                                             "Remove");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Remove"))

                {

                    li.remove();

                    System.out.println("Student removed");

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

 


            catch (IllegalStateException e)

            {

                System.out.println("No one available to remove");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                li.add(input);

                System.out.println(input + " added");

            }

            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                li.add(input);

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                 "Add Grad Student\n" +

                                                 "Remove");       

        }

    }

}


// ex8.java

// Add and remove with ArrayList

// Restart List Iterator

 

import java.util.ArrayList;

import java.util.ListIterator;

import java.util.NoSuchElementException;

import javax.swing.JOptionPane;

 

public class ex8

{

    public static void main(String[] args)

    {

        ArrayList students = new ArrayList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student\n" +

                                             "Remove First\n" +

                                             "Remove Last");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Remove First"))

                {

                    Student gone = (Student)students.remove(0);

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

 


                else if (choice.equalsIgnoreCase("Remove Last"))

                {

                    Student gone = (Student)students.remove(students.size()

                                                                - 1);

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            catch (IndexOutOfBoundsException e)

            {

                System.out.println("No one available to remove");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                students.add(students.size(), input);

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                students.add(students.size(), input);

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                 "Add Grad Student\n" +

                                                 "Remove First\n" +

                                                 "Remove Last");      

        }

    }

}


// ex9.java

// Use LinkedList class

 

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.NoSuchElementException;

import javax.swing.JOptionPane;

 

public class ex9

{

    public static void main(String[] args)

    {

        LinkedList students = new LinkedList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student\n" +

                                             "Remove First\n" +

                                             "Remove Last");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Remove First"))

                {

                    Student gone = (Student)students.removeFirst();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

 


                else if (choice.equalsIgnoreCase("Remove Last"))

                {

                    Student gone = (Student)students.removeLast();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            catch (IndexOutOfBoundsException e)

            {

                System.out.println("No one available to remove");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                students.addLast(input);

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                students.addLast(input);

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                 "Add Grad Student\n" +

                                                 "Remove First\n" +

                                                 "Remove Last");     

        }

    }

}


// StudentComparator.java

 

import java.util.Comparator;

 

public class StudentComparator implements Comparator

{

    public int compare(Object a, Object b)

    {

      Student s1 = (Student) a;

      Student s2 = (Student) b; 

      String s1Name = s1.getLastName() + " " + s1.getFirstName();

      String s2Name = s2.getLastName() + " " + s2.getFirstName();

      return s1Name.compareToIgnoreCase(s2Name);

    }

}

 

 

// ex10.java

// Use Comparator interface

 

import java.util.Comparator;

import java.util.LinkedList;

import java.util.ListIterator;

 

import java.util.NoSuchElementException;

 

import javax.swing.JOptionPane;

 

public class ex10

{

    public static void main(String[] args)

    {

        LinkedList students = new LinkedList();

        ListIterator li;

        Student current;

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student\n" +

                                             "Remove First\n" +

                                             "Remove Last");

 


       while (! choice.equalsIgnoreCase("Quit"))

       

       {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Remove First"))

                {

                    Student gone = (Student)students.removeFirst();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

                else if (choice.equalsIgnoreCase("Remove Last"))

                {

                    Student gone = (Student)students.removeLast();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            catch (IndexOutOfBoundsException e)

            {

                System.out.println("No one available to remove");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                add(students, input, new StudentComparator());

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                add(students, input, new StudentComparator());

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

 


            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                

                                                 "Add Grad Student\n" +

                                                 "Remove First\n" +

                                                 "Remove Last");     

        }

    }

   

    static private void add(LinkedList data, Object s, Comparator c)

    {

      int position = data.size();

      for (int i = 0; i < data.size(); i++)

      {

        if (c.compare(s, data.get(i)) < 0)

        {

          position = i;

          break;

        }

      }

      data.add(position, s);

    }    

}

 

 


// ex11.java

// Use Collections object to sort

 

import java.util.Collections;

import java.util.Comparator;

import java.util.LinkedList;

import java.util.ListIterator;

 

import java.util.NoSuchElementException;

 

import javax.swing.JOptionPane;

 

 

public class ex11

{

    public static void main(String[] args)

    {

        LinkedList students = new LinkedList();

        ListIterator li;

        Student current;

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        Collections.sort(students, new StudentComparator());

        li = students.listIterator();

        current = (Student) li.next();

        System.out.println(current);

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Next\n" +

                                             "Previous\n" +

                                             "Add Undergrad Student\n" +

                                             "Add Grad Student\n" +

                                             "Remove First\n" +

                                             "Remove Last");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            try

            {

                if (choice.equalsIgnoreCase("Next"))

                {

                    current = (Student) li.next();

                    System.out.println(current);

                }

                else if (choice.equalsIgnoreCase("Previous"))

                {

                    current = (Student) li.previous();

                    System.out.println(current);

                }

 


                else if (choice.equalsIgnoreCase("Remove First"))

                {

                    Student gone = (Student)students.removeFirst();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

                else if (choice.equalsIgnoreCase("Remove Last"))

                {

                    Student gone = (Student)students.removeLast();

                    li = students.listIterator();

                    // restart list iterator if add or remove

                    System.out.println(gone + " removed, Back to Start");

                }

            }

            catch (NoSuchElementException e)

            {

                System.out.println("No more");

            }

            catch (IndexOutOfBoundsException e)

            {

                System.out.println("No one available to remove");

            }

            if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                add(students, input, new StudentComparator());

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                add(students, input, new StudentComparator());

                li = students.listIterator();

                // restart list iterator if add or remove

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Next\n" +

                                                 "Previous\n" +

                                                 "Add Undergrad Student\n" +

                                                 "Add Grad Student\n" +

                                                 "Remove First\n" +

                                                 "Remove Last");     

        }

    }

   

    static private void add(LinkedList data, Object s, Comparator c)

    {

        data.add(s);

        Collections.sort(data, c);

    }    

}

 


// ex12.java

// Use other Collections methods

// Now Student uses its Comparable interface

// for a natural ordering

 

import java.util.Collections;

import java.util.LinkedList;

import java.util.ListIterator;

 

import javax.swing.JOptionPane;

 

public class ex12

{

    public static void main(String[] args)

    {

        LinkedList students = new LinkedList();

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        Collections.sort(students, new StudentComparator());

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Sort\n" +

                                             "Reverse\n" +

                                             "Shuffle");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            if (choice.equalsIgnoreCase("Sort"))

            {

                Collections.sort(students);

                print(students);

            }

            else if (choice.equalsIgnoreCase("Reverse"))

            {

                Collections.sort(students, Collections.reverseOrder());

                print(students);

            }

            else if (choice.equalsIgnoreCase("Shuffle"))

            {

                Collections.shuffle(students);

                print(students);

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Sort\n" +

                                                 "Reverse\n" +

                                                 "Shuffle");    

        }

    }

   

 


    static private void print(LinkedList data)

    {

        ListIterator li = data.listIterator();

        System.out.println();

        System.out.println();      

        while (li.hasNext())

        {

            System.out.println(li.next());

        }

    }    

}

 

 


// ex13.java

// Use TreeSet and Iterator

 

import java.util.Iterator;

import java.util.TreeSet;

import javax.swing.JOptionPane;

 

public class ex13

{

    public static void main(String[] args)

    {

        TreeSet students = new TreeSet();

        students.add( new GradStudent("Mary", "Poppins", "11", "BS in CS"));

        students.add( new GradStudent("Peter", "Pan", "22", "BS in Math"));

        students.add( new UndergradStudent("Wicked", "Stepmother", "33",

                                               "Monroe High"));

        students.add( new UndergradStudent("Prince", "Charming", "44",

                                               "Madison High"));

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Print\n" +

                                             "Add Grad Student\n" +

                                             "Add Undergrad Student");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            if (choice.equalsIgnoreCase("Print"))

            {

                print(students);

            }

            else if (choice.equalsIgnoreCase("Add Grad Student"))

            {

                GradStudent input = new GradStudent();

                students.add(input);

                System.out.println(input + " added");

            }

            else if (choice.equalsIgnoreCase("Add Undergrad Student"))

            {

                UndergradStudent input = new UndergradStudent();

                students.add(input);

                System.out.println(input + " added");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Print\n" +

                                                 "Add Grad Student\n" +

                                                 "Add Undergrad Student"); 

        }

    }

   

 


    static private void print(TreeSet data)

    {

        Iterator li = data.iterator();

        System.out.println();

        System.out.println();      

        while (li.hasNext())

        {

            System.out.println(li.next());

        }

    }    

}

 

 


// ex14.java

// Use TreeSet and Iterator

// contains, remove, add methods for TreeSet

 

import java.util.Iterator;

import java.util.TreeSet;

import javax.swing.JOptionPane;

 

public class ex14

{

    public static void main(String[] args)

    {

        TreeSet letters = new TreeSet();

        letters.add("A");

        letters.add("D");

        letters.add("B");

        letters.add("A");

        letters.add("D");

        letters.add("C");

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Print\n" +

                                             "Remove\n" +

                                             "Add");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            if (choice.equalsIgnoreCase("Print"))

            {

                print(letters);

            }

            else if (choice.equalsIgnoreCase("Add"))

            {

                String input = JOptionPane.showInputDialog("Enter letter");

                letters.add(input);

                System.out.println(input + " added");

            }

            else if (choice.equalsIgnoreCase("Remove"))

            {

                String input = JOptionPane.showInputDialog(

                                         "Enter letter to remove");

                if (letters.contains(input))

                {

                    letters.remove(input);

                    System.out.println(input + " removed");

                }

                else

                    System.out.println(input + " not found");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Print\n" +

                                                 "Remove\n" +

                                                 "Add");

        }

    }

   

 


    static private void print(TreeSet data)

    {

        Iterator li = data.iterator();

        System.out.println();

        System.out.println();      

        while (li.hasNext())

        {

            System.out.println(li.next());

        }

    }     

}

 

 


// ex15.java

// Use TreeMap, Set, and Iterator

 

import java.util.Iterator;

import java.util.Set;

import java.util.TreeMap;

import javax.swing.JOptionPane;

 

public class ex15

{

    public static void main(String[] args)

    {

        TreeMap teams = new TreeMap();

        teams.put("New York", "Yankees");

        teams.put("Boston", "Red Sox");

        teams.put("Toronto", "Blue Jays");

        teams.put("Baltimore", "Orioles");

        teams.put("Detroit", "Tigers");

        // put(key, value)

        String choice;

        choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                             "Quit\n" +

                                             "Print\n" +

                                             "Remove\n" +

                                             "Add");

        while (! choice.equalsIgnoreCase("Quit"))

        {

            if (choice.equalsIgnoreCase("Print"))

            {

                print(teams);

            }

            else if (choice.equalsIgnoreCase("Add"))

            {

                String city = JOptionPane.showInputDialog("Enter city");

                String team = JOptionPane.showInputDialog("Enter team");

                teams.put(city, team);

                System.out.println(team + " added");

            }

            else if (choice.equalsIgnoreCase("Remove"))

            {

                String city = JOptionPane.showInputDialog("Enter city");

                if (teams.containsKey(city))

                {

                    teams.remove(city);

                    System.out.println(city + " removed");

                }

                else

                    System.out.println(city + " not found");

            }

            choice = JOptionPane.showInputDialog("Enter your choice\n" +

                                                 "Quit\n" +

                                                 "Print\n" +

                                                 "Remove\n" +

                                                 "Add");

        }

    }

   

    static private void print(TreeMap data)

    {

        Set s = data.keySet();

        Iterator i = s.iterator();

        System.out.println();

        System.out.println();      

        while (i.hasNext())

        {

            String city = (String) i.next();

            System.out.println(city + " " + data.get(city));

        }

    }    

}