Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2 . 1 Create a file Main.java in which to write your code for this question. 2 . 2 Write code for a main method

2.1 Create a file Main.java in which to write your code for this question.
2.2 Write code for a main method to build a Min Heap from the data in flights.txt
You will need to write code that performs String pre-processing. This means transform
the raw data into the appropriate Node attributes and calculate the arrival time, before
creating a Node and adding it to the Heap. The heap should be ordered by arrival time.
2.3 In the main method, print out the order in which incoming flights should land Sample data: flights.txt
registration_no, departure_timestamp, flight_duration (hh:mm:ss)
ZA7117,14:01:36,3:33:22
ZA3642,18:06:20,5:33:59
ZA5453,23:43:56,4:21:13
ZA0559,16:09:05,8:58:24
ZA6903,13:54:48,11:59:58
ZA9604,12:29:53,9:09:07
ZA5348,2:59:13,6:23:48
ZA3835,8:42:41,14:55:44
ZA4949,12:42:13,3:05:13
ZA2951,23:40:10,8:05:41
ZA4940,9:11:30,12:25:47
ZA3121,4:42:50,7:53:03
ZA6470,22:59:30,10:03:19
ZA4981,19:37:53,12:58:38
ZA0647,9:19:03,14:50:22 Give me a code that runs with all the methods
ZA5632,12:23:53,1:46:22
ZA2321,20:46:09,5:07:25
ZA3408,22:48:49,1:17:55
ZA2851,4:28:12,13:45:53
ZA7759,18:35:41,4:25:57 import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Node {
String registrationNo;
String departureTimestamp;
String flightDuration;
long arrivalTime;
Node(String registrationNo, String departureTimestamp, String flightDuration){
this.registrationNo = registrationNo;
this.departureTimestamp = departureTimestamp;
this.flightDuration = flightDuration;
this.arrivalTime = calculateArrivalTime(departureTimestamp, flightDuration);
}
private long calculateArrivalTime(String departureTimestamp, String flightDuration){
// Convert departureTimestamp and flightDuration to milliseconds and add them
// Implementation left for brevity
return 0; // Replace with actual calculation
}
}
class MinHeap {
Node[] heap;
int size;
MinHeap(int capacity){
heap = new Node[capacity];
size =0;
}
void add(Node node){
heap[size]= node;
size++; // Increment size after adding the node
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){
// Heapify up from index
// Implementation left for brevity
}
private void heapifyDown(int index){
// Heapify down from index
// Implementation left for brevity
}
}
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); // Adjust capacity as needed
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("Order of incoming flights:");
while (!minHeap.isEmpty()){
Node node = minHeap.poll();
System.out.println(node.registrationNo);
}
} catch (FileNotFoundException e){
System.out.println("File not found: flights.txt");
}
}
} this is the code that i was given by chegg and it doesnt work as it gives me this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20
at MException in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20
at MinHeap.add(Main.java:35)
at Main.main(Main.java:84)inHeap.add(Main.java:35)), i want full code will all implementation and main method that runs without error and gives me answer

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

More Books

Students also viewed these Databases questions

Question

Identify four forces that are reshaping organizations today.

Answered: 1 week ago

Question

To what extent can we see inside the brain using modern technology?

Answered: 1 week ago