User Tools

Site Tools


java:anon_inner_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java:anon_inner_classes [2021/04/12 06:14] – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanosjava:anon_inner_classes [2023/03/23 20:43] (current) – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanos
Line 12: Line 12:
 ===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface ===== ===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface =====
  
-Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, **lastName**, **AEM**. Στη μέθοδο **//main//** ορίζουμε μία λίστα (ArrayList) από δεδομένα τύπου **Student** τα οποία διαβάζουμε από το αρχείο students.txt. Θέλουμε να ταξινομήσουμε τα δεδομένα της λίστας κατά αύξουσα σειρά με δύο διαφορετικούς τρόπους α)με βάση τη σειρά επώνυμο/όνομα/ΑΕΜ και β) με βάση τη σειρά ΑΕΜ/επώνυμο/όνομα. Η ταξινόμηση των στοιχείων μίας λίστας μπορεί να γίνει με τη βοήθεια της στατικής μεθόδου [[https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)|Collections.sort(java.util.List, java.util.Comparator)]], όπου παρέχεται ένας **συγκριτής** (//comparator//) για την σύγκριση των στοιχείων της λίστας. Η σύγκριση έχει υλοποιηθεί με δύο ανώνυμες κλάσεις που υλοποιούν και οι δύο το //interface// [[https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html|java.util.Comparator]]. +Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, **lastName**, **AEM**. Θέλουμε να ταξινομήσουμε τα δεδομένα της λίστας κατά αύξουσα σειρά με δύο διαφορετικούς τρόπους α) με βάση τη σειρά επώνυμο/όνομα/ΑΕΜ και β) με βάση τη σειρά ΑΕΜ/επώνυμο/όνομα. Η ταξινόμηση των στοιχείων μίας λίστας μπορεί να γίνει με τη βοήθεια της στατικής μεθόδου [[https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)|Collections.sort(java.util.List, java.util.Comparator)]], όπου παρέχεται ένας **συγκριτής** (//comparator//) για την σύγκριση των στοιχείων της λίστας. Η σύγκριση έχει υλοποιηθεί με δύο ανώνυμες κλάσεις που υλοποιούν και οι δύο το //interface// [[https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html|java.util.Comparator]].
- +
-<code txt students.txt> +
-4596 Super Man +
-5819 Peter Pan +
-1694 Susan Brown +
-7895 Lena Smith +
-3256 Oscar Gonzales +
-1243 Edgar Wallace +
-3265 Kate Gordon +
-4377 Leda Evans +
-1118 David Lerroy +
-8744 Amelia Dray +
-</code>+
  
 <code java Student.java> <code java Student.java>
Line 67: Line 54:
     Collections.sort(students, new Comparator<Student>() {     Collections.sort(students, new Comparator<Student>() {
         public int compare(Student st1, Student st2) {         public int compare(Student st1, Student st2) {
-          int cmp = cmp = st1.AEM - st2.AEM;+          int cmp = st1.AEM - st2.AEM;
           if(cmp==0)           if(cmp==0)
             cmp = st1.lastName.compareTo(st2.lastName);                         cmp = st1.lastName.compareTo(st2.lastName);            
Line 76: Line 63:
       }       }
     );     );
 +  }
 +}
 +</code>
 +
 +Στη μέθοδο **//main//** διαβάζουμε από το αρχείο **students.txt** και καταχωρούμε το περιεχόμενο του σε μία λίστα (**ArrayList<Student>**). Στη συνέχεια εκτυπώνουμε τα στοιχεία της λίστας
 +  * με τη σειρά που τα διαβάσαμε
 +  * με λεξικογραφική σειρά.
 +  * με βάση το ΑΕΜ
 +
 +<code txt students.txt>
 +4596 Super Man
 +5819 Peter Pan
 +1694 Susan Brown
 +7895 Lena Smith
 +3256 Oscar Gonzales
 +1243 Edgar Wallace
 +3265 Kate Gordon
 +4377 Leda Evans
 +1118 David Lerroy
 +8744 Amelia Dray
 +</code>
 +
 +<code java SortListOfStudents.java>
 +import java.io.*;
 +import java.util.*;
 +
 +public class SortListOfStudents {
 +  
 +  public static void readFromFile(List<Student> students, File f) {    
 +    try(Scanner sc = new Scanner(f)) {
 +      while(sc.hasNextLine()) {        
 +        students.add( new Student(sc.nextInt(), sc.next(), sc.next()) );
 +      }
 +    }
 +    catch(FileNotFoundException ex) {
 +      System.out.println("File \""+f.getName()+"\" was not found!");
 +    }
 +    catch(InputMismatchException ex) {
 +      System.out.println("Input mismatch while reading with scanner!!!");
 +      ex.printStackTrace();
 +      System.exit(-1);
 +    }
 +  }
 +  
 +  public static String toString(List<Student> students) {
 +    StringBuilder str = new StringBuilder();
 +    for(Student st : students) 
 +      str.append(st+"\n");
 +    str.append("---------------------------------\n");
 +    return str.toString();
 +  }
 +  
 +  public static void main(String []args) {
 +    if(args.length == 0) {
 +      System.out.println("Insufficient number of arguments!\n");
 +      return;
 +    }
 +    List<Student> students = new ArrayList<Student>();
 +    readFromFile(students, new File(args[0]));
 +    System.out.println(" --- Init Ordering ---\n");
 +    System.out.println(toString(students));
 +    Student.sortLexicographically(students);
 +    System.out.println(" --- Lexicographic Ordering ---\n");
 +    System.out.println(toString(students));
 +    Student.sortByAEM(students);
 +    System.out.println(" --- AEM Ordering ---\n");
 +    System.out.println(toString(students));
   }   }
 } }
Line 105: Line 159:
   }   }
      
-  abstract class BasicComparator implements Comparator<Student> { +  static abstract class BasicComparator implements Comparator<Student> {
-   +
   }   }
          
-  Comparator<Student> lexicographicCmp = new BasicComparator() {+  static Comparator<Student> lexicographicCmp = new BasicComparator() {
     public int compare(Student st1, Student st2) {     public int compare(Student st1, Student st2) {
       int cmp = st1.lastName.compareTo(st2.lastName);       int cmp = st1.lastName.compareTo(st2.lastName);
Line 119: Line 172:
     }         }    
   };   };
- 
-  public static void readFromFile(List<Student> students, File f) {     
-    try(Scanner sc = new Scanner(f)) { 
-      while(sc.hasNextLine()) {         
-        students.add( new Student(sc.nextInt(), sc.next(), sc.next()) ); 
-      } 
-    } 
-    catch(FileNotFoundException ex) { 
-      System.out.println("File \""+f.getName()+"\" was not found!"); 
-    } 
-    catch(InputMismatchException ex) { 
-      System.out.println("Input mismatch while reading with scanner!!!"); 
-      ex.printStackTrace(); 
-      System.exit(-1); 
-    } 
-  } 
      
-  public void sortLexicographically(List<Student> students) {    +  public static void sortLexicographically(List<Student> students) {    
     Collections.sort(students, lexicographicCmp);     Collections.sort(students, lexicographicCmp);
   }   }
          
-  public void sortByAEM(List<Student> students) {+  public static void sortByAEM(List<Student> students) {
     Collections.sort(students, new BasicComparator() {     Collections.sort(students, new BasicComparator() {
         public int compare(Student st1, Student st2) {         public int compare(Student st1, Student st2) {
Line 152: Line 189:
       }       }
     );     );
-  } +  } 
-   +
-  public static String toString(List<Student> students) { +
-    StringBuffer str = new StringBuffer(); +
-    for(Student st : students)  +
-      str.append(st+"\n"); +
-    str.append("---------------------------------\n"); +
-    return str.toString(); +
-  } +
-   +
-  public static void main(String []args) { +
-    if(args.length == 0) { +
-      System.out.println("Insufficient number of arguments!\n"); +
-      return; +
-    } +
-    List<Student> students = new ArrayList<Student>(); +
-    readFromFile(students, new File(args[0])); +
-    System.out.println(" --- Init Ordering ---\n"); +
-    System.out.println(toString(students)); +
-    students.get(0).sortLexicographically(students); +
-    System.out.println(" --- Lexicographic Ordering ---\n"); +
-    System.out.println(toString(students)); +
-    students.get(0).sortByAEM(students); +
-    System.out.println(" --- AEM Ordering ---\n"); +
-    System.out.println(toString(students)); +
-  } +
-    +
 } }
 </code> </code>
java/anon_inner_classes.1618208098.txt.gz · Last modified: 2021/04/12 05:14 (external edit)