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
11 changes: 11 additions & 0 deletions Practice№1/Practice№1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
1 change: 1 addition & 0 deletions Practice№1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Практическая №1.
9 changes: 9 additions & 0 deletions Practice№1/src/main/java/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Car {
String name;
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, тем самым исключив возможность их модификации извне

Car(String name, int speed){
this.name = name;
this.speed = speed;
}

}
55 changes: 55 additions & 0 deletions Practice№1/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {

int numberCars=3;

Car[] cars = new Car[numberCars];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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


for (int i = 0; i < cars.length; i++) {
System.out.println("Введите название машины №" + (i + 1) + ":");
String nameCar = checkCarName();
System.out.println("Введите скорость машины №" + (i + 1) + ":");
int speedCar = checkCarSpeed();
cars[i] = new Car(nameCar, speedCar);
}

Race.leManRace(cars);
}



static String checkCarName() {
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.

Сканнер лучше не создавать на каждое чтение, т.к. при создании сканнеры открывается новый входной поток, поэтому лучше 1 раз создать и переиспользовать

String name;
while (true) {
name = scanner.nextLine();
if (!(name.trim().isEmpty())) {
return name;
} else {
System.out.println("Пустая строка!");
}
}
}


public static int checkCarSpeed() {
Scanner scanner = new Scanner(System.in);
while (true) {
if (scanner.hasNextInt()) {
int speed = scanner.nextInt();
if ((0 < speed) && (speed <= 250)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Минимальную и максимальную скорости лучше вынести в константы для повышения читабельности кода

return speed;

} else {
System.out.println("Неправильная скорость!");
}
} else {
System.out.println("Введите целое число:");
scanner.next();
}
}
}


}
22 changes: 22 additions & 0 deletions Practice№1/src/main/java/Race.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Race {

static void leManRace(Car[] cars){
int hours = 24;
String fastCar ="";
for (int i=1; i<cars.length; i++) {
if (calculateDistance(cars[i].speed, hours) > calculateDistance(cars[i - 1].speed, hours)) {
fastCar = cars[i].name;
} else {
fastCar = cars[i - 1].name;
}
}
System.out.println("Самая быстрая машина: "+fastCar);
}



static int calculateDistance(int speed,int hours){
return speed * hours;
}
}