-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathMain.java
More file actions
94 lines (64 loc) · 2.43 KB
/
Main.java
File metadata and controls
94 lines (64 loc) · 2.43 KB
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
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
Race race = new Race();
System.out.println("Welcome to Le-Man 24");
Scanner scanner = new Scanner(System.in);
for (int i = 0; i <3; i++) {
System.out.println("Enter the name of the vehicle # " + (i+1));
String name = scanner.next();
int maxSpeed;
do {
try {
System.out.println("Enter the max speed of the vehicle " + name + " (1 - 250 km/h)");
maxSpeed = scanner.nextInt();
if (maxSpeed <= 0 || maxSpeed > 0) {
System.out.println("The speed must be within 1 - 250 km/h range");
}
} catch(InputMismatchException j) {
System.out.println("Error. Enter the correct speed within 1 - 250 km/h");
scanner.next();
maxSpeed = 0;
}
}
while (maxSpeed <= 0 || maxSpeed > 250);
Vehicle vehicle = new Vehicle(name, maxSpeed);
race.determineTheChampion(vehicle);
}
Vehicle theChampion = race.announceTheChampion();
System.out.println(" The Champion of Le-Man 24 is " + theChampion.getName());
}
}
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
class Vehicle {
private String name;
private int maxSpeed;
public Vehicle(String name, int maxSpeed) {
this.name = name;
this.maxSpeed = maxSpeed;
}
public String getName() {
return name;
}
public int getMaxSpeed() {
return maxSpeed;
}
}
class Race {
public Vehicle theChampion;
public Race() {
this.theChampion = null;
}
public void determineTheChampion(Vehicle vehicle) {
int theLongestDistance = vehicle.getMaxSpeed() * 24;
System.out.println("Vehicle " + vehicle.getName() + " runs " + theLongestDistance + " km for 24 hours");
if (theChampion == null || vehicle.getMaxSpeed() > theChampion.getMaxSpeed()) {
theChampion = vehicle;
}
}
public Vehicle announceTheChampion() {
return theChampion;
}
}