Answered step by step
Verified Expert Solution
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.ioFile;
import java.ioFileNotFoundException;
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
NodeString registrationNo, String departureTimestamp, String flightDuration
this.regNo registrationNo;
this.departureTimestamp departureTimestamp;
this.flightDur flightDuration;
this.arrivalTime calculateArrivalTimedepartureTimestamp flightDuration;
private long calculateArrivalTimeString departureTimestamp, String flightDuration
SimpleDateFormat format new SimpleDateFormatHH:mm:ss;
try
Date departureTime format.parsedepartureTimestamp;
Date duration format.parseflightDuration;
return departureTime.getTime duration.getTime;
catch ParseException e
eprintStackTrace;
return ;
class MinHeap
Node heap;
int size;
MinHeapint capacity
heap new Nodecapacity;
size ;
void addNode node
if size heap.length
System.out.printlnHeap is full";
return;
heapsize node;
size;
heapifyUpsize ;
Node poll
if size
return null;
Node minNode heap;
heap heapsize ;
size;
heapifyDown;
return minNode;
int size
return size;
boolean isEmpty
return size ;
private void heapifyUpint index
while index && heapindexarrivalTime heapparentindexarrivalTime
swapindex parentindex;
index parentindex;
private void heapifyDownint index
int smallest index;
int left leftChildindex;
int right rightChildindex;
if left size && heapleftarrivalTime heapsmallestarrivalTime
smallest left;
if right size && heaprightarrivalTime heapsmallestarrivalTime
smallest right;
if smallest index
swapindex smallest;
heapifyDownsmallest;
private int parentint index
return index ;
private int leftChildint index
return index ;
private int rightChildint index
return index ;
private void swapint i int j
Node temp heapi;
heapi heapj;
heapj temp;
Main method
public class Main
public static void mainString args
try
File file new FileC:UsersIdeaProjectsQuestionPtacsrcflighttxt;
Scanner scanner new Scannerfile;
MinHeap minHeap new MinHeap;
while scannerhasNextLine
String line scanner.nextLine;
String parts line.split;
String registrationNo partstrim;
String departureTimestamp partstrim;
String flightDuration partstrim;
Node node new NoderegistrationNo departureTimestamp, flightDuration;
minHeap.addnode;
scanner.close;
System.out.printlnIncoming flights in order";
while minHeap.isEmpty
Node node minHeap.poll;
System.out.printlnnoderegNo node.departureTimestamp;
catch FileNotFoundException e
System.out.printlnFile 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started