Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

//Автомобиль — объект, содержащий в себе параметры «название» и «скорость».
public class Car {
// на основе класса создаем объект, поля: наименование и скорость.
String name;
int speed;
// конструктор принимает 2 параметра
Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
}
42 changes: 40 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
System.out.println("Hello world!");
ArrayList<Car> cars = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
Race win = new Race();
for (int i = 1; i < 4; i++) {

String name = "";
while (name.isEmpty()) {
System.out.printf("Введите наименование машины №%d ", i);
name = scanner.nextLine().trim();
}

int speed = 0;

while (speed <= 0 || speed > 250) {
System.out.printf("Введите скорость машины №%d ", i);
try {
//speed = scanner.nextInt();
String l = scanner.nextLine();
speed = Integer.parseInt(l.trim());
//Double sm=scanner.nextDouble();

} catch (Exception e) {
System.out.println("Введите целое число от 0 до 250");
}
}


Car car = new Car(name, speed);
cars.add(car);


}
Car fin = win.winner(cars);
System.out.println("Победитель :" + fin.name);
}
}

}

29 changes: 29 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;

public class Race {
public Car winner(ArrayList<Car> cars) {
if (cars.size() == 0 || cars == null) {
return null;
}
Car carWin = cars.get(0);

for (int i = 1; i < cars.size(); i++) {
int distance = 24 * cars.get(i).speed;
if (distance > carWin.speed * 24) {
carWin = cars.get(i);

}

}

return carWin;

}
}