Skip to content
21 changes: 21 additions & 0 deletions src/main/java/Automobile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Automobile {
public String name;
public double speed; // 0..250

public Automobile(String name) {
this.name = name;
}

public Automobile(String name, double startSpeed) {
this.name = name;
this.speed = startSpeed;
}

public void stop() {
this.speed = 0;
}

public double calculateDistance(int hours) {
return hours * speed;
}
}
20 changes: 20 additions & 0 deletions src/main/java/Gonka.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Gonka {
Automobile[] autos;

public Gonka(Automobile[] autos) {
this.autos = autos;
}

public Automobile startAndGetLeader(int hours) {
double max_distance = -1.0;
Automobile leader = null;
for (Automobile a : autos) {
double distance = a.calculateDistance(hours);
if (distance > max_distance) {
max_distance = distance;
leader = a;
}
}
return leader;
}
}
35 changes: 34 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner in = new Scanner(System.in);
Automobile[] autos = new Automobile[3]; //Автомобили - участники гонки
// Переменные для информации об автомобиле
double speed;
String name = "";
// Ввод
for (short i = 0; i < 3; i++) {
name = "";
while (name.isEmpty()) {
System.out.println("Введите название автомобиля " + (i + 1));
name = in.nextLine(); //допускаем имя автомобиля с пробелами
}
speed = -1.0;
while (!((speed >= 0) && (speed <= 250))) {
System.out.println("Введите скорость автомобиля " + (i + 1));
String speedStr = in.nextLine();
try {
speed = Double.parseDouble(speedStr);
} catch (Exception e) {
System.out.println("Неправильная скорость!");
continue;
}
if (!((speed >= 0) && (speed <= 250))) {
System.out.println("Неправильная скорость!");
}
}
Automobile auto = new Automobile(name, speed);
autos[i] = auto;
}
System.out.println("Ввод успешно завершён!");
Gonka gonka = new Gonka(autos);
Automobile leader = gonka.startAndGetLeader(24);
System.out.println("Самая быстрая машина: " + leader.name);
}
}