Repository navigation
homework #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: car-and-race
Are you sure you want to change the base?
homework #1
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
|
|
||
| public class Car { | ||
| public String name; | ||
| public int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,48 @@ | ||
| 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(); | ||
|
|
||
| for (int carNumber = 1; carNumber <= 3; carNumber++) { | ||
|
|
||
| String name = askCarName(scanner, carNumber); | ||
| int speed = askCarSpeed(scanner, carNumber); | ||
|
|
||
| Car car = new Car(name, speed); | ||
| race.checkNewLeader(car); | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.leaderName); | ||
| scanner.close(); | ||
|
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 static String askCarName(Scanner scanner, int carNumber) { | ||
| System.out.print("Введите название машины №" + carNumber + ": "); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
|
|
||
| public static int askCarSpeed(Scanner scanner, int carNumber) { | ||
|
|
||
| while (true) { | ||
| System.out.print("Введите скорость машины №" + carNumber + " (от 1 до 250): "); | ||
| String input = scanner.nextLine(); | ||
|
|
||
| int speed = Integer.parseInt(input); | ||
|
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. Данный вызов может выбросить креш, если вместо числа ввести символы - этот эксепшн необходимо обработать, предложив пользователю ввести данные заново |
||
|
|
||
| if (speed >= 1 && speed <= 250) { | ||
| return speed; | ||
| } else { | ||
| System.out.println("Ошибка: скорость должна быть в диапазоне от 1 до 250. Попробуйте снова."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Race { | ||
| String leaderName = ""; | ||
| int maxDistance = 0; | ||
| int timeOfTheRace = 24; | ||
|
Comment on lines
+2
to
+4
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 void checkNewLeader(Car car) { | ||
| int distance = car.speed * timeOfTheRace; | ||
| if (distance > maxDistance) { | ||
| maxDistance = distance; | ||
| leaderName = car.name; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
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.
Поля лучше пометить
final, тем самым исключив возможность их модификации извне