Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (48 loc) · 1.5 KB
/
Copy pathMain.java
File metadata and controls
59 lines (48 loc) · 1.5 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String autoName;
int autoVelocity;
Scanner scanner = new Scanner(System.in);
Gonka gonka = new Gonka();
for (int i = 1; i <= 3; i++) {
System.out.println("Enter name of auto # " + i + ":");
autoName = scanner.next();
while (true) {
System.out.println("Enter velocity of auto # " + i + ":");
try {
autoVelocity = scanner.nextInt();
if (autoVelocity >= 0 && autoVelocity <= 250) {
break;
}
}
catch (java.util.InputMismatchException e) {
scanner.next();
}
System.out.println("Wrong velocity.");
}
gonka.findLeader(new Automobil(autoName, autoVelocity));
}
System.out.println("the fastest auto is: " + gonka.leader);
}
}
class Automobil {
String name;
int velocity;
Automobil(String name, int velocity) {
this.name = name;
this.velocity = velocity;
}
}
class Gonka {
String leader = "";
int leaderDistance = 0;
void findLeader(Automobil newAuto) {
int time = 24;
int newAutoDistance = time * newAuto.velocity;
if (newAutoDistance > leaderDistance) {
leader = newAuto.name;
leaderDistance = newAutoDistance;
}
}
}