Skip to content

Commit 4bc407b

Browse files
committed
Uploading source files
0 parents  commit 4bc407b

8 files changed

+267
-0
lines changed

MedianOfAges.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Collections;
2+
import java.util.PriorityQueue;
3+
4+
public class MedianOfAges {
5+
// 2 priority queues to store the lower half and the upper half
6+
private PriorityQueue<Integer> maxHeap;
7+
private PriorityQueue<Integer> minHeap;
8+
9+
public MedianOfAges() {
10+
maxHeap = new PriorityQueue<>(Collections.reverseOrder());
11+
minHeap = new PriorityQueue<>();
12+
}
13+
14+
// Inserting the number to the correct heap
15+
public void insertNum(int num) {
16+
if (maxHeap.isEmpty() || num <= maxHeap.peek()) {
17+
maxHeap.add(num);
18+
} else {
19+
minHeap.add(num);
20+
}
21+
22+
// Balancing the heaps according to their sizes to keep the median
23+
if (maxHeap.size() > minHeap.size() + 1) {
24+
minHeap.add(maxHeap.poll());
25+
} else if (minHeap.size() > maxHeap.size()) {
26+
maxHeap.add(minHeap.poll());
27+
}
28+
}
29+
30+
// Finding the median
31+
public double findMedian() {
32+
if (maxHeap.size() == minHeap.size()) {
33+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
34+
} else {
35+
return maxHeap.peek();
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
MedianOfAges medianOfAges = new MedianOfAges();
41+
medianOfAges.insertNum(22);
42+
medianOfAges.insertNum(35);
43+
System.out.println("The recommended content will be for ages under: " + medianOfAges.findMedian());
44+
medianOfAges.insertNum(30);
45+
System.out.println("The recommended content will be for ages under: " + medianOfAges.findMedian());
46+
medianOfAges.insertNum(25);
47+
System.out.println("The recommended content will be for ages under: " + medianOfAges.findMedian());
48+
}
49+
}

Shipment.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//-----------------------------------------------------
2+
// Title: Shipment Class
3+
// Author: Mahmut Emre Demir
4+
// ID: 59344436266
5+
// Section: 2
6+
// Assignment: 4
7+
// Description: This class contains the shipment object with name, deadline and duration. Also, it has an overridden toString method to print the shipment object accordingly.
8+
//-----------------------------------------------------
9+
10+
public class Shipment {
11+
private String name;
12+
private Long deadLine;
13+
private Long duration;
14+
15+
public Shipment(String name, Long deadLine, Long duration) {
16+
this.name = name;
17+
this.deadLine = deadLine;
18+
this.duration = duration;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public Long getDeadLine() {
30+
return deadLine;
31+
}
32+
33+
public void setDeadLine(Long deadLine) {
34+
this.deadLine = deadLine;
35+
}
36+
37+
public Long getDuration() {
38+
return duration;
39+
}
40+
41+
public void setDuration(Long duration) {
42+
this.duration = duration;
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return getName() + " with deadline " + getDeadLine() + " and duration " + getDuration();
48+
}
49+
}

ShipmentComparator.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//-----------------------------------------------------
2+
// Title: Shipment Comparator
3+
// Author: Mahmut Emre Demir
4+
// ID: 59344436266
5+
// Section: 2
6+
// Assignment: 4
7+
// Description: This class compares the shipments according to their deadlines. It implements Comparator interface and overrides compare method.
8+
// compare method is used to store the shipments in a priority queue according to their deadlines.
9+
//-----------------------------------------------------
10+
11+
import java.util.Comparator;
12+
13+
public class ShipmentComparator implements Comparator<Shipment> {
14+
@Override
15+
public int compare(Shipment s1, Shipment s2) {
16+
if (s1.getDeadLine() > s2.getDeadLine()) {
17+
return 1;
18+
} else if (s1.getDeadLine() < s2.getDeadLine()) {
19+
return -1;
20+
} else {
21+
return 0;
22+
}
23+
}
24+
}

ShipmentTracking.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//-----------------------------------------------------
2+
// Title: Shipment Tracking
3+
// Author: Mahmut Emre Demir
4+
// ID: 59344436266
5+
// Section: 2
6+
// Assignment: 4
7+
// Description: This class stores all the shipments in a priority queue and a current time object.
8+
// scheduleShipment() method is used to add a shipment to the priority queue.
9+
// startShipment() method is used to print the shipment that is currently being processed.
10+
// completeShipment() method is used to print the shipment that is completed.
11+
// runTime() method is used to simulate the shipment process. It takes the time as a parameter and processes the shipments accordingly.
12+
//-----------------------------------------------------
13+
14+
import java.util.PriorityQueue;
15+
16+
public class ShipmentTracking {
17+
private static PriorityQueue<Shipment> allShipments = new PriorityQueue<Shipment>(new ShipmentComparator());
18+
private static Long currentTime = 0L;
19+
20+
21+
public static PriorityQueue<Shipment> getAllShipments() {
22+
return allShipments;
23+
}
24+
25+
public static void setAllShipments(PriorityQueue<Shipment> allShipments) {
26+
ShipmentTracking.allShipments = allShipments;
27+
}
28+
29+
public static Long getCurrentTime() {
30+
return currentTime;
31+
}
32+
33+
public static void setCurrentTime(Long currentTime) {
34+
ShipmentTracking.currentTime = currentTime;
35+
}
36+
37+
public static void runTime(Long time) {
38+
Long remainingTime = time;
39+
40+
// Firstly, checking if there are any shipments to process
41+
if (!getAllShipments().isEmpty()) {
42+
43+
// Processing the shipments until there's no time left or no shipments left
44+
while (remainingTime > 0 && !getAllShipments().isEmpty()) {
45+
// Getting the shipment with the earliest deadline and creating a new shipment object
46+
Shipment shipment = getAllShipments().poll();
47+
if (shipment != null) {
48+
// Starting the shipment
49+
startShipment(shipment);
50+
// Checking if there is enough time to complete the shipment
51+
if (shipment.getDuration() <= remainingTime) {
52+
// If there is enough time completing the shipment and updating the remaining time
53+
setCurrentTime(getCurrentTime() + shipment.getDuration());
54+
completeShipment(shipment);
55+
remainingTime -= shipment.getDuration();
56+
} else {
57+
// If there is not enough time to complete the shipment updating the remaining time and adding the shipment back to the queue
58+
setCurrentTime(getCurrentTime() + remainingTime);
59+
scheduleShipment(new Shipment(shipment.getName(), shipment.getDeadLine(), shipment.getDuration() - remainingTime));
60+
break; // Breaking the loop since there is not enough time to complete this shipment
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
68+
public static void scheduleShipment(Shipment shipment) {
69+
getAllShipments().add(shipment);
70+
System.out.println(getCurrentTime() + ": adding " + shipment.toString());
71+
}
72+
73+
public static void startShipment(Shipment shipment) {
74+
System.out.println(getCurrentTime() + ": busy with " + shipment.toString());
75+
}
76+
77+
public static void completeShipment(Shipment shipment) {
78+
System.out.println(getCurrentTime() + ": done with " + shipment.getName());
79+
}
80+
}

TXTReader.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//-----------------------------------------------------
2+
// Title: TXT Reader
3+
// Author: Mahmut Emre Demir
4+
// ID: 59344436266
5+
// Section: 2
6+
// Assignment: 4
7+
// Description: This class reads the input file and processes the shipments accordingly.
8+
//-----------------------------------------------------
9+
10+
import java.io.File;
11+
import java.io.FileNotFoundException;
12+
import java.util.Scanner;
13+
14+
public class TXTReader {
15+
16+
public void readTxtFile(File file) {
17+
try (Scanner scanner = new Scanner(file)) {
18+
// Reading the file line by line
19+
while (scanner.hasNextLine()) {
20+
String line = scanner.nextLine();
21+
// Splitting the line by spaces
22+
String[] words = line.split("\\s+");
23+
// Checking the first word to determine the operation
24+
if (words[0].equals("schedule")) {
25+
ShipmentTracking.scheduleShipment(new Shipment(words[1], Long.parseLong(words[2]), Long.parseLong(words[3])));
26+
} else {
27+
ShipmentTracking.runTime(Long.parseLong(words[1]));
28+
}
29+
30+
31+
}
32+
} catch (FileNotFoundException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}

Task2Tester.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//-----------------------------------------------------
2+
// Title: Tester for Task 2
3+
// Author: Mahmut Emre Demir
4+
// ID: 59344436266
5+
// Section: 2
6+
// Assignment: 4
7+
// Description: This class is a tester for Task2. To test the program with different inputs, you can change the file name in the main method.
8+
// sampleInput1.txt is provided to test the program as default. To test with different inputs simply uncomment the line 17.
9+
// You can also create your own input files to test the program.
10+
//-----------------------------------------------------
11+
12+
import java.io.File;
13+
14+
public class Task2Tester {
15+
public static void main(String[] args) {
16+
TXTReader txtReader = new TXTReader();
17+
txtReader.readTxtFile(new File("sampleinput1.txt"));
18+
//txtReader.readTxtFile(new File("sampleinput2.txt"));
19+
20+
}
21+
}

sampleInput1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
schedule shipmentA 1300 30
2+
schedule shipmentB 7200 2300
3+
schedule shipmentC 2359 300
4+
run 2400

sampleinput2.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
schedule shipmentX 1200 50
2+
schedule shipmentY 3000 500
3+
schedule shipmentZ 1500 200
4+
run 2000

0 commit comments

Comments
 (0)