Repository navigation
commit #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?
commit #1 #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,15 @@ | ||
| public class Car { | ||
| final static int HOURS = 24; | ||
| 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 calcDist() { | ||
| return speed * HOURS; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,51 @@ | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Main { | ||
| final static int MIN_SPEED = 1; | ||
| final static int MAX_SPEED = 250; | ||
| final static int NUM_CARS = 3; | ||
|
Comment on lines
+5
to
+7
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. Всю логику из класса Main лучше вынести в отдельный класс и файл, чтобы в Main остался только запуск программы |
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| ArrayList<Car> cars = new ArrayList<>(); | ||
|
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. От хранения массива машин и лишнего цикла при определении победителя можно избавиться, если при вводе данных сразу вычислять победителя и хранить его в отдельной переменной, тогда программа будет требовать меньше памяти и работать быстрее |
||
|
|
||
| for (int i = 1; i <= NUM_CARS; i++) { | ||
| boolean trueName = false; | ||
| System.out.println("Введите автомобиль №" + i); | ||
| String name = ""; | ||
| while (!trueName) { | ||
| System.out.print("Наименование: "); | ||
| name = scanner.nextLine().trim(); | ||
| if (name.isEmpty()) | ||
| System.out.println("Ошибка. Пустое название недопустимо"); | ||
| else | ||
| trueName = true; | ||
| } | ||
|
Comment on lines
+16
to
+23
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 = 0; | ||
| boolean trueSpeed = false; | ||
|
|
||
| while (!trueSpeed) { | ||
| System.out.print("Скорость в км/ч, от " + MIN_SPEED + " до " + MAX_SPEED + ": "); | ||
| if (scanner.hasNextInt()) { | ||
| speed = scanner.nextInt(); | ||
| if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)) | ||
| trueSpeed = true; | ||
| else | ||
| System.out.println("Ошибка. Введите допустимую скорость"); | ||
| } else | ||
| System.out.println("Ошибка. Введите целое число"); | ||
| scanner.nextLine(); | ||
| } | ||
|
Comment on lines
+28
to
+39
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. Код для считывания скорости тоже лучше вынести в отдельную функцию |
||
|
|
||
| cars.add(new Car(name, speed)); | ||
| } | ||
|
|
||
| Race race = new Race(); | ||
| race.winner(cars); | ||
| System.out.println("\nСамая быстрая машина: " + race.getWinner().getName()); | ||
|
|
||
| scanner.close(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import java.util.ArrayList; | ||
| public class Race { | ||
| private Car winner; | ||
| private int maxDist = 0; | ||
| public void winner(ArrayList<Car> cars) { | ||
| for (Car car : cars) { | ||
| int dist = car.calcDist(); | ||
| if (dist > maxDist) { | ||
| maxDist = dist; | ||
| winner = car; | ||
| } | ||
| } | ||
| } | ||
| public Car getWinner() { | ||
| return winner; | ||
| } | ||
| } |
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.
Константу лучше вынести в класс Race, так как эта константа означает продолжительность гонки