Advertisement
malixds_

prac10

Dec 12th, 2022
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. package ru.mirea.prac10;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     private static final StudentGpaComparator comp = new StudentGpaComparator();            //выбираем по какому признаку их сортировать
  7.     //private static final StudentCourseComparator comp = new StudentCourseComparator();
  8.     //private static final StudentGroupComparator comp = new StudentGroupComparator();
  9.     //private static final StudentSNComparator comp = new StudentSNComparator();
  10.  
  11.     private static Student[] idNumber;      //создаем массив
  12.  
  13.     public static void setArray() {
  14.         System.out.println("Введите размер списка: ");  //размерность списка
  15.         Scanner in = new Scanner(System.in);
  16.         int size = in.nextInt();
  17.         Student[] idNumber = new Student[size];
  18.         in.nextLine();
  19.         for (int i = 0; i < size; i++) {        //заполняем массив
  20.             idNumber[i] = new Student();
  21.  
  22.             System.out.println("Введите имя студента: ");
  23.             idNumber[i].setName(in.nextLine());
  24.  
  25.             System.out.println("Введите фамилию студента: ");
  26.             idNumber[i].setSurname(in.nextLine());
  27.  
  28.             System.out.println("Введите специальность студента: ");
  29.             idNumber[i].setSpecialty(in.nextLine());
  30.  
  31.             System.out.println("Введите курс студента: ");
  32.             idNumber[i].setCourse(in.nextInt());
  33.             in.nextLine();
  34.  
  35.             System.out.println("Введите группу студента: ");
  36.             idNumber[i].setGroup(in.nextLine());
  37.  
  38.             System.out.println("Введите средний балл студента: ");
  39.             idNumber[i].setGPA(in.nextInt());
  40.             in.nextLine();
  41.             System.out.println();
  42.         }
  43.     }
  44.  
  45.     public static void outArray() {                         //вывод массива
  46.         for (int i = idNumber.length - 1; i >= 0; i--) {
  47.             System.out.println(
  48.                     idNumber[i].getName() + " " +
  49.                             idNumber[i].getSurname() + " " +
  50.                             idNumber[i].getCourse() + " " +
  51.                             idNumber[i].getGroup() + " " +
  52.                             idNumber[i].getSpecialty() + "  " +
  53.                             idNumber[i].getGPA()
  54.             );
  55.         }
  56.         System.out.println();
  57.     }
  58.  
  59.     public static void extendArray(Student[] otherArray) {      //добаление еще одного "студента"
  60.         int preSize = idNumber.length;
  61.         int newSize = preSize + otherArray.length;
  62.         Student[] newArray = new Student[newSize];
  63.         for (int i = 0; i < newSize; i++) {
  64.             if (i < preSize) newArray[i] = idNumber[i];
  65.             else newArray[i] = otherArray[i - preSize];
  66.         }
  67.         idNumber = newArray;
  68.     }
  69.  
  70.     public static void quickSort(Student[] arr, int leftBorder, int rightBorder) {
  71.         int leftMarker = leftBorder;
  72.         int rightMarker = rightBorder;
  73.         Student pivot = arr[(leftMarker + rightMarker) / 2];
  74.         do {
  75.             while (comp.compare(arr[leftMarker], pivot) < 0) {
  76.                 leftMarker++;
  77.             }
  78.             while (comp.compare(arr[rightMarker], pivot) > 0) {
  79.                 rightMarker--;
  80.             }
  81.             if (leftMarker <= rightMarker) {
  82.                 if (leftMarker < rightMarker) {
  83.                     Student tmp = arr[leftMarker];
  84.                     arr[leftMarker] = arr[rightMarker];
  85.                     arr[rightMarker] = tmp;
  86.                 }
  87.                 leftMarker++;
  88.                 rightMarker--;
  89.             }
  90.         } while (leftMarker <= rightMarker);
  91.         if (leftMarker < rightBorder) {
  92.             quickSort(arr, leftMarker, rightBorder);
  93.         }
  94.         if (leftBorder < rightMarker) {
  95.             quickSort(arr, leftBorder, rightMarker);
  96.         }
  97.     }
  98.  
  99.     public static void insertSort() {
  100.         for (int left = 1; left < idNumber.length; left++) {
  101.             Student temp = idNumber[left];
  102.             int i = left - 1;
  103.             for (; i >= 0; i--) {
  104.                 if (comp.compare(temp, idNumber[i]) < 0) {
  105.                     idNumber[i + 1] = idNumber[i];
  106.                 } else {
  107.                     break;
  108.                 }
  109.             }
  110.             idNumber[i + 1] = temp;
  111.         }
  112.     }
  113.  
  114.     public static void main(String[] args) {
  115.         idNumber = new Student[] {
  116.                 new Student("Anna", "Volkova", 1, "IVBO-01-22", "ITI", 4),
  117.                 new Student("Bob", "Stone", 4, "KBBO-05-19", "ITI", 2),
  118.                 new Student("German", "Muller", 3, "IKBO-01-20", "ITI", 3)
  119.         };
  120.         outArray();
  121.  
  122.         quickSort(idNumber, 0, idNumber.length-1);
  123.         outArray();
  124.  
  125.         extendArray( new Student[] {
  126.                 new Student("Arthur", "Black", 1, "IVBO-01-22", "ITI", 4)
  127.         });
  128.         outArray();
  129.         insertSort();
  130.         outArray();
  131.     }
  132. }
  133.  
  134. ##############################################
  135.  
  136. package ru.mirea.prac10;
  137.  
  138. public class Student implements Comparable<Student> {
  139.     private String Name, Surname, Specialty, Group;
  140.     private int Course;
  141.     private int GPA;
  142.  
  143.     public Student() {}
  144.  
  145.     public String getName() {
  146.         return Name;
  147.     }
  148.  
  149.     public Student(String name, String surname, int course, String group, String specialty, int gpa) {
  150.         Name = name;
  151.         Surname = surname;
  152.         Course = course;
  153.         Group = group;
  154.         Specialty = specialty;
  155.         GPA = gpa;
  156.     }
  157.  
  158.     public void setName(String name){
  159.         this.Name = name;
  160.     }
  161.  
  162.     public String getSurname() {
  163.         return Surname;
  164.     }
  165.  
  166.     public void setSurname(String surname){
  167.         this.Surname = surname;
  168.     }
  169.  
  170.     public String getSpecialty() {
  171.         return Specialty;
  172.     }
  173.  
  174.     public void setSpecialty(String specialty){
  175.         this.Specialty = specialty;
  176.     }
  177.  
  178.     public String getGroup() {
  179.         return Group;
  180.     }
  181.  
  182.     public void setGroup(String group){
  183.         this.Group = group;
  184.     }
  185.  
  186.     public int getCourse() {
  187.         return Course;
  188.     }
  189.  
  190.     public void setCourse(int course){
  191.         this.Course = course;
  192.     }
  193.  
  194.     public int getGPA() {
  195.         return GPA;
  196.     }
  197.  
  198.     public void setGPA(int gpa) {
  199.         this.GPA = gpa;
  200.     }
  201.  
  202.     @Override
  203.     public int compareTo(Student other){
  204.         if (this.getName().compareTo(other.getName()) == 0) {
  205.             return this.getSurname().compareTo(other.getSurname());
  206.         }
  207.         return this.getName().compareTo(other.getName());
  208.     }
  209. }
  210.  
  211. ##############################################
  212. package ru.mirea.prac10;
  213.  
  214. import java.util.Comparator;
  215.  
  216. public class StudentCourseComparator implements Comparator<Student> {
  217.     @Override
  218.     public int compare(Student s1, Student s2) {
  219.         return s1.getCourse() - s2.getCourse();
  220.     }
  221. }
  222.  
  223. ##############################################
  224.  
  225. package ru.mirea.prac10;
  226.  
  227. import java.util.Comparator;
  228.  
  229. public class StudentGpaComparator implements Comparator<Student> {
  230.     @Override
  231.     public int compare(Student s1, Student s2) {
  232.         return s1.getGPA() - s2.getGPA();
  233.     }
  234. }
  235.  
  236.  
  237. ##############################################
  238.  
  239. package ru.mirea.prac10;
  240.  
  241. import java.util.Comparator;
  242.  
  243. public class StudentGroupComparator implements Comparator<Student> {
  244.     @Override
  245.     public int compare(Student s1, Student s2) {
  246.         if (s1.getGroup().equals(s2.getGroup())) return 0;
  247.         else return 1;
  248.     }
  249. }
  250.  
  251.  
  252.  
  253. ##############################################
  254.  
  255. package ru.mirea.prac10;
  256.  
  257. import java.util.Comparator;
  258.  
  259. public class StudentSNComparator implements Comparator<Student> {
  260.     public int compare(Student s1, Student s2) {
  261.         return s1.compareTo(s2) * (-1);
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement