OZBEKISTON RESPUBLIKASI AXBOROT TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
Algoritmlarni loyhalash fani boyicha
4-Labaratoriya ish
Topshirdi: Komilov Muhriddin
Toshkent 2020
Ixtiyoriy dasturlash tilida Priority Queue strukturaga ega bolgan studentlar massivi ifodalovchi dastur tuzing. Ustuvorlikni student yoshi boyicha ifodalang.
Student Model:
package algorithm_design.labaratoriya.lab4;
public class Student {
private String firstName;
private String lastName;
private int course;
private int age;
public Student(String firstName, String lastName, int course, int age) { this.firstName = firstName;
this.lastName = lastName;
this.course = course;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getCourse() {
return course;
}
public void setCourse(int course) {
this.course = course;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", course=" + course +
", age=" + age +
'}';
}
}
Main:
package algorithm_design.labaratoriya.lab4;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PriorityQueue students = new
PriorityQueue<>(Comparator.comparingInt(Student::getAge));
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("Ism: ");
scanner = new Scanner(System.in);
String firstName = scanner.nextLine();
System.out.print("Familiyasi: ");
String lastName = scanner.nextLine();
scanner = new Scanner(System.in);
System.out.print("Kursi: ");
int course = scanner.nextInt();
System.out.print("Yoshi: ");
int age = scanner.nextInt();
Student student = new Student(firstName, lastName, course, age);
students.add(student);
System.out.println("----------------------");
}
System.out.println("Studentlar PriorityQueuega qo`shildi"); while (!students.isEmpty()) {
Student student = students.poll();
System.out.println(student.getFirstName() + " " + student.getLastName() + " - " + student.getAge());
}
}
}
Natija:
Do'stlaringiz bilan baham: |