Repository navigation
Added class Car and Race with new methods and fields. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,85 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race = new Race(); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
|
||
| System.out.println("Добро пожаловать в 24 часа Ле-Мана!!!"); | ||
| for (int i = 1; i <= 3; i++) { | ||
| System.out.println("Введите название машины №" + i + ":"); | ||
| String name = scanner.nextLine(); | ||
|
|
||
| int speed = 0; | ||
| while (true) { | ||
| System.out.println("Введите скорость машины №" + i + ":"); | ||
| try { | ||
| speed = Integer.parseInt(scanner.nextLine()); | ||
| if (speed > 0 && speed <= 250) { | ||
| break; | ||
| } else { | ||
| System.out.println("Введена неправильная скорость! Скорость должна быть от 1 до 250."); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Введите корректное целое число от 1 до 250."); | ||
| } | ||
| } | ||
|
|
||
| Car car = new Car(name, speed); | ||
| cars.add(car); | ||
| } | ||
|
|
||
| for (Car car : cars) { | ||
| race.findLeader(car); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. От хранения массива машин можно избавиться, если прямо при чтении данных определять и запоминать только победителя. Таким образом, программа будет работать быстрее и требовать меньше памяти |
||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.getLeaderName()); | ||
| } | ||
| } | ||
|
|
||
| class Car { | ||
| private String name; | ||
| private int speed; | ||
|
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поля лучше пометить Плюс можно убрать |
||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } | ||
|
|
||
| class Race { | ||
| private String leaderName; | ||
| private int maxDistance; | ||
|
|
||
| public void findLeader(Car car) { | ||
| int distance = car.getSpeed() * 24; | ||
| if (distance > maxDistance) { | ||
| maxDistance = distance; | ||
| leaderName = car.getName(); | ||
| } | ||
| } | ||
|
|
||
| public String getLeaderName() { | ||
| return leaderName; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Минимальную и максимальную допустимые скорости лучше вынести в константы с говорящими названиями для повышения читабельности кода