lab02

 avatar
user_3924995
plain_text
10 months ago
3.7 kB
7
Indexable
package oe.wd;

public class Student implements Comparable{
    private int age;
    private String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object other) {
        return Integer.compare(age, ((Student)other).getAge());
    }
}





package oe.wd;

import java.util.*;

public class MainClass
{
    public static void main(String[] args) {
            Student peter = new Student(23, "Peter");
            Student attila = new Student(19, "Attila");
            Student helen = new Student(25, "Helen");
            Student john = new Student(20, "John");

        ArrayList<Student> myList = new ArrayList<>();
        myList.add(peter);
        myList.add(attila);
        myList.add(helen);

        for(Student student : myList){
            System.out.println(student);
        }

        myList.set(1, john); //replaces
        System.out.println("After changing the list");
        for( Student student : myList){
            System.out.println(student);
        }

        myList.add(1, attila); //adds and shifts the list
        System.out.println("After changing the list for the second time");
        for( Student student : myList){
            System.out.println(student);
        }

        List<Student> second = Arrays.asList(attila, john, helen);
        System.out.println(second.size());
        //second.add(peter);
        //it is a fixed-size list, so we cant add anymore elements to it
        //but we can replace an element
        second.set(2,peter); //replaces helen with peter

        PriorityQueue<Student> studQueue = new PriorityQueue<>();
        studQueue.add(helen);
        studQueue.add(attila);
        studQueue.add(peter);
        studQueue.add(john);

        System.out.println("The content of the priority queue");
        for(Student student : studQueue){
            System.out.println(student);
        }

        studQueue.add(studQueue.poll());

        System.out.println("Priority queue after the change");
        while(studQueue.isEmpty()){
            System.out.println(studQueue.poll()); //removes and returns
        }

        System.out.println(studQueue.peek());


        System.out.println("Priority queue");
        while(!studQueue.isEmpty()){
            System.out.println(studQueue.poll());
        }

        LinkedList<Student> studentLinkedList = new LinkedList<>();
        studentLinkedList.add(peter);
        studentLinkedList.add(attila);
        studentLinkedList.add(helen);
        studentLinkedList.add(john);
        studentLinkedList.add(2, john);

        System.out.println("The student at the head of the queue");
        System.out.println(studentLinkedList.peek());
        System.out.println("The content of the queue");
        while(!studentLinkedList.isEmpty()){
            System.out.println(studentLinkedList.poll());
        }

        TreeSet<Student> studentTreeSet = new TreeSet<>();
        studentTreeSet.add(peter);
        studentTreeSet.add(attila);
        studentTreeSet.add(john);
        studentTreeSet.add(helen);
    }
}
Editor is loading...
Leave a Comment