Haifisch7734

Zad3/Lab

Apr 23rd, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class Grupa {
  4.     String nazwa;
  5.     Student[] studenci;
  6.  
  7.     public Grupa(String nazwa){
  8.         this.nazwa = nazwa;
  9.         studenci = null;
  10.     }
  11.    
  12.     public void dodajStudenta(Student student) {
  13.         if (studenci == null)
  14.             studenci = new Student[1];
  15.         else
  16.             studenci = (Student[]) Arrays.copyOf(studenci, studenci.length + 1);
  17.         studenci[studenci.length - 1] = student;
  18.         Arrays.sort(studenci);
  19.     }
  20.  
  21.     public void usunStudenta(int ind){
  22.         studenci[ind] = studenci[studenci.length-1];
  23.         studenci = (Student[]) Arrays.copyOf(studenci, studenci.length - 1);
  24.         Arrays.sort(studenci);
  25.     }
  26.    
  27.     public void sklad(){
  28.         System.out.println(this.nazwa);
  29.         for(int i = 0;i < studenci.length;i++){
  30.             System.out.println(studenci[i]);
  31.         }
  32.     }
  33. }
  34.  
  35. @SuppressWarnings("rawtypes")
  36. class Student implements Comparable {
  37.     String imie;
  38.     String nazwisko;
  39.     Grupa grupa;
  40.  
  41.     public Student(String imie, String nazwisko, Grupa grupa) {
  42.         this.imie = imie;
  43.         this.nazwisko = nazwisko;
  44.         this.grupa = grupa;
  45.     }
  46.     @Override
  47.     public String toString(){
  48.         return imie + " " + nazwisko;
  49.        
  50.     }
  51.    
  52.     @Override
  53.     public int compareTo(Object o) {
  54.         Student p = (Student) o;
  55.         int porownaneNazwiska = nazwisko.compareTo(p.nazwisko);
  56.  
  57.         if (porownaneNazwiska == 0) {
  58.             return imie.compareTo(p.imie);
  59.         } else {
  60.             return porownaneNazwiska;
  61.         }
  62.     }
  63. }
  64.  
  65. public class Dziekanat {
  66.     public static void main(String[] args){
  67.         Grupa g1 = new Grupa("2ID12");
  68.         g1.dodajStudenta(new Student("Jan","Kowalski",g1));
  69.         g1.dodajStudenta(new Student("Andrzej","Kowalski",g1));
  70.         g1.dodajStudenta(new Student("Jan","Adamczyk",g1));
  71.         g1.dodajStudenta(new Student("Jan","Ziemczyk",g1));
  72.         g1.sklad();
  73.         int x = Arrays.binarySearch(g1.studenci, new Student("Jan","Kowalski",g1));
  74.         System.out.println(x);
  75.         g1.usunStudenta(x);
  76.         g1.sklad();
  77.     }
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment