Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mport java.io . File; import java.io . FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; / / Attributes for node class Node { String

mport java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//Attributes for node
class Node {
String regNo;
String departureTimestamp;
String flightDur;
long arrivalTime;
//constructor
Node(String registrationNo, String departureTimestamp, String flightDuration){
this.regNo = registrationNo;
this.departureTimestamp = departureTimestamp;
this.flightDur = flightDuration;
this.arrivalTime = calculateArrivalTime(departureTimestamp, flightDuration);
}
private long calculateArrivalTime(String departureTimestamp, String flightDuration){
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
try {
Date departureTime = format.parse(departureTimestamp);
Date duration = format.parse(flightDuration);
return departureTime.getTime()+ duration.getTime();
} catch (ParseException e){
e.printStackTrace();
return 0;
}
}
}
class MinHeap {
Node[] heap;
int size;
MinHeap(int capacity){
heap = new Node[capacity];
size =0;
}
void add(Node node){
if (size == heap.length){
System.out.println("Heap is full");
return;
}
heap[size]= node;
size++;
heapifyUp(size -1);
}
Node poll(){
if (size ==0)
return null;
Node minNode = heap[0];
heap[0]= heap[size -1];
size--;
heapifyDown(0);
return minNode;
}
int size(){
return size;
}
boolean isEmpty(){
return size ==0;
}
private void heapifyUp(int index){
while (index >0 && heap[index].arrivalTime < heap[parent(index)].arrivalTime){
swap(index, parent(index));
index = parent(index);
}
}
private void heapifyDown(int index){
int smallest = index;
int left = leftChild(index);
int right = rightChild(index);
if (left < size && heap[left].arrivalTime < heap[smallest].arrivalTime)
smallest = left;
if (right < size && heap[right].arrivalTime < heap[smallest].arrivalTime)
smallest = right;
if (smallest != index){
swap(index, smallest);
heapifyDown(smallest);
}
}
private int parent(int index){
return (index -1)/2;
}
private int leftChild(int index){
return 2* index +1;
}
private int rightChild(int index){
return 2* index +2;
}
private void swap(int i, int j){
Node temp = heap[i];
heap[i]= heap[j];
heap[j]= temp;
}
}
//Main method
public class Main {
public static void main(String[] args){
try {
File file = new File("C:\\Users\\27725\\IdeaProjects\\Question3Ptac5\\src\\flight.txt");
Scanner scanner = new Scanner(file);
MinHeap minHeap = new MinHeap(20);
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] parts = line.split(",");
String registrationNo = parts[0].trim();
String departureTimestamp = parts[1].trim();
String flightDuration = parts[2].trim();
Node node = new Node(registrationNo, departureTimestamp, flightDuration);
minHeap.add(node);
}
scanner.close();
System.out.println("Incoming flights in order");
while (!minHeap.isEmpty()){
Node node = minHeap.poll();
System.out.println(node.regNo +""+ node.departureTimestamp);
}
} catch (FileNotFoundException e){
System.out.println("File not found: flights.txt");
}
}
} giveme references for the code you gave me so that i could avoid plagiarism

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions