idastan97

CSCI152 L12 P2

Mar 2nd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////// Student Class
  2. public class Student implements Comparable<Student>{
  3.     private final int id;
  4.     private final String name;
  5.    
  6.     public Student(int i, String n){
  7.         id=i;
  8.         name=n;
  9.     }
  10.    
  11.     public int getId(){
  12.         return id;
  13.     }
  14.    
  15.     public String getName(){
  16.         return name;
  17.     }
  18.    
  19.     @Override
  20.     public int compareTo(Student s){
  21.         if (id>s.getId()){
  22.             return 1;
  23.         } else if (id<s.getId()){
  24.             return -1;
  25.         } else {
  26.             return 0;
  27.         }
  28.     }
  29.    
  30.     @Override
  31.     public String toString(){
  32.         return id+" "+name;
  33.     }
  34. }
  35.  
  36. ///////////////////////////////////////////////////////////////////////////////Test Class
  37. public class testClass {
  38.    
  39.     public static void main(String[] args){
  40.         SortedQueue<Student> mSQ=new LinkedListSortedQueue();
  41.         mSQ.insert(new Student(2, "q"));
  42.         mSQ.insert(new Student(4, "w"));
  43.         mSQ.insert(new Student(1, "d"));
  44.         mSQ.insert(new Student(3, "j"));
  45.         mSQ.insert(new Student(5, "o"));
  46.         System.out.println("Size: "+mSQ.getSize()+"\n"+mSQ+"\n");
  47.        
  48.         while (mSQ.getSize()>0){
  49.             try{
  50.                 System.out.println(mSQ.dequeue());
  51.             } catch (Exception ex){
  52.                
  53.             }
  54.         }
  55.        
  56.         try{
  57.             System.out.println(mSQ.dequeue());
  58.         } catch (Exception ex){
  59.             System.out.println(ex.getMessage());
  60.         }
  61.        
  62.        
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment