Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.ArrayList; import java.util.Comparator; class Train { int trainId; String arrivalTime; String departureTime; public Train ( int trainId, String arrivalTime, String departureTime ) { this.trainId

import java.util.ArrayList;
import java.util.Comparator;
class Train
{
int trainId;
String arrivalTime;
String departureTime;
public Train
(
int trainId, String arrivalTime, String departureTime
)
{
this.trainId
=
trainId;
this.arrivalTime
=
arrivalTime;
this.departureTime
=
departureTime;
}
}
class TrainScheduleManager
{
ArrayList trains;
public TrainScheduleManager
(
)
{
this.trains
=
new ArrayList
<
>
(
)
;
}
public void addTrain
(
int trainId, String arrivalTime, String departureTime
)
{
Train newTrain
=
new Train
(
trainId
,
arrivalTime, departureTime
)
;
int index
=
binarySearchInsertPosition
(
arrivalTime
)
;
trains.add
(
index
,
newTrain
)
;
}
private int binarySearchInsertPosition
(
String targetArrivalTime
)
{
int low
=
0
;
int high
=
trains.size
(
)
-
1
;
while
(
low
<
=
high
)
{
int mid
=
low
+
(
high
-
low
)
/
2
;
String midArrivalTime
=
trains.get
(
mid
)
.
arrivalTime;
if
(
midArrivalTime
.
compareTo
(
targetArrivalTime
)
<
0
)
{
low
=
mid
+
1
;
}
else if
(
midArrivalTime
.
compareTo
(
targetArrivalTime
)
>
0
)
{
high
=
mid
-
1
;
}
else
{
/
/
Handle cases where arrival times are the same
(
unlikely for this problem
)
low
=
mid
+
1
;
/
/
Insert after duplicates
(
if any
)
}
}
return low;
/
/
Insert position
}
public void removeTrain
(
int trainId
)
{
for
(
int i
=
0
; i
<
trains.size
(
)
; i
+
+
)
{
if
(
trains
.
get
(
i
)
.
trainId
=
=
trainId
)
{
trains.remove
(
i
)
;
break;
}
}
}
public void displaySchedule
(
)
{
for
(
Train train : trains
)
{
System.out.println
(
"
Train ID:
"
+
train.trainId
+
"
|
Arrival:
"
+
train.arrivalTime
+
"
|
Departure:
"
+
train.departureTime
)
;
}
}
}
public class Main
{
public static void main
(
String
[
]
args
)
{
TrainScheduleManager manager
=
new TrainScheduleManager
(
)
;
manager.addTrain
(
1
,
"
0
8
:
0
0
"
,
"
0
8
:
3
0
"
)
;
manager.addTrain
(
2
,
"
0
9
:
0
0
"
,
"
0
9
:
3
0
"
)
;
manager.addTrain
(
3
,
"
0
9
:
3
0
"
,
"
1
0
:
0
0
"
)
;
manager.addTrain
(
4
,
"
1
0
:
0
0
"
,
"
1
0
:
3
0
"
)
;
System.out.println
(
"
Initial Train Schedule:"
)
;
manager.displaySchedule
(
)
;
manager.removeTrain
(
2
)
;
System.out.println
(
"
Updated Train Schedule after removing Train
2
:
"
)
;
manager.displaySchedule
(
)
;
}
}
describe the algotithm used to solve this problem

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

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

Students also viewed these Databases questions

Question

Solve Prob. 27.4 with the finite-difference approach using x = 2.

Answered: 1 week ago