Skip to content

Commit 02e7016

Browse files
committed
Air traffic controller and Problems added
1 parent fee50ea commit 02e7016

File tree

7 files changed

+257
-3
lines changed

7 files changed

+257
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
5+
class AirTrafficControl extends Thread {
6+
String flightName;
7+
int flightWeight;
8+
int compTime;
9+
int ch;
10+
String cho = "";
11+
ArrayList<Flight> flights;
12+
ArrayList<RunWay> runWays;
13+
Scanner sc = new Scanner(System.in);
14+
15+
AirTrafficControl() {
16+
Flight atr = new Flight("atr", 12, 30); // name,max weight,time to halt
17+
Flight airbus = new Flight("airbus", 20, 40);
18+
Flight boeing = new Flight("boeing", 40, 50);
19+
Flight cargo = new Flight("cargo", 100, 60);
20+
flights = new ArrayList<>(Arrays.asList(atr, airbus, boeing, cargo));
21+
22+
RunWay r1 = new RunWay("run way one", 40, true);
23+
RunWay r2 = new RunWay("run way two", 60, true);
24+
RunWay r3 = new RunWay("run way three", 80, true);
25+
RunWay r4 = new RunWay("run way four", 90, true);
26+
runWays = new ArrayList<>(Arrays.asList(r1, r2, r3, r4));
27+
}
28+
29+
public static void main(String[] args) {
30+
AirTrafficControl atc = new AirTrafficControl();
31+
while (true) {
32+
atc.getChoice();
33+
}
34+
}
35+
36+
public void getChoice() {
37+
System.out.println(
38+
"1.Take off\n2.Landing\n3.Emergency Landing\n4.show RunWays\n5.Exit\nEnter Your Choice :");
39+
ch = sc.nextInt();
40+
switch (ch) {
41+
case 1:
42+
cho = "Take Off";
43+
getAndAllocate();
44+
break;
45+
case 2:
46+
cho = "Landing";
47+
getAndAllocate();
48+
break;
49+
case 3:
50+
cho = "Emergency Landing";
51+
getAndAllocate();
52+
break;
53+
case 4:
54+
runWays.forEach(System.out::println);
55+
break;
56+
case 5:
57+
System.exit(0);
58+
}
59+
}
60+
61+
private void getAndAllocate() {
62+
System.out.println("Enter name of flight:");
63+
flightName = sc.next();
64+
Flight getFlight =
65+
flights.stream().filter(f -> f.flightName.equals(flightName)).findFirst().orElse(null);
66+
if (getFlight != null) {
67+
System.out.println("Enter weight of flight(in tons):");
68+
flightWeight = sc.nextInt();
69+
compTime = getFlight.computeTime(flightWeight);
70+
checkAndAssignRunWay();
71+
} else System.out.println("Flight doesn't authorised");
72+
}
73+
74+
public void checkAndAssignRunWay() {
75+
RunWay selectedRunWay =
76+
runWays.stream().filter(r -> compTime <= r.time && r.status).findFirst().orElse(null);
77+
if (selectedRunWay != null) {
78+
Request r = new Request(flightName, flightWeight, cho, compTime, selectedRunWay);
79+
r.start();
80+
try {
81+
Thread.sleep(500);
82+
} catch (Exception ignored) {
83+
}
84+
} else System.out.println("you have to wait");
85+
}
86+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Flight {
2+
String flightName;
3+
int maxWeight;
4+
int allottedTime;
5+
6+
Flight(String fName, int maxWeight, int timeHalt) {
7+
this.flightName = fName;
8+
this.maxWeight = maxWeight;
9+
this.allottedTime = timeHalt;
10+
}
11+
12+
int computeTime(int weight) {
13+
int weightPercentage = (weight / maxWeight) * 100; // to get percentage of weight
14+
int compileTime;
15+
compileTime =
16+
weightPercentage >= 75
17+
? allottedTime
18+
: (weightPercentage > 50 && weightPercentage < 75)
19+
? allottedTime - (allottedTime * (10 / 100))
20+
: allottedTime - (allottedTime * (20 / 100));
21+
22+
return compileTime + 10; // always add 10 sec extra...
23+
}
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Request extends Thread {
2+
String requestedFlight; // flight name
3+
int flightWeight; // flight weight
4+
int coolTime;
5+
6+
String type;
7+
RunWay runWay;
8+
9+
public Request(String aName, int weightA, String cho, int compTime, RunWay rw) {
10+
this.requestedFlight = aName;
11+
this.flightWeight = weightA;
12+
this.runWay = rw;
13+
this.coolTime = compTime;
14+
this.type = cho;
15+
}
16+
17+
public void run() {
18+
runWay.status = false;
19+
try {
20+
System.out.println(
21+
"---------------------------------------------------------------------------------");
22+
System.out.println(
23+
type + " Approved for " + requestedFlight + " with " + flightWeight + " tons of weight in " + runWay.name);
24+
System.out.println("Touch down will happen in 15 sec");
25+
System.out.println("the plane will stop after " + coolTime + " sec of touch down");
26+
System.out.println(runWay.name + " will be ready for next request in " + coolTime + " sec");
27+
System.out.println(
28+
"---------------------------------------------------------------------------------");
29+
Thread.sleep(1000L * coolTime);
30+
runWay.status = true;
31+
} catch (Exception ignored) {
32+
}
33+
}
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class RunWay {
2+
String name;
3+
int time;
4+
boolean status;
5+
6+
RunWay(String name, int time, boolean status) {
7+
this.name = name;
8+
this.time = time;
9+
this.status = status;
10+
}
11+
12+
@Override
13+
public String toString() {
14+
return name + " is " + (status ? "free" : "busy");
15+
}
16+
}

PatternPrinting/src/PatternPrinting.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,50 @@ public static void main(String[] args) {
1616
spiralPattern();
1717
mergeTwoArraysWithoutDuplicates();
1818
printNumberInWords();
19+
slidingIndex();
20+
patternPrinting();
21+
System.out.println(
22+
Arrays.toString(placeZerosLast(new int[] {8, 0, 4, 0, 2, 0, 1, 3, 55, 5001})));
23+
}
24+
25+
private static int[] placeZerosLast(int[] numbs) {
26+
int[] newArr = new int[numbs.length];
27+
int count = 0;
28+
for (int num : numbs) {
29+
if (num != 0) newArr[count++] = num;
30+
}
31+
return newArr;
32+
}
33+
34+
private static void patternPrinting() {
35+
int num = 5;
36+
String start = "1";
37+
System.out.println(start);
38+
for (int i = 0; i < num - 1; i++) {
39+
StringBuilder str = new StringBuilder();
40+
for (int j = 0; j < start.length(); j++) {
41+
int cnt = 1;
42+
while (j + 1 < start.length() && start.charAt(j) == start.charAt(j + 1)) {
43+
cnt++;
44+
j++;
45+
}
46+
str.append(cnt).append(start.charAt(j));
47+
}
48+
start = str.toString();
49+
System.out.println(start);
50+
}
51+
}
52+
53+
private static void slidingIndex() {
54+
int[] arr = {1, 2, 3, 1, 4, 5, 2, 3, 6};
55+
int k = 3;
56+
for (int i = 0; i <= arr.length - k; i++) {
57+
int max = arr[i];
58+
for (int j = 1; j < k; j++) {
59+
if (arr[i + j] > max) max = arr[i + j];
60+
}
61+
System.out.print(max + " ");
62+
}
1963
}
2064

2165
private static void printNumberInWords() {
@@ -122,7 +166,7 @@ private static void wordPattern() {
122166
}
123167

124168
private static void spiralPattern() {
125-
int matrix[][] = {
169+
int[][] matrix = {
126170
{1, 2, 3, 4}, {14, 15, 16, 5}, {13, 20, 17, 6}, {12, 19, 18, 7}, {11, 10, 9, 8}
127171
};
128172

@@ -232,14 +276,13 @@ private static int[] insertionSort(int[] intArray) {
232276
return intArray;
233277
}
234278

235-
private static int[] descendingSort(int[] intArray) {
279+
private static void descendingSort(int[] intArray) {
236280
for (int i = 1; i < intArray.length; i++) {
237281
int previousIndex = i - 1, currentValue = intArray[i];
238282
while ((previousIndex > -1) && intArray[previousIndex] < currentValue) {
239283
intArray[previousIndex + 1] = intArray[previousIndex--];
240284
}
241285
intArray[previousIndex + 1] = currentValue;
242286
}
243-
return intArray;
244287
}
245288
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.ArrayList;
2+
3+
class minCostPathRecursion {
4+
public static void main(String[] args) {
5+
int[][] cost = {
6+
{1, 1, 1, 1, 1},
7+
{0, 1, 0, 0, 0},
8+
{0, 1, 1, 1, 0},
9+
{0, 1, 0, 1, 1},
10+
{0, 1, 1, 1, 1}
11+
};
12+
13+
System.out.println(
14+
"The minimum cost is " + findMinCostUsingRecursion(cost, cost.length, cost[0].length));
15+
findValueInMaze(cost, 0, 0, new ArrayList<>());
16+
}
17+
18+
19+
private static void findValueInMaze(int[][] cost, int x, int y, ArrayList<String> Path) {
20+
if (x < cost.length && y < cost[0].length && cost[x][y] != 0) {
21+
Path.add("(" + x + ", " + y + ") ");
22+
if (x == cost.length - 1 && y == cost[0].length - 1) {
23+
System.out.println(Path + "Total Jumps = " + (Path.size() - 1));
24+
System.exit(0);
25+
}
26+
findValueInMaze(cost, x + 1, y, Path);
27+
findValueInMaze(cost, x, y + 1, Path);
28+
Path.remove(Path.size() - 1);
29+
}
30+
}
31+
32+
private static int findMinCostUsingRecursion(int[][] cost, int row, int col) {
33+
if (row == 0 || col == 0 || cost[row - 1][col - 1] == 0) return Integer.MAX_VALUE;
34+
if (row == 1 && col == 1) return cost[0][0];
35+
return Integer.min(
36+
findMinCostUsingRecursion(cost, row - 1, col),
37+
findMinCostUsingRecursion(cost, row, col - 1))
38+
+ cost[row - 1][col - 1];
39+
}
40+
}

0 commit comments

Comments
 (0)