-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа №1 #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: main
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 +1 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| #Проектная работа №1 Бондарев Кирилл |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class Car { | ||
| private String name; | ||
| private int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
|
|
||
| public int calculateDistance() { | ||
| return 24 * speed; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,41 @@ | ||
|
|
||
| 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 i = 1; i <= 3; i++) { | ||
| String name; | ||
| //проверим что пользовател ввел название машины а не пусто | ||
| while (true) { | ||
| System.out.println("Введите название машины №" + i + ":"); | ||
| name = scanner.nextLine().trim(); | ||
| if (!name.isEmpty()) { | ||
| break; | ||
| } | ||
| System.out.println("Название машины не может быть пустым!"); | ||
| } | ||
|
Comment on lines
+10
to
+17
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. Код для считывания непустой строки с ввода лучше вынести в отдельную функцию - код, разделённый на небольшие функции, легче читать, поддерживать и переиспользовать |
||
|
|
||
| int speed; | ||
| //проверяем правильность ввода скорости иначе выкидываем ошибку | ||
| while (true) { | ||
| System.out.println("Введите скорость машины №" + i + ":"); | ||
| try { | ||
| speed = Integer.parseInt(scanner.nextLine()); | ||
| if (speed > 0 && speed <= 250) { | ||
|
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. Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода |
||
| break; | ||
| } else { | ||
| System.out.println("Неправильная скорость (должна быть от 1 до 250)"); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("Неправильная скорость (введите целое число)"); | ||
| } | ||
| } | ||
|
|
||
| Car car = new Car(name, speed); | ||
| race.determineLeader(car); | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + race.getLeaderName()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| public class Race { | ||
| // Класс Гонка | ||
| private String leaderName; | ||
| private int leaderDistance; | ||
|
Comment on lines
+3
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 Race() { | ||
| leaderName = ""; | ||
| leaderDistance = 0; | ||
| } | ||
|
|
||
| public void determineLeader(Car car) { | ||
| int currentDistance = car.calculateDistance(); | ||
| if (currentDistance > leaderDistance) { | ||
| leaderName = car.getName(); | ||
| leaderDistance = currentDistance; | ||
| } | ||
| } | ||
|
|
||
| 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.
Поля лучше пометить
final, тем самым исключив возможность их модификации извне, тогда можно убрать геттеры и поля сделать публичными