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
16 changes: 16 additions & 0 deletions src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Car {
private final String name;
private final int speed;
Comment on lines +2 to +3

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так как обе переменные являются final, то можно не делать их приватными и писать геттеры, а просто сделать публичными - это безопасно, т.к. доступ к этим переменным будет только на чтение за счёт final


public Car(String name, int speed) {
this.name = name;
this.speed = speed;
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
}

41 changes: 39 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import java.util.Scanner;
import java.util.InputMismatchException;

public class Main {



public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
Race race=new Race();
Car[] cars=new Car[3];

for (int i = 0; i < 3; i++) {
enterNameString(i+1, "название");
String carName = scanner.next();
enterNameString(i+1, "скорость");
int carSpeed = speedChecking();
while(!(carSpeed > 0 && carSpeed <= 250)) {
System.out.println("Неправильная скорость");
enterNameString(i+1, "скорость");
carSpeed = speedChecking();
}

cars[i]=new Car(carName, carSpeed);

}
race.bestCar(cars);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно сразу при чтении данных выявлять нового победителя, тогда не придётся хранить весь массив машин в программе и тогда программа будет требовать меньше памяти для её выполнения

}

public static int speedChecking() {
Scanner scanner = new Scanner(System.in);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сканнер лучше создать один раз на уровне класса и переиспользовать, т.к. создание этого объекта - тяжёлая операция

try {
return scanner.nextInt();
} catch (InputMismatchException e) {
return 0;
}
}
}

public static void enterNameString(int i, String s) {
System.out.println("Введите "+ s+" машины №"+i);
}
}

22 changes: 22 additions & 0 deletions src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Race {
private String leader;
private int distance;

public Race() {
this.leader="";
this.distance=0;
}


public void bestCar (Car[] cars) {
for (Car car : cars) {
if (distance < car.getSpeed()*24) {
distance = car.getSpeed()*24;
leader= car.getName();
}
}

System.out.println("Самая быстрая машина: " + leader);
}

}