Repository navigation
Моя первая домашка в практикуме #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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Car { | ||
| private final String name; | ||
| private final int speed; | ||
|
|
||
| public Car(String name, int speed) { | ||
| this.name = name; | ||
| this.speed = speed; | ||
| } | ||
| public String getName() { | ||
| return name; | ||
| } | ||
| public int getSpeed() { | ||
| return speed; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,43 @@ | ||
| import java.util.Scanner; | ||
| import java.util.InputMismatchException; | ||
|
|
||
| public class Main { | ||
|
|
||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Race race=new Race(); | ||
| Car[] cars=new Car[3]; | ||
|
|
||
| for (int i = 0; i < 3; i++) { | ||
| enterNameString(i+1, "название"); | ||
| String carName = scanner.next(); | ||
| enterNameString(i+1, "скорость"); | ||
| int carSpeed = speedChecking(); | ||
| while(!(carSpeed > 0 && carSpeed <= 250)) { | ||
| System.out.println("Неправильная скорость"); | ||
| enterNameString(i+1, "скорость"); | ||
| carSpeed = speedChecking(); | ||
| } | ||
|
|
||
| cars[i]=new Car(carName, carSpeed); | ||
|
|
||
| } | ||
| race.bestCar(cars); | ||
|
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 int speedChecking() { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
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. Сканнер лучше создать один раз на уровне класса и переиспользовать, т.к. создание этого объекта - тяжёлая операция |
||
| try { | ||
| return scanner.nextInt(); | ||
| } catch (InputMismatchException e) { | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void enterNameString(int i, String s) { | ||
| System.out.println("Введите "+ s+" машины №"+i); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class Race { | ||
| private String leader; | ||
| private int distance; | ||
|
|
||
| public Race() { | ||
| this.leader=""; | ||
| this.distance=0; | ||
| } | ||
|
|
||
|
|
||
| public void bestCar (Car[] cars) { | ||
| for (Car car : cars) { | ||
| if (distance < car.getSpeed()*24) { | ||
| distance = car.getSpeed()*24; | ||
| leader= car.getName(); | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Самая быстрая машина: " + leader); | ||
| } | ||
|
|
||
| } |
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, то можно не делать их приватными и писать геттеры, а просто сделать публичными - это безопасно, т.к. доступ к этим переменным будет только на чтение за счётfinal