From b794721b74534f5ebd790adaafeb32640546885c Mon Sep 17 00:00:00 2001 From: Ilya Lychkou Date: Sat, 29 Aug 2020 21:07:45 +0300 Subject: [PATCH 1/5] lesson 4 task3 fixed - add classes TriangleService and Validator --- .../javafordev/lesson4/task3/Triangle.java | 66 +--------- .../lesson4/task3/TriangleRunner.java | 75 ++++------- .../lesson4/task3/TriangleService.java | 117 ++++++++++++++++++ .../javafordev/lesson4/task3/Validator.java | 53 ++++++++ 4 files changed, 200 insertions(+), 111 deletions(-) create mode 100644 src/com/javafordev/lesson4/task3/TriangleService.java create mode 100644 src/com/javafordev/lesson4/task3/Validator.java diff --git a/src/com/javafordev/lesson4/task3/Triangle.java b/src/com/javafordev/lesson4/task3/Triangle.java index 05b3f25..8f8327f 100644 --- a/src/com/javafordev/lesson4/task3/Triangle.java +++ b/src/com/javafordev/lesson4/task3/Triangle.java @@ -1,6 +1,7 @@ package com.javafordev.lesson4.task3; -import static java.lang.StrictMath.abs; +import static com.javafordev.lesson4.task3.Validator.isTriangleCorrect; +import static com.javafordev.lesson4.task3.Validator.validateSide; import static java.lang.StrictMath.pow; /** @@ -59,26 +60,15 @@ public Triangle(double sideOne, double sideTwo, double sideThree) { } } - public double getSquare() { - double perimeter = this.getPerimeter(); + public double calculateSquare() { + double perimeter = this.calculatePerimeter(); return Math.sqrt(0.5 * perimeter * (0.5 * perimeter - this.sideOne) * (0.5 * perimeter - this.sideTwo) * (0.5 * perimeter - this.sideThree)); } - public double getPerimeter() { + public double calculatePerimeter() { return this.getSideOne() + this.getSideTwo() + this.getSideThree(); } - public static boolean isTriangleCorrect(double sideOne, double sideTwo, double sideThree) { - boolean condition1 = getCondition(sideOne, sideTwo, sideThree); - boolean condition2 = getCondition(sideTwo, sideOne, sideThree); - boolean condition3 = getCondition(sideThree, sideOne, sideTwo); - return (condition1 && condition2 && condition3); - } - - public static boolean getCondition(double side1, double side2, double side3) { - return (side1 < (side2 + side3)) && (side1 > (abs(side2 - side3))); - } - public boolean isTriangleNinetyAngle() { boolean condition1 = (pow(this.getSideOne(), 2)) == (pow(this.getSideTwo(), 2) + pow(this.getSideThree(), 2)); boolean condition2 = (pow(this.getSideTwo(), 2)) == (pow(this.getSideOne(), 2) + pow(this.getSideThree(), 2)); @@ -97,51 +87,5 @@ public boolean isTriangleEquilateral() { return (this.getSideOne() == this.getSideTwo() && this.getSideOne() == this.getSideThree()); } - public static double getMinTriangleByFilter(Triangle[] triangles, String filter) { - double minElement = 0; - if (filter.equals("square")) { - minElement = triangles[0].getSquare(); - for (int i = 0; i < triangles.length; i++) { - if ((triangles[i] != null) && (triangles[i].getSquare() < minElement)) { - minElement = triangles[i].getSquare(); - } - } - } else if (filter.equals("perimeter")) { - minElement = triangles[0].getPerimeter(); - for (int i = 0; i < triangles.length; i++) { - if ((triangles[i] != null) && (triangles[i].getPerimeter() < minElement)) { - minElement = triangles[i].getPerimeter(); - } - } - } - return minElement; - } - - public static double getMaxTriangleByFilter(Triangle[] triangles, String filter) { - double maxElement = 0; - if (filter.equals("square")) { - maxElement = triangles[0].getSquare(); - for (int i = 0; i < triangles.length; i++) { - if ((triangles[i] != null) && (triangles[i].getSquare() > maxElement)) { - maxElement = triangles[i].getSquare(); - } - } - } else if (filter.equals("perimeter")) { - maxElement = triangles[0].getPerimeter(); - for (int i = 0; i < triangles.length; i++) { - if ((triangles[i] != null) && (triangles[i].getPerimeter() > maxElement)) { - maxElement = triangles[i].getPerimeter(); - } - } - } - return maxElement; - } - - public static double validateSide(double side) { - if (side <= 0) { - System.out.println("Задана сторона треугольника <=0"); - } - return side; - } } diff --git a/src/com/javafordev/lesson4/task3/TriangleRunner.java b/src/com/javafordev/lesson4/task3/TriangleRunner.java index 1815db3..77e0c45 100644 --- a/src/com/javafordev/lesson4/task3/TriangleRunner.java +++ b/src/com/javafordev/lesson4/task3/TriangleRunner.java @@ -1,8 +1,5 @@ package com.javafordev.lesson4.task3; -import static com.javafordev.lesson4.task3.Triangle.getMaxTriangleByFilter; -import static com.javafordev.lesson4.task3.Triangle.getMinTriangleByFilter; - public class TriangleRunner { /** * Тестовые значения: @@ -31,68 +28,46 @@ public static void main(String[] args) { triangles[10] = new Triangle(2, 3, 4); // произвольныи triangles[11] = new Triangle(11, 12, 22); // произвольныи - int countOfNinetyAngleTriangle = 0; - int countOfIsoscelesTriangle = 0; - int countOfEquilateralTriangle = 0; - int countOfRandomTriangle = 0; - - Triangle[] ninetyAngleTriangles = new Triangle[countOfTriangles]; - Triangle[] isoscelesTriangles = new Triangle[countOfTriangles]; - Triangle[] equilateralTriangles = new Triangle[countOfTriangles]; - Triangle[] randomTriangles = new Triangle[countOfTriangles]; + TriangleService triangleService = new TriangleService(triangles); + TriangleService ninetyAngleTrianglesService = new TriangleService(triangleService.findByNinetyAngle()); + TriangleService isoscelesTrianglesService = new TriangleService(triangleService.findByIsosceles()); + TriangleService equilateralTrianglesService = new TriangleService(triangleService.findByEquilateral()); + TriangleService randomTrianglesService = new TriangleService(triangleService.findByRandom()); - for (Triangle element : triangles) { - if (element.isTriangleNinetyAngle()) { - ninetyAngleTriangles[countOfNinetyAngleTriangle] = element; - countOfNinetyAngleTriangle++; - } - if (element.isTriangleIsosceles()) { - isoscelesTriangles[countOfIsoscelesTriangle] = element; - countOfIsoscelesTriangle++; - } - if (element.isTriangleEquilateral()) { - equilateralTriangles[countOfEquilateralTriangle] = element; - countOfEquilateralTriangle++; - } - if (!(element.isTriangleNinetyAngle()) && !(element.isTriangleIsosceles()) && !(element.isTriangleEquilateral())) { - randomTriangles[countOfRandomTriangle] = element; - countOfRandomTriangle++; - } - } System.out.println("Инфо о количестве треугольников по типам..."); - System.out.println("Количество прямоугольных треугольников : " + countOfNinetyAngleTriangle); - System.out.println("Количество равнобедренных треугольников : " + countOfIsoscelesTriangle); - System.out.println("Количество равносторонних треугольников : " + countOfEquilateralTriangle); - System.out.println("Количество произвольных треугольников : " + countOfRandomTriangle); + System.out.println("Количество прямоугольных треугольников : " + ninetyAngleTrianglesService.getTriangles().length); + System.out.println("Количество равнобедренных треугольников : " + isoscelesTrianglesService.getTriangles().length); + System.out.println("Количество равносторонних треугольников : " + equilateralTrianglesService.getTriangles().length); + System.out.println("Количество произвольных треугольников : " + randomTrianglesService.getTriangles().length); System.out.println(); System.out.println("Инфо о мин (макс) площади (периметре) прямоугольных треугольников..."); - System.out.println("Мин площадь элемента массива прямоугольных треугольников : " + getMinTriangleByFilter(ninetyAngleTriangles, "square")); - System.out.println("Макс площадь элемента массива прямоугольных треугольников : " + getMaxTriangleByFilter(ninetyAngleTriangles, "square")); - System.out.println("Мин периметр элемента массива прямоугольных треугольников : " + getMinTriangleByFilter(ninetyAngleTriangles, "perimeter")); - System.out.println("Макс периметр элемента массива прямоугольных треугольников : " + getMaxTriangleByFilter(ninetyAngleTriangles, "perimeter")); + System.out.println("Мин площадь элемента массива прямоугольных треугольников : " + ninetyAngleTrianglesService.findMinBySquare().calculateSquare()); + System.out.println("Макс площадь элемента массива прямоугольных треугольников : " + ninetyAngleTrianglesService.findMaxBySquare().calculateSquare()); + System.out.println("Мин периметр элемента массива прямоугольных треугольников : " + ninetyAngleTrianglesService.findMinByPerimeter().calculatePerimeter()); + System.out.println("Макс периметр элемента массива прямоугольных треугольников : " + ninetyAngleTrianglesService.findMaxByPerimeter().calculatePerimeter()); System.out.println(); System.out.println("Инфо о мин (макс) площади (периметре) равнобедренных треугольников..."); - System.out.println("Мин площадь элемента массива равнобедренных треугольников : " + getMinTriangleByFilter(isoscelesTriangles, "square")); - System.out.println("Макс площадь элемента массива равнобедренных треугольников : " + getMaxTriangleByFilter(isoscelesTriangles, "square")); - System.out.println("Мин периметр элемента массива равнобедренных треугольников : " + getMinTriangleByFilter(isoscelesTriangles, "perimeter")); - System.out.println("Макс периметр элемента массива равнобедренных треугольников : " + getMaxTriangleByFilter(isoscelesTriangles, "perimeter")); + System.out.println("Мин площадь элемента массива равнобедренных треугольников : " + isoscelesTrianglesService.findMinBySquare().calculateSquare()); + System.out.println("Макс площадь элемента массива равнобедренных треугольников : " + isoscelesTrianglesService.findMaxBySquare().calculateSquare()); + System.out.println("Мин периметр элемента массива равнобедренных треугольников : " + isoscelesTrianglesService.findMinByPerimeter().calculatePerimeter()); + System.out.println("Макс периметр элемента массива равнобедренных треугольников : " + isoscelesTrianglesService.findMaxByPerimeter().calculatePerimeter()); System.out.println(); System.out.println("Инфо о мин (макс) площади (периметре) равносторонних треугольников..."); - System.out.println("Мин площадь элемента массива равносторонних треугольников : " + getMinTriangleByFilter(equilateralTriangles, "square")); - System.out.println("Макс площадь элемента массива равносторонних треугольников : " + getMaxTriangleByFilter(equilateralTriangles, "square")); - System.out.println("Мин периметр элемента массива равносторонних треугольников : " + getMinTriangleByFilter(equilateralTriangles, "perimeter")); - System.out.println("Макс периметр элемента массива равносторонних треугольников : " + getMaxTriangleByFilter(equilateralTriangles, "perimeter")); + System.out.println("Мин площадь элемента массива равносторонних треугольников : " + equilateralTrianglesService.findMinBySquare().calculateSquare()); + System.out.println("Макс площадь элемента массива равносторонних треугольников : " + equilateralTrianglesService.findMaxBySquare().calculateSquare()); + System.out.println("Мин периметр элемента массива равносторонних треугольников : " + equilateralTrianglesService.findMinByPerimeter().calculatePerimeter()); + System.out.println("Макс периметр элемента массива равносторонних треугольников : " + equilateralTrianglesService.findMaxByPerimeter().calculatePerimeter()); System.out.println(); System.out.println("Инфо о мин (макс) площади (периметре) произвольных треугольников..."); - System.out.println("Мин площадь элемента массива произвольных треугольников : " + getMinTriangleByFilter(randomTriangles, "square")); - System.out.println("Макс площадь элемента массива произвольных треугольников : " + getMaxTriangleByFilter(randomTriangles, "square")); - System.out.println("Мин периметр элемента массива произвольных треугольников : " + getMinTriangleByFilter(randomTriangles, "perimeter")); - System.out.println("Макс периметр элемента массива произвольных треугольников : " + getMaxTriangleByFilter(randomTriangles, "perimeter")); + System.out.println("Мин площадь элемента массива произвольных треугольников : " + randomTrianglesService.findMinBySquare().calculateSquare()); + System.out.println("Макс площадь элемента массива произвольных треугольников : " + randomTrianglesService.findMaxBySquare().calculateSquare()); + System.out.println("Мин периметр элемента массива произвольных треугольников : " + randomTrianglesService.findMinByPerimeter().calculatePerimeter()); + System.out.println("Макс периметр элемента массива произвольных треугольников : " + randomTrianglesService.findMaxByPerimeter().calculatePerimeter()); System.out.println(); } } diff --git a/src/com/javafordev/lesson4/task3/TriangleService.java b/src/com/javafordev/lesson4/task3/TriangleService.java new file mode 100644 index 0000000..b80819b --- /dev/null +++ b/src/com/javafordev/lesson4/task3/TriangleService.java @@ -0,0 +1,117 @@ +package com.javafordev.lesson4.task3; + +import static com.javafordev.lesson4.task3.Validator.removeNullElementsFromArray; +import static com.javafordev.lesson4.task3.Validator.validateInputParameterForArray; + +public class TriangleService { + + private Triangle[] triangles; + + public TriangleService(Triangle[] triangles) { + this.triangles = validateInputParameterForArray(triangles); + } + + public Triangle[] getTriangles() { + return triangles; + } + + public void setTriangles(Triangle[] triangles) { + this.triangles = validateInputParameterForArray(triangles); + } + + public Triangle findMinBySquare() { + Triangle triangleWithMinSquare = triangles[0]; + double minElement = this.triangles[0].calculateSquare(); + for (int i = 0; i < triangles.length; i++) { + if ((triangles[i] != null) && (triangles[i].calculateSquare() < minElement)) { + triangleWithMinSquare = triangles[i]; + } + } + return triangleWithMinSquare; + } + + public Triangle findMaxBySquare() { + Triangle triangleWithMaxSquare = triangles[0]; + double maxElement = this.triangles[0].calculateSquare(); + for (int i = 0; i < triangles.length; i++) { + if ((triangles[i] != null) && (triangles[i].calculateSquare() > maxElement)) { + triangleWithMaxSquare = triangles[i]; + } + } + return triangleWithMaxSquare; + } + + public Triangle findMinByPerimeter() { + Triangle triangleWithMinPerimeter = triangles[0]; + double minElement = this.triangles[0].calculatePerimeter(); + for (int i = 0; i < triangles.length; i++) { + if ((triangles[i] != null) && (triangles[i].calculatePerimeter() < minElement)) { + triangleWithMinPerimeter = triangles[i]; + } + } + return triangleWithMinPerimeter; + } + + public Triangle findMaxByPerimeter() { + Triangle triangleWithMaxPerimeter = triangles[0]; + double maxElement = this.triangles[0].calculatePerimeter(); + for (int i = 0; i < triangles.length; i++) { + if ((triangles[i] != null) && (triangles[i].calculatePerimeter() > maxElement)) { + maxElement = triangles[i].calculatePerimeter(); + triangleWithMaxPerimeter = triangles[i]; + } + } + return triangleWithMaxPerimeter; + } + + public Triangle[] findByNinetyAngle() { + Triangle[] ninetyAngleTriangles = new Triangle[this.triangles.length]; + int j = 0; + for (int i = 0; i < this.triangles.length; i++) { + if ((this.triangles[i] != null) && (this.triangles[i].isTriangleNinetyAngle())) { + ninetyAngleTriangles[j] = this.triangles[i]; + j++; + } + } + return removeNullElementsFromArray(ninetyAngleTriangles); + } + + public Triangle[] findByIsosceles() { + Triangle[] isoscelesTriangles = new Triangle[this.triangles.length]; + int j = 0; + for (int i = 0; i < this.triangles.length; i++) { + if ((this.triangles[i] != null) && (this.triangles[i].isTriangleIsosceles())) { + isoscelesTriangles[j] = this.triangles[i]; + j++; + } + } + return removeNullElementsFromArray(isoscelesTriangles); + } + + public Triangle[] findByEquilateral() { + Triangle[] equilateralTriangles = new Triangle[this.triangles.length]; + int j = 0; + for (int i = 0; i < this.triangles.length; i++) { + if ((this.triangles[i] != null) && (this.triangles[i].isTriangleEquilateral())) { + equilateralTriangles[j] = this.triangles[i]; + j++; + } + } + return removeNullElementsFromArray(equilateralTriangles); + } + + public Triangle[] findByRandom() { + Triangle[] randomTriangles = new Triangle[this.triangles.length]; + int j = 0; + for (int i = 0; i < this.triangles.length; i++) { + if ((this.triangles[i] != null) && (!(this.triangles[i].isTriangleIsosceles())) && !(this.triangles[i].isTriangleIsosceles()) && !(this.triangles[i].isTriangleNinetyAngle())) { + randomTriangles[j] = this.triangles[i]; + j++; + } + } + return removeNullElementsFromArray(randomTriangles); + } + + + +} diff --git a/src/com/javafordev/lesson4/task3/Validator.java b/src/com/javafordev/lesson4/task3/Validator.java new file mode 100644 index 0000000..768abea --- /dev/null +++ b/src/com/javafordev/lesson4/task3/Validator.java @@ -0,0 +1,53 @@ +package com.javafordev.lesson4.task3; + +import static java.lang.StrictMath.abs; + +public class Validator { + + public static double validateSide(double side) { + if (side <= 0) { + System.out.println("Задана сторона треугольника <=0"); + } + return side; + } + + public static Triangle[] validateInputParameterForArray(Triangle[] arrayName) { + for (Triangle element : arrayName) { + if (element == null) { + System.out.println("Входнои массив содержит null элементы!"); + return new Triangle[arrayName.length]; + } + } + return arrayName; + } + + public static boolean isTriangleCorrect(double sideOne, double sideTwo, double sideThree) { + boolean condition1 = getCondition(sideOne, sideTwo, sideThree); + boolean condition2 = getCondition(sideTwo, sideOne, sideThree); + boolean condition3 = getCondition(sideThree, sideOne, sideTwo); + return (condition1 && condition2 && condition3); + } + + public static boolean getCondition(double side1, double side2, double side3) { + return (side1 < (side2 + side3)) && (side1 > (abs(side2 - side3))); + } + + public static Triangle[] removeNullElementsFromArray(Triangle[] arrayName) { + int countOfNotNullElements = 0; + for (Triangle element : arrayName) { + if (element != null) { + countOfNotNullElements++; + } + } + + Triangle[] fitleredArrayWithoutNullElements = new Triangle[countOfNotNullElements]; + int i = 0; + for (Triangle element : arrayName) { + if (element != null) { + fitleredArrayWithoutNullElements[i] = element; + i++; + } + } + return fitleredArrayWithoutNullElements; + } +} From 78f8b317c91d94974ef8a587b4d8f1a62aea6c5e Mon Sep 17 00:00:00 2001 From: Ilya Lychkou Date: Sat, 29 Aug 2020 21:57:09 +0300 Subject: [PATCH 2/5] lesson 4 task2 fixed - add classes FractionService and Validator --- .../javafordev/lesson4/task2/Fraction.java | 53 +++------------- .../lesson4/task2/FractionRunner.java | 25 ++++---- .../lesson4/task2/FractionService.java | 62 +++++++++++++++++++ .../lesson4/task2/RationalFraction.java | 18 ++---- .../javafordev/lesson4/task2/Validator.java | 22 +++++++ 5 files changed, 110 insertions(+), 70 deletions(-) create mode 100644 src/com/javafordev/lesson4/task2/FractionService.java create mode 100644 src/com/javafordev/lesson4/task2/Validator.java diff --git a/src/com/javafordev/lesson4/task2/Fraction.java b/src/com/javafordev/lesson4/task2/Fraction.java index 241df68..92ecb30 100644 --- a/src/com/javafordev/lesson4/task2/Fraction.java +++ b/src/com/javafordev/lesson4/task2/Fraction.java @@ -1,6 +1,6 @@ package com.javafordev.lesson4.task2; -import java.util.Scanner; +import static com.javafordev.lesson4.task2.Validator.validateDenominator; /** * 2)Реализовать методы сложения, вычитания, умножения и деления объектов (для @@ -27,15 +27,15 @@ public void setM(double m) { } public void setN(double n) { - this.n = n; + this.n = validateDenominator(n); } public Fraction() { } public Fraction(double m, double n) { - this.m = m; - this.n = n; + this.m = m; + this.n = n; } @Override @@ -47,64 +47,29 @@ public String toString() { public Fraction add(Fraction fraction) { double denominator = this.getN() * fraction.getN(); - double numerator = denominator / this.getM() + denominator / fraction.getM(); + double numerator = denominator / validateDenominator(this.getM()) + denominator / validateDenominator(fraction.getM()); return new Fraction(numerator, denominator); } public Fraction subtract(Fraction fraction) { double denominator = this.getN() * fraction.getN(); - double numerator = denominator / this.getM() - denominator / fraction.getM(); + double numerator = denominator / validateDenominator(this.getM()) - denominator / validateDenominator(fraction.getM()); return new Fraction(numerator, denominator); } public Fraction multiply(Fraction fraction) { double denominator = this.getN() * fraction.getN(); - double numerator = (denominator / this.getM()) * (denominator / fraction.getM()); + double numerator = (denominator / validateDenominator(this.getM())) * (denominator / validateDenominator(fraction.getM())); return new Fraction(numerator, denominator); } public Fraction divide(Fraction fraction) { double denominator = this.getN() * fraction.getN(); - double numerator = (denominator / this.getM()) / (denominator / fraction.getM()); + double numerator = (denominator / validateDenominator(this.getM())) / (denominator / validateDenominator(fraction.getM())); return new Fraction(numerator, denominator); } - //метод для вводв элементов массива типа Fraction - public static Fraction[] fillArrayOfFractionsFromCommandLine() { - System.out.print("Введите количество элементов массива: "); - Scanner scan = new Scanner(System.in); - int arrayLength = Math.abs(scan.nextInt()); - Fraction[] fractions = new Fraction[arrayLength]; - for (int i = 0; i < arrayLength; i++) { - double randomNumerator = Math.random() * 31 + 10; - double randomDenominator = Math.random() * 74 + 10; - fractions[i] = new Fraction(randomNumerator, randomDenominator); - } - return fractions; - } - - //метод для вывода элементов массива типа Fraction - public static void printArrayOfFractions(Fraction[] fractions) { - for (Fraction element : fractions) { - System.out.println(element.toString()); - } - } - - //метод для изменения массива fractions - public static Fraction[] changeArrayOfFractions(Fraction[] fractions) { - for (int i = 0; i < fractions.length; i = i + 2) { - fractions[i] = fractions[i].add(fractions[i + 1]); - } - return fractions; - } - public double getFraction() { - double fraction=0.0; - try { - fraction = this.getM() / this.getN(); - } catch (Exception e) { - System.out.println("Знаменатель дроби равен 0!"); - } - return fraction; + return this.getM() / validateDenominator(this.getN()); } } diff --git a/src/com/javafordev/lesson4/task2/FractionRunner.java b/src/com/javafordev/lesson4/task2/FractionRunner.java index 8c98a29..38ddc15 100644 --- a/src/com/javafordev/lesson4/task2/FractionRunner.java +++ b/src/com/javafordev/lesson4/task2/FractionRunner.java @@ -14,7 +14,7 @@ public static void main(String[] args) { int numberOfFractions = 7; //инициализация объектов класса Fraction - Fraction fraction1 = new Fraction(3, 9); + Fraction fraction1 = new Fraction(3, 8); Fraction fraction2 = new Fraction(5, -13); System.out.println(); @@ -56,29 +56,28 @@ public static void main(String[] args) { rationalFractions[5] = new RationalFraction(116, 476); rationalFractions[6] = new RationalFraction(6, 11); + FractionService fractionService = new FractionService(fractions); + FractionService rationalFractionService = new FractionService(rationalFractions); + + //вывод на печать массива элементов типа Fraction System.out.println("Вывод на печать массива типа Fraction..."); - printArrayOfFractions(fractions); + fractionService.printArrayOfFractions(); System.out.println(); //вывод на печать массива элементов типа RationalFraction System.out.println("Вывод на печать массива типа RationalFraction..."); - printArrayOfFractions(rationalFractions); + rationalFractionService.printArrayOfFractions(); System.out.println(); //заполнение и вывод на печать массива, заполненного дробями на основании случаиных чисел - System.out.println("Заполнение и вывод на печать массива типа Fraction, заполненного дробями на основании случаиных чисел..."); - printArrayOfFractions(fillArrayOfFractionsFromCommandLine()); - System.out.println(); - - //преобразование исх массива, добавление элементам с четными индексами следующего элемента - Fraction[] fractions1 = fillArrayOfFractionsFromCommandLine(); - System.out.println("Вывод на печать исходного массива..."); - printArrayOfFractions(fractions1); - changeArrayOfFractions(fractions1); + System.out.println("Заполнение и вывод на печать исходного массива типа Fraction, заполненного дробями на основании случаиных чисел..."); + FractionService fractionServiceFromCommandLine = new FractionService(FractionService.fillArrayOfFractionsFromCommandLine()); + fractionServiceFromCommandLine.printArrayOfFractions(); + fractionServiceFromCommandLine.changeArrayOfFractions(); System.out.println(); System.out.println("Вывод на печать измененного массива - добавление элементам с четными индексами следующего элемента..."); - printArrayOfFractions(fractions1); + fractionServiceFromCommandLine.printArrayOfFractions(); } } diff --git a/src/com/javafordev/lesson4/task2/FractionService.java b/src/com/javafordev/lesson4/task2/FractionService.java new file mode 100644 index 0000000..c6fbfd0 --- /dev/null +++ b/src/com/javafordev/lesson4/task2/FractionService.java @@ -0,0 +1,62 @@ +package com.javafordev.lesson4.task2; + +import java.util.Scanner; + +import static com.javafordev.lesson4.task2.Validator.validateInputParameterForArray; + +public class FractionService { + Fraction[] fractions; + + public Fraction[] getFractions() { + return fractions; + } + + public void setFractions(Fraction[] fractions) { + this.fractions = validateInputParameterForArray(fractions); + } + + public FractionService(Fraction[] fractions) { + this.fractions = validateInputParameterForArray(fractions); + } + + + //метод для вводв элементов массива типа Fraction + public static Fraction[] fillArrayOfFractionsFromCommandLine() { + System.out.print("Введите количество элементов массива: "); + Scanner scan = new Scanner(System.in); + int arrayLength = Math.abs(scan.nextInt()); + Fraction[] fractions = new Fraction[arrayLength]; + for (int i = 0; i < arrayLength; i++) { + double randomNumerator = Math.random() * 31 + 10; + double randomDenominator = Math.random() * 74 + 10; + fractions[i] = new Fraction(randomNumerator, randomDenominator); + } + return fractions; + } + + //метод для вывода элементов массива типа Fraction + public void printArrayOfFractions() { + for (Fraction element : this.fractions) { + System.out.println(element); + } + } + + //метод для изменения массива fractions + public Fraction[] changeArrayOfFractions() { + if (this.fractions.length % 2 == 0) { + for (int i = 0; i < this.fractions.length; i = i + 2) { + + this.fractions[i] = this.fractions[i].add(fractions[i + 1]); + } + } else { + for (int i = 0; i < this.fractions.length - 1; i = i + 2) { + + this.fractions[i] = this.fractions[i].add(fractions[i + 1]); + } + } + + return fractions; + } + + +} diff --git a/src/com/javafordev/lesson4/task2/RationalFraction.java b/src/com/javafordev/lesson4/task2/RationalFraction.java index aac88db..06b2d36 100644 --- a/src/com/javafordev/lesson4/task2/RationalFraction.java +++ b/src/com/javafordev/lesson4/task2/RationalFraction.java @@ -1,24 +1,16 @@ package com.javafordev.lesson4.task2; +import static com.javafordev.lesson4.task2.Validator.validateDenominator; + public class RationalFraction extends Fraction { public RationalFraction(double m, double n) { - try { - this.m = m; - this.n = n; - } catch (Exception e) { - System.out.println("Знаменатель рациjлнальнои дроби равен 0!"); - } + this.m = m; + this.n = validateDenominator(n); } public double getRationalFraction() { - double rationalFraction = 0.0; - try { - rationalFraction = (this.getM() + this.getN()) / (this.getM() - this.getN()); - } catch (Exception e) { - System.out.println("Знаменатель дроби равен 0!"); - } - return rationalFraction; + return (this.getM() + this.getN()) / validateDenominator(this.getM() - this.getN()); } @Override diff --git a/src/com/javafordev/lesson4/task2/Validator.java b/src/com/javafordev/lesson4/task2/Validator.java new file mode 100644 index 0000000..38d00bb --- /dev/null +++ b/src/com/javafordev/lesson4/task2/Validator.java @@ -0,0 +1,22 @@ +package com.javafordev.lesson4.task2; + +public class Validator { + + public static double validateDenominator(double denominator) { + + if (denominator == 0) { + throw new IllegalArgumentException("Знаменатель дроби равен 0! Деление на 0 невозможно!"); + } + return denominator; + } + + public static Fraction[] validateInputParameterForArray(Fraction[] arrayName) { + for (Fraction element : arrayName) { + if (element == null) { + System.out.println("Входнои массив содержит null элементы!"); + return new Fraction[arrayName.length]; + } + } + return arrayName; + } +} From dfde71b159285cd15bbdda42da1fc9091e166616 Mon Sep 17 00:00:00 2001 From: Ilya Lychkou Date: Sat, 29 Aug 2020 23:11:11 +0300 Subject: [PATCH 3/5] lesson 4 task1 fixed --- src/com/javafordev/lesson4/task1/Airline.java | 23 +++--- .../lesson4/task1/AirlineRunner.java | 43 +++++------ .../lesson4/task1/AirlineService.java | 77 ++++++++----------- .../javafordev/lesson4/task1/Validator.java | 73 +++++++++++------- 4 files changed, 109 insertions(+), 107 deletions(-) diff --git a/src/com/javafordev/lesson4/task1/Airline.java b/src/com/javafordev/lesson4/task1/Airline.java index 1fd760b..aa6863b 100644 --- a/src/com/javafordev/lesson4/task1/Airline.java +++ b/src/com/javafordev/lesson4/task1/Airline.java @@ -2,8 +2,7 @@ import java.util.Arrays; -import static com.javafordev.lesson4.task1.Validator.validateInputParameterForString; -import static com.javafordev.lesson4.task1.Validator.validateStringParameterForNumber; +import static com.javafordev.lesson4.task1.Validator.*; /** * 1)Создать класс Airline: @@ -53,11 +52,11 @@ public String[] getDaysOfWeek() { } public void setDestination(String destination) { - this.destination = validateInputParameterForString("destination", destination); + this.destination = validateDestination(destination); } public void setNumberOfFlight(int numberOfFlight) { - this.numberOfFlight = validateStringParameterForNumber("numberOfFlight", numberOfFlight); + this.numberOfFlight = validateNumberOfFlight(numberOfFlight); } public void setPlaneType(char planeType) { @@ -65,26 +64,26 @@ public void setPlaneType(char planeType) { } public void setDepartureTimeInHours(int departureTimeInHours) { - this.departureTimeInHours = validateStringParameterForNumber("departureTimeInHours", departureTimeInHours); + this.departureTimeInHours = validateDepartureTimeInHours(departureTimeInHours); } public void setDepartureTimeInMinutes(int departureTimeInMinutes) { - this.departureTimeInMinutes = validateStringParameterForNumber("departureTimeInMinutes", departureTimeInMinutes); + this.departureTimeInMinutes = validateDepartureTimeInMinutes(departureTimeInMinutes); } - public void setDaysOfWeek(String... daysOfWeek) { + public void setDaysOfWeek(String[] daysOfWeek) { this.daysOfWeek = new String[daysOfWeek.length]; for (int i = 0; i < daysOfWeek.length; i++) { this.daysOfWeek[i] = daysOfWeek[i]; } } - public Airline(String destination, int numberOfFlight, char planeType, int departureTimeInHours, int departureTimeInMinutes, String... daysOfWeek) { + public Airline(String destination, int numberOfFlight, char planeType, int departureTimeInHours, int departureTimeInMinutes, String[] daysOfWeek) { - this.destination = validateInputParameterForString("destination", destination); - this.numberOfFlight = validateStringParameterForNumber("numberOfFlight", numberOfFlight); - this.departureTimeInHours = validateStringParameterForNumber("departureTimeInHours", departureTimeInHours); - this.departureTimeInMinutes = validateStringParameterForNumber("departureTimeInMinutes", departureTimeInMinutes); + this.destination = validateDestination(destination); + this.numberOfFlight = validateNumberOfFlight(numberOfFlight); + this.departureTimeInHours = validateDepartureTimeInHours(departureTimeInHours); + this.departureTimeInMinutes = validateDepartureTimeInMinutes(departureTimeInMinutes); this.planeType = planeType; this.daysOfWeek = new String[daysOfWeek.length]; for (int i = 0; i < daysOfWeek.length; i++) { diff --git a/src/com/javafordev/lesson4/task1/AirlineRunner.java b/src/com/javafordev/lesson4/task1/AirlineRunner.java index 99cac93..7e6daf2 100644 --- a/src/com/javafordev/lesson4/task1/AirlineRunner.java +++ b/src/com/javafordev/lesson4/task1/AirlineRunner.java @@ -1,10 +1,5 @@ package com.javafordev.lesson4.task1; -import java.util.Arrays; - -import static com.javafordev.lesson4.task1.AirlineService.printArrayOfAirlines; - - public class AirlineRunner { /** * Тестовые значения: @@ -22,38 +17,42 @@ public class AirlineRunner { public static void main(String[] args) { int numberOfAirlines = 5; - String firstFilterName = "destination"; - String firstFilterValue = "Warsaw"; - String secondFilterName = "dayOfWeek"; - String secondFilterValue = "Thu"; + String firstFilterValue = "Tokyo"; + String secondFilterValue = "Fri"; int departureTimeInHoursToFilter = 5; int departureTimeInMinutesToFilter = 22; //инициализация массива объектов типа Airline Airline[] airlines = new Airline[numberOfAirlines]; - airlines[0] = new Airline("Братислава", 1675, 'A', 22, 10, "Sun", "Mon"); - airlines[1] = new Airline("Warsaw", 1540, 'B', 9, 3, "Thu"); - airlines[2] = new Airline("Ottawa", 1004, 'A', 7, 37, "Mon", "Fri"); - airlines[3] = new Airline("Tokyo", 936, 'C', 16, 21, "Mon", "Tue", "Fri", "Sat"); - airlines[4] = new Airline("Minsk", 2255, 'B', 4, -21, "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); + String[] daysOfWeek0 = {"Sun", "Mon"}; + String[] daysOfWeek1 = {"Thu"}; + String[] daysOfWeek2 = {"Mon", "Fri"}; + String[] daysOfWeek3 = {"Mon", "Tue", "Fri", "Sat"}; + String[] daysOfWeek4 = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + airlines[0] = new Airline("Rome", 1675, 'A', 22, 10, daysOfWeek0); + airlines[1] = new Airline("Warsaw", 1540, 'B', 9, 3, daysOfWeek1); + airlines[2] = new Airline("Ottawa", 1004, 'A', 7, 37, daysOfWeek2); + airlines[3] = new Airline("Tokyo", 936, 'C', 16, 21, daysOfWeek3); + airlines[4] = new Airline("Minsk", 2255, 'B', 4, 22, daysOfWeek4); AirlineService airlineService = new AirlineService(airlines); System.out.println("Вывод элементов массива отфильтрованного по destination"); - Airline[] airlinesFilteredByDestination = airlineService.findAirlinesByFilter(firstFilterName, firstFilterValue); - printArrayOfAirlines(airlinesFilteredByDestination); + Airline[] airlinesFilteredByDestination = airlineService.findAllByDestination(firstFilterValue); + AirlineService airlinesFilteredByDestinationService = new AirlineService(airlinesFilteredByDestination); + airlinesFilteredByDestinationService.printList(); System.out.println(); System.out.println("Вывод элементов массива отфильтрованного по dayOfWeek"); - Airline[] airlinesFilteredByDayOfWeek = airlineService.findAirlinesByFilter(secondFilterName, secondFilterValue); - printArrayOfAirlines(airlinesFilteredByDayOfWeek); + Airline[] airlinesFilteredByDayOfWeek = airlineService.findAllByDayOfWeek(secondFilterValue); + AirlineService airlinesFilteredByDayOfWeekService = new AirlineService(airlinesFilteredByDayOfWeek); + airlinesFilteredByDayOfWeekService.printList(); System.out.println(); - AirlineService airlineServiceFilteredByDayOfWeek = new AirlineService(airlinesFilteredByDayOfWeek); - System.out.println("Вывод элементов массива отфильтрованного по dayOfWeek и departureTime"); - Airline[] airlinesFilteredByDayOfWeekAndDepartureTime = airlineServiceFilteredByDayOfWeek.findAirlinesByDepartureTime(departureTimeInHoursToFilter, departureTimeInMinutesToFilter); - printArrayOfAirlines(airlinesFilteredByDayOfWeekAndDepartureTime); + Airline[] airlinesFilteredByDayOfWeekAndDepartureTime = airlinesFilteredByDayOfWeekService.findAllByDepartureTime(departureTimeInHoursToFilter, departureTimeInMinutesToFilter); + AirlineService airlinesFilteredByDayOfWeekAndDepartureTimeService = new AirlineService(airlinesFilteredByDayOfWeekAndDepartureTime); + airlinesFilteredByDayOfWeekAndDepartureTimeService.printList(); } } diff --git a/src/com/javafordev/lesson4/task1/AirlineService.java b/src/com/javafordev/lesson4/task1/AirlineService.java index 51151bb..9435fef 100644 --- a/src/com/javafordev/lesson4/task1/AirlineService.java +++ b/src/com/javafordev/lesson4/task1/AirlineService.java @@ -2,6 +2,7 @@ import java.util.Arrays; +import static com.javafordev.lesson4.task1.Validator.removeNullElementsFromAirlines; import static com.javafordev.lesson4.task1.Validator.validateInputParameterForArray; public class AirlineService { @@ -27,32 +28,34 @@ public String toString() { '}'; } - //метод для поиска объектов из массива airlines по фильтру - 1) destination, 2)dayOfWeek, 3) можно добавить другои - planeType, numberOfFlight - public Airline[] findAirlinesByFilter(String filterName, String filterValue) { + public Airline[] findAllByDestination(String destination) { Airline[] filteredAirlines = new Airline[this.airlines.length]; - switch (filterName) { - case "destination": - int i = 0; - for (Airline element : this.airlines) { - if (element.getDestination().equals(filterValue)) { - filteredAirlines[i] = element; - i++; - } - } - break; - case "dayOfWeek": - int j = 0; - for (Airline element : this.airlines) { - for (int k = 0; k < element.getDaysOfWeek().length; k++) { - if (element.getDaysOfWeek()[k].equals(filterValue)) { - filteredAirlines[j] = element; - j++; - } - } + int i = 0; + for (Airline element : this.airlines) { + if (element.getDestination().equals(destination)) { + filteredAirlines[i] = element; + i++; + } + } + + Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines); + + if (fitleredAirlinesWithoutNullElements.length == 0) { + System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру"); + } + return fitleredAirlinesWithoutNullElements; + } + + public Airline[] findAllByDayOfWeek(String dayOfWeek) { + Airline[] filteredAirlines = new Airline[this.airlines.length]; + int j = 0; + for (Airline element : this.airlines) { + for (int k = 0; k < element.getDaysOfWeek().length; k++) { + if (element.getDaysOfWeek()[k].equals(dayOfWeek)) { + filteredAirlines[j] = element; + j++; } - break; - default: - System.out.println("Невозможно выполнить фильтрация по заданному параметру!"); + } } Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines); @@ -64,7 +67,7 @@ public Airline[] findAirlinesByFilter(String filterName, String filterValue) { } //метод для поиска объектов из массива airlines по фильтру departureTime - public Airline[] findAirlinesByDepartureTime(int departureTimeInHours, int departureTimeInMinutes) { + public Airline[] findAllByDepartureTime(int departureTimeInHours, int departureTimeInMinutes) { Airline[] filteredAirlines = new Airline[this.airlines.length]; int timeInMinutesToFilter = departureTimeInHours * 60 + departureTimeInMinutes; int i = 0; @@ -83,28 +86,10 @@ public Airline[] findAirlinesByDepartureTime(int departureTimeInHours, int depar return fitleredAirlinesWithoutNullElements; } - public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) { - int countOfNotNullElements = 0; - for (Airline element : airlines) { - if (element != null) { - countOfNotNullElements++; - } - } - - Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements]; - int i = 0; - for (Airline element : airlines) { - if (element != null) { - fitleredAirlinesWithoutNullElements[i] = element; - i++; - } - } - return fitleredAirlinesWithoutNullElements; - } - public static void printArrayOfAirlines(Airline[] airlines) { - for (Airline element : airlines) { - System.out.println(element.toString()); + public void printList() { + for (Airline element : this.airlines) { + System.out.println(element); } } } diff --git a/src/com/javafordev/lesson4/task1/Validator.java b/src/com/javafordev/lesson4/task1/Validator.java index 3745215..1c68b62 100644 --- a/src/com/javafordev/lesson4/task1/Validator.java +++ b/src/com/javafordev/lesson4/task1/Validator.java @@ -2,43 +2,42 @@ public class Validator { - public static String validateInputParameterForString(String parameterName, String parameterValue) { + public static String validateDestination(String destination) { - if (parameterName.equals("destination") && parameterValue.matches("[A-Za-z]+")) { - return parameterValue; + if (destination.matches("[A-Za-z]+")) { + return destination; } else { System.out.println("Для параметра destination задано пустое значение или строка состоит не из латинских символов."); return " "; } } - public static int validateStringParameterForNumber(String parameterName, int parameterValue) { - - switch (parameterName) { - case "numberOfFlight": - if (parameterValue <= 0) { - System.out.println("Для параметра numberOfFlight задано отрицательное или нулевое значение. " - + "Допустимые значения - целые положительные числа"); - } - break; - case "departureTimeInHours": - if (parameterValue < 0 || parameterValue > 23) { - System.out.println("Для параметра departureTimeInHours задано недопустимое значение. " - + "Допустимые значения - целые положительные числа от 0 до 23"); - } - break; - case "departureTimeInMinutes": - if (parameterValue < 0 || parameterValue > 59) { - System.out.println("Для параметра departureTimeInMinutes задано недопустимое значение. " - + "Допустимые значения - целые положительные числа от 0 до 59"); - } - break; - default: - System.out.println("Задано некорректное имя входного параметра!"); + public static int validateNumberOfFlight(int numberOfFlight) { + if (numberOfFlight <= 0) { + System.out.println("Для параметра numberOfFlight задано отрицательное или нулевое значение. " + + "Допустимые значения - целые положительные числа"); } - return parameterValue; + return numberOfFlight; + + } + + public static int validateDepartureTimeInHours(int departureTimeInHours) { + if (departureTimeInHours < 0 || departureTimeInHours > 23) { + System.out.println("Для параметра departureTimeInHours задано недопустимое значение. " + + "Допустимые значения - целые положительные числа от 0 до 23"); + } + return departureTimeInHours; + } + public static int validateDepartureTimeInMinutes(int departureTimeInMinutes) { + if (departureTimeInMinutes < 0 || departureTimeInMinutes > 59) { + System.out.println("Для параметра departureTimeInMinutes задано недопустимое значение. " + + "Допустимые значения - целые положительные числа от 0 до 59"); + } + return departureTimeInMinutes; + + } public static Airline[] validateInputParameterForArray(Airline[] arrayName) { for (Airline element : arrayName) { @@ -49,5 +48,25 @@ public static Airline[] validateInputParameterForArray(Airline[] arrayName) { } return arrayName; } + + + public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) { + int countOfNotNullElements = 0; + for (Airline element : airlines) { + if (element != null) { + countOfNotNullElements++; + } + } + + Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements]; + int i = 0; + for (Airline element : airlines) { + if (element != null) { + fitleredAirlinesWithoutNullElements[i] = element; + i++; + } + } + return fitleredAirlinesWithoutNullElements; + } } From 6876dfbbe161400c354bcad7111bb39f74b0d194 Mon Sep 17 00:00:00 2001 From: Ilya Lychkou Date: Sat, 29 Aug 2020 23:21:18 +0300 Subject: [PATCH 4/5] lesson4 task1 AirlineService fixed --- src/com/javafordev/lesson4/task1/AirlineService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/com/javafordev/lesson4/task1/AirlineService.java b/src/com/javafordev/lesson4/task1/AirlineService.java index 9435fef..b4265a6 100644 --- a/src/com/javafordev/lesson4/task1/AirlineService.java +++ b/src/com/javafordev/lesson4/task1/AirlineService.java @@ -86,7 +86,6 @@ public Airline[] findAllByDepartureTime(int departureTimeInHours, int departureT return fitleredAirlinesWithoutNullElements; } - public void printList() { for (Airline element : this.airlines) { System.out.println(element); From 76791d537266a9711ce0a17c79b210168c87fad4 Mon Sep 17 00:00:00 2001 From: Ilya Lychkou Date: Sun, 6 Sep 2020 20:16:25 +0300 Subject: [PATCH 5/5] lesson4 task1 add ArrayUtils class, move static metod removeNullElementsForArrays there --- .../lesson4/task1/AirlineService.java | 7 +++--- .../javafordev/lesson4/task1/ArrayUtils.java | 23 +++++++++++++++++++ .../javafordev/lesson4/task1/Validator.java | 17 -------------- 3 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 src/com/javafordev/lesson4/task1/ArrayUtils.java diff --git a/src/com/javafordev/lesson4/task1/AirlineService.java b/src/com/javafordev/lesson4/task1/AirlineService.java index b4265a6..5b7c4c0 100644 --- a/src/com/javafordev/lesson4/task1/AirlineService.java +++ b/src/com/javafordev/lesson4/task1/AirlineService.java @@ -2,7 +2,6 @@ import java.util.Arrays; -import static com.javafordev.lesson4.task1.Validator.removeNullElementsFromAirlines; import static com.javafordev.lesson4.task1.Validator.validateInputParameterForArray; public class AirlineService { @@ -38,7 +37,7 @@ public Airline[] findAllByDestination(String destination) { } } - Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines); + Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines); if (fitleredAirlinesWithoutNullElements.length == 0) { System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру"); @@ -58,7 +57,7 @@ public Airline[] findAllByDayOfWeek(String dayOfWeek) { } } - Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines); + Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines); if (fitleredAirlinesWithoutNullElements.length == 0) { System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру"); @@ -78,7 +77,7 @@ public Airline[] findAllByDepartureTime(int departureTimeInHours, int departureT i++; } } - Airline[] fitleredAirlinesWithoutNullElements = removeNullElementsFromAirlines(filteredAirlines); + Airline[] fitleredAirlinesWithoutNullElements = ArrayUtils.removeNullElementsFromAirlines(filteredAirlines); if (fitleredAirlinesWithoutNullElements.length == 0) { System.out.println("Не удалось наити объект массива airlines, которыи соответствует фильтру"); diff --git a/src/com/javafordev/lesson4/task1/ArrayUtils.java b/src/com/javafordev/lesson4/task1/ArrayUtils.java new file mode 100644 index 0000000..6f5daea --- /dev/null +++ b/src/com/javafordev/lesson4/task1/ArrayUtils.java @@ -0,0 +1,23 @@ +package com.javafordev.lesson4.task1; + +public class ArrayUtils { + + public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) { + int countOfNotNullElements = 0; + for (Airline element : airlines) { + if (element != null) { + countOfNotNullElements++; + } + } + + Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements]; + int i = 0; + for (Airline element : airlines) { + if (element != null) { + fitleredAirlinesWithoutNullElements[i] = element; + i++; + } + } + return fitleredAirlinesWithoutNullElements; + } +} diff --git a/src/com/javafordev/lesson4/task1/Validator.java b/src/com/javafordev/lesson4/task1/Validator.java index 1c68b62..a63fc4b 100644 --- a/src/com/javafordev/lesson4/task1/Validator.java +++ b/src/com/javafordev/lesson4/task1/Validator.java @@ -50,23 +50,6 @@ public static Airline[] validateInputParameterForArray(Airline[] arrayName) { } - public static Airline[] removeNullElementsFromAirlines(Airline[] airlines) { - int countOfNotNullElements = 0; - for (Airline element : airlines) { - if (element != null) { - countOfNotNullElements++; - } - } - Airline[] fitleredAirlinesWithoutNullElements = new Airline[countOfNotNullElements]; - int i = 0; - for (Airline element : airlines) { - if (element != null) { - fitleredAirlinesWithoutNullElements[i] = element; - i++; - } - } - return fitleredAirlinesWithoutNullElements; - } }