-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.txt
93 lines (82 loc) · 3.26 KB
/
sandbox.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Exercise 2 : The number of recorded vehicles
*/
public static String exercise2(ArrayList<Car> cars) {
return cars.size() + " cars recorded";
}
/*
* Exercise 3 : The number of vehicles that passed the exit point before 09:00
*/
public static String exercise3(ArrayList<Car> cars) {
int nCars9 = 0;
for(Car car: cars) {
if(car.getExitHour() < 9) {
nCars9++;
}
}
return nCars9 + " vehicles passed the exit point before 09:00";
}
/*
* Exercise 4 :
* The number of vehicles that passed the entry point and the traffic intensity
* in the timeframe (hour, minute) given as argument
*/
public static String exercise4(ArrayList<Car> cars, int userHour, int userMinute) {
String ret0 = "In hour, minute : " + userHour + ", " + userMinute + " : \n";
int nCarsMinute = 0;
for(Car car: cars) {
if(car.getEntryHour() == userHour && car.getEntryMinute() == userMinute) {
nCarsMinute++;
}
}
String ret1 = nCarsMinute + " vehicles passed the entry point. \n";
int presentMinute = 0;
for(Car car: cars) {
// Current car has passed the entry after the start of the minute
if((car.getEntryHour() == userHour && car.getEntryMinute() == userMinute) || (car.getExitHour() == userHour && car.getExitMinute() == userMinute)) {
presentMinute++;
}
}
double trafficIntensityMinute = presentMinute/10.0;
String ret2 = "The traffic intensity is " + trafficIntensityMinute;
return ret0 + ret1 + ret2;
}
/*
* Exercise 5 : The fastest vehicle, its speed and its overtakes
*/
public static String exercise5(ArrayList<Car> cars) {
Car fastestCar = cars.get(0);
double fastestSpeed = fastestCar.getSpeed();
for(Car car: cars) {
if(car.getSpeed() > fastestSpeed) {
fastestCar = car;
fastestSpeed = car.getSpeed();
}
}
String ret1 = "The fastest vehicle is " + fastestCar.getLicensePlate() + " \n";
String ret2 = "It has an average speed of " + fastestSpeed + " \n";
int overtakes = 0;
for(Car car: cars) {
// Current car has passed the entry point before the fastest car AND fastest car has passed the exit point before the current car
if(car != fastestCar) {
if(car.getEntryAllSeconds() <= fastestCar.getEntryAllSeconds() && car.getExitAllSeconds() >= fastestCar.getExitAllSeconds()) {
overtakes++;
}
}
}
String ret3 = "It has overtaken " + overtakes + " cars";
return ret1 + ret2 + ret3;
}
/*
* Exercise 6 : The percentage of the vehicles that exceeded the maximum speed limit (90 km/h)
*/
public static String exercise6(ArrayList<Car> cars) {
int nExceed = 0;
for(Car car: cars) {
if(car.getSpeed() > 90.0) {
nExceed++;
}
}
double averageExceed = (float) nExceed / (float) cars.size() * 100.0;
return averageExceed + "% of the vehicles were speeding.";
}