0% found this document useful (0 votes)
10 views4 pages

HM 3

The document contains two Java classes: CPU_Scheduler, which manages tasks using a priority queue based on their priority, and StudentDirectory, which loads student data from a file into a map and allows searching for students by ID. The CPU_Scheduler class demonstrates task scheduling by adding tasks and printing them in order of priority. The StudentDirectory class reads student information from a file and retrieves student names based on user input IDs.

Uploaded by

prbzd2rddh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

HM 3

The document contains two Java classes: CPU_Scheduler, which manages tasks using a priority queue based on their priority, and StudentDirectory, which loads student data from a file into a map and allows searching for students by ID. The CPU_Scheduler class demonstrates task scheduling by adding tasks and printing them in order of priority. The StudentDirectory class reads student information from a file and retrieves student names based on user input IDs.

Uploaded by

prbzd2rddh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

PriorityQueue;

import java.util.Comparator;

class Task {

int id;

int priority;

public Task(int id, int priority) {

this.id = id;

this.priority = priority;

class CPU_Scheduler {

private PriorityQueue<Task> taskQueue;

public CPU_Scheduler() {

taskQueue = new PriorityQueue<>(Comparator.comparingInt(task -> task.priority));

public void addTask(int id, int priority) {

taskQueue.add(new Task(id, priority));

public void scheduleTasks() {


while (!taskQueue.isEmpty()) {

Task task = taskQueue.poll();

System.out.println("ID: " + task.id + " : " + task.priority);

public static void main(String[] args) {

CPU_Scheduler scheduler = new CPU_Scheduler();

scheduler.addTask(1, 3);

scheduler.addTask(2, 1);

scheduler.addTask(3, 2);

scheduler.scheduleTasks();

q2

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class StudentDirectory {

private Map<String, String> studentMap;


public StudentDirectory() {

studentMap = new HashMap<>();

public void loadStudentsFromFile(String filename) {

try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {

String line;

while ((line = reader.readLine()) != null) {

String[] parts = line.split(" ");

if (parts.length >= 4) {

String id = parts[0];

String fullName = String.join(" ", parts[1], parts[2], parts[3]);

studentMap.put(id, fullName);

} catch (IOException e) {

System.out.

public void searchStudent(String id) {

if (studentMap.containsKey(id)) {

System.out.println(studentMap.get(id));

} else {
System.out.println("");

public static void main(String[] args) {

StudentDirectory directory = new StudentDirectory();

directory.loadStudentsFromFile("input.txt");

Scanner scanner = new Scanner(System.in);

String studentId = scanner.nextLine();

directory.searchStudent(studentId);

scanner.close();

You might also like