Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.mirea.prac10;
- import java.util.Scanner;
- public class Main {
- private static final StudentGpaComparator comp = new StudentGpaComparator(); //выбираем по какому признаку их сортировать
- //private static final StudentCourseComparator comp = new StudentCourseComparator();
- //private static final StudentGroupComparator comp = new StudentGroupComparator();
- //private static final StudentSNComparator comp = new StudentSNComparator();
- private static Student[] idNumber; //создаем массив
- public static void setArray() {
- System.out.println("Введите размер списка: "); //размерность списка
- Scanner in = new Scanner(System.in);
- int size = in.nextInt();
- Student[] idNumber = new Student[size];
- in.nextLine();
- for (int i = 0; i < size; i++) { //заполняем массив
- idNumber[i] = new Student();
- System.out.println("Введите имя студента: ");
- idNumber[i].setName(in.nextLine());
- System.out.println("Введите фамилию студента: ");
- idNumber[i].setSurname(in.nextLine());
- System.out.println("Введите специальность студента: ");
- idNumber[i].setSpecialty(in.nextLine());
- System.out.println("Введите курс студента: ");
- idNumber[i].setCourse(in.nextInt());
- in.nextLine();
- System.out.println("Введите группу студента: ");
- idNumber[i].setGroup(in.nextLine());
- System.out.println("Введите средний балл студента: ");
- idNumber[i].setGPA(in.nextInt());
- in.nextLine();
- System.out.println();
- }
- }
- public static void outArray() { //вывод массива
- for (int i = idNumber.length - 1; i >= 0; i--) {
- System.out.println(
- idNumber[i].getName() + " " +
- idNumber[i].getSurname() + " " +
- idNumber[i].getCourse() + " " +
- idNumber[i].getGroup() + " " +
- idNumber[i].getSpecialty() + " " +
- idNumber[i].getGPA()
- );
- }
- System.out.println();
- }
- public static void extendArray(Student[] otherArray) { //добаление еще одного "студента"
- int preSize = idNumber.length;
- int newSize = preSize + otherArray.length;
- Student[] newArray = new Student[newSize];
- for (int i = 0; i < newSize; i++) {
- if (i < preSize) newArray[i] = idNumber[i];
- else newArray[i] = otherArray[i - preSize];
- }
- idNumber = newArray;
- }
- public static void quickSort(Student[] arr, int leftBorder, int rightBorder) {
- int leftMarker = leftBorder;
- int rightMarker = rightBorder;
- Student pivot = arr[(leftMarker + rightMarker) / 2];
- do {
- while (comp.compare(arr[leftMarker], pivot) < 0) {
- leftMarker++;
- }
- while (comp.compare(arr[rightMarker], pivot) > 0) {
- rightMarker--;
- }
- if (leftMarker <= rightMarker) {
- if (leftMarker < rightMarker) {
- Student tmp = arr[leftMarker];
- arr[leftMarker] = arr[rightMarker];
- arr[rightMarker] = tmp;
- }
- leftMarker++;
- rightMarker--;
- }
- } while (leftMarker <= rightMarker);
- if (leftMarker < rightBorder) {
- quickSort(arr, leftMarker, rightBorder);
- }
- if (leftBorder < rightMarker) {
- quickSort(arr, leftBorder, rightMarker);
- }
- }
- public static void insertSort() {
- for (int left = 1; left < idNumber.length; left++) {
- Student temp = idNumber[left];
- int i = left - 1;
- for (; i >= 0; i--) {
- if (comp.compare(temp, idNumber[i]) < 0) {
- idNumber[i + 1] = idNumber[i];
- } else {
- break;
- }
- }
- idNumber[i + 1] = temp;
- }
- }
- public static void main(String[] args) {
- idNumber = new Student[] {
- new Student("Anna", "Volkova", 1, "IVBO-01-22", "ITI", 4),
- new Student("Bob", "Stone", 4, "KBBO-05-19", "ITI", 2),
- new Student("German", "Muller", 3, "IKBO-01-20", "ITI", 3)
- };
- outArray();
- quickSort(idNumber, 0, idNumber.length-1);
- outArray();
- extendArray( new Student[] {
- new Student("Arthur", "Black", 1, "IVBO-01-22", "ITI", 4)
- });
- outArray();
- insertSort();
- outArray();
- }
- }
- ##############################################
- package ru.mirea.prac10;
- public class Student implements Comparable<Student> {
- private String Name, Surname, Specialty, Group;
- private int Course;
- private int GPA;
- public Student() {}
- public String getName() {
- return Name;
- }
- public Student(String name, String surname, int course, String group, String specialty, int gpa) {
- Name = name;
- Surname = surname;
- Course = course;
- Group = group;
- Specialty = specialty;
- GPA = gpa;
- }
- public void setName(String name){
- this.Name = name;
- }
- public String getSurname() {
- return Surname;
- }
- public void setSurname(String surname){
- this.Surname = surname;
- }
- public String getSpecialty() {
- return Specialty;
- }
- public void setSpecialty(String specialty){
- this.Specialty = specialty;
- }
- public String getGroup() {
- return Group;
- }
- public void setGroup(String group){
- this.Group = group;
- }
- public int getCourse() {
- return Course;
- }
- public void setCourse(int course){
- this.Course = course;
- }
- public int getGPA() {
- return GPA;
- }
- public void setGPA(int gpa) {
- this.GPA = gpa;
- }
- @Override
- public int compareTo(Student other){
- if (this.getName().compareTo(other.getName()) == 0) {
- return this.getSurname().compareTo(other.getSurname());
- }
- return this.getName().compareTo(other.getName());
- }
- }
- ##############################################
- package ru.mirea.prac10;
- import java.util.Comparator;
- public class StudentCourseComparator implements Comparator<Student> {
- @Override
- public int compare(Student s1, Student s2) {
- return s1.getCourse() - s2.getCourse();
- }
- }
- ##############################################
- package ru.mirea.prac10;
- import java.util.Comparator;
- public class StudentGpaComparator implements Comparator<Student> {
- @Override
- public int compare(Student s1, Student s2) {
- return s1.getGPA() - s2.getGPA();
- }
- }
- ##############################################
- package ru.mirea.prac10;
- import java.util.Comparator;
- public class StudentGroupComparator implements Comparator<Student> {
- @Override
- public int compare(Student s1, Student s2) {
- if (s1.getGroup().equals(s2.getGroup())) return 0;
- else return 1;
- }
- }
- ##############################################
- package ru.mirea.prac10;
- import java.util.Comparator;
- public class StudentSNComparator implements Comparator<Student> {
- public int compare(Student s1, Student s2) {
- return s1.compareTo(s2) * (-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement