[Go to site: main page, start]

0% found this document useful (0 votes)
3 views3 pages

Java Priority Queue and TreeSet Lab

The document contains two Java assignments focused on collections. The first assignment involves creating an Item class for a priority queue with a custom comparator for sorting, while the second assignment requires creating a Student class that implements Comparable for natural ordering in a TreeSet. Both assignments include example implementations and expected outputs demonstrating the functionality of the classes and collections.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Priority Queue and TreeSet Lab

The document contains two Java assignments focused on collections. The first assignment involves creating an Item class for a priority queue with a custom comparator for sorting, while the second assignment requires creating a Student class that implements Comparable for natural ordering in a TreeSet. Both assignments include example implementations and expected outputs demonstrating the functionality of the classes and collections.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA ASSIGNMENT (COLLECTION LAB-2)

Q1. Create a class named Item that represents an object you want to store in the priority
queue. For this lab, we'll define an item with a name and a priority level.
Create a main class, Priority Queue Lab, to instantiate the Priority Queue and add Item
objects.
Instead of having the Item class implement Comparable, you can define a separate
Comparator to change the sorting logic without modifying [Link].

SOL: package Collection2;


import [Link];
import [Link];
public class questt1 {
// Inner Item class
static class Item {
private String name;
private int priority;
public Item(String name, int priority) {
[Link] = name;
[Link] = priority;
}
public String getName() {
return name;
}
public int getPriority() {
return priority;
}
@Override
public String toString() {
return "Item{name='" + name + "', priority=" + priority + "}";
}
}
// Inner Comparator
static class PriorityComparator implements Comparator<Item> {
@Override
public int compare(Item i1, Item i2) {
return [Link]([Link](), [Link]());
}
}
public static void main(String[] args) {
PriorityQueue<Item> pq = new PriorityQueue<>(new PriorityComparator());
[Link](new Item("Task A", 3));
[Link](new Item("Task B", 1));
[Link](new Item("Task C", 2));
[Link](new Item("Task D", 5));
while (![Link]()) {
[Link]([Link]());
}
}
}

OUTPUT: Item{name='Task B', priority=1}


Item{name='Task C', priority=2}
Item{name='Task A', priority=3}
Item{name='Task D', priority=5}

Q2. Create a class named Student with the following private fields:

 studentId (int)

 name (String)

If the Student class is to be stored in a TreeSet<Student>, it must define an [Link]


the Student class so that it implements the Comparable<Student> [Link] the
compareTo() method to define the natural ordering of students based primarily on
their studentId in ascending [Link] a main class (e.g., TreeSetLab).Instantiate a
TreeSet<Student>.Add the following Student objects to the set:

 S1: (101, "Alice", 3.8)

 S2: (105, "Bob", 3.2)

 S3: (101, "Charlie", 3.5)

 S4: (103, "David", 4.0)

 S5: (101, "Alice", 3.8) - (Should test the uniqueness constraint)

Iterate and print all elements in the TreeSet and observe the order.

SOL: package Collection2;


import [Link];
public class questt2 {
// Inner Student class
static class Student implements Comparable<Student> {
private int studentId;
private String name;
private double gpa;
public Student(int studentId, String name, double gpa) {
[Link] = studentId;
[Link] = name;
[Link] = gpa;
}
@Override
public int compareTo(Student other) {
return [Link]([Link], [Link]);
}
@Override
public String toString() {
return "Student{id=" + studentId + ", name='" + name + "', gpa=" + gpa + "}";
}
}
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<>();
[Link](new Student(101, "Mohan", 3.8));
[Link](new Student(105, "Rohit", 3.2));
[Link](new Student(101, "Virat", 3.5)); // Duplicate ID
[Link](new Student(103, "Suresh", 4.0));
[Link](new Student(101, "Vihaan", 3.8));// Exact duplicate
[Link]("Students in TreeSet (ordered by studentId):");
for (Student s : students) {
[Link](s);
}
}
}

OUTPUT: Students in TreeSet (ordered by studentId):

Student{id=101, name='Mohan', gpa=3.8}

Student{id=103, name='Suresh', gpa=4.0}

Student{id=105, name='Rohit', gpa=3.2}

You might also like