SHARE
TWEET

Untitled

a guest Jan 19th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Student Class
  2.  
  3. package edu.myuni1001;
  4.  
  5.  
  6. public class Student implements Comparable {
  7.  
  8.     private String name;
  9.  
  10.     public Student(String name) {
  11.  
  12.         this.name = name;
  13.     }
  14.  
  15.     public String getName() {
  16.         return name;
  17.     }
  18.  
  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }
  22.  
  23.     @Override
  24.     public int compareTo(Object o) {
  25.         return this.name.compareTo(((Student)o).getName());
  26.     }
  27.  
  28.  
  29. }
  30.  
  31.  
  32. #Main Class
  33.  
  34. package edu.myuni1001;
  35.  
  36. import java.util.*;
  37.  
  38. public class Main {
  39.  
  40.     public static void main(String[] args) {
  41.  
  42.      Set<Student> students = new TreeSet<>();
  43.  
  44.      Student tim = new Student("Tim");
  45.  
  46.      Student david =  new Student("David");
  47.  
  48.      Student mary =  new Student("Mary");
  49.  
  50.      students.add(tim);
  51.  
  52.      students.add(david);
  53.  
  54.      students.add(mary);
  55.  
  56.      for (Student student: students){
  57.          System.out.println(student.getName());
  58.      }
  59.  
  60.     }
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top