Last active
May 19, 2016 13:06
-
-
Save theotheo/7af328405fb43b6e98d2382d15d56ec5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RightTriangle { | |
// a и b -- длинны катетов | |
private final double a, b; | |
// конструктор класса | |
RightTriangle(double a, double b) { | |
// проверка на то, что катеты заданы положительными числами | |
if(a <= 0 || b <= 0) { | |
// выдаем ошибку, если любои из катетов задан не положительным числом | |
throw new IllegalArgumentException("a and b should be positive"); | |
} | |
this.a = a; | |
this.b = b; | |
} | |
// методы для получения длин катетов | |
public double getA() { | |
return this.a; | |
} | |
public double getB() { | |
return this.b; | |
} | |
// проверка на подобие | |
public static boolean similarity(RightTriangle first, RightTriangle second) { | |
return (first.getA()/second.getA() == first.getB()/second.getB()); | |
} | |
// вывод на экран информации о треугольнике | |
public String toString() { | |
return "Right triangle with a = " + this.a + " b = " + this.b; | |
} | |
public static void main(String [] args) { | |
System.out.println("Hello there"); | |
// создаем 2 треугольника по заданию | |
RightTriangle first = new RightTriangle(3, 4); | |
RightTriangle second = new RightTriangle(5, 6); | |
// выводим информацию о них на экран | |
System.out.println(first.toString()); | |
System.out.println(second.toString()); | |
// проверяем эти два треугольника на подобие -- они неподобны | |
boolean similarityFirstSecond = RightTriangle.similarity(first, second); | |
if(similarityFirstSecond) { | |
System.out.println("The triangles is similar"); | |
} else { | |
System.out.println("The triangles is not similar"); | |
} | |
// создаем два подобным треугольники и так же выводим на экран и проверяем | |
first = new RightTriangle(3, 4); | |
second = new RightTriangle(6, 8); | |
System.out.println(first.toString()); | |
System.out.println(second.toString()); | |
similarityFirstSecond = RightTriangle.similarity(first, second); | |
if(similarityFirstSecond) { | |
System.out.println("The triangles is similar"); | |
} else { | |
System.out.println("The triangles is not similar"); | |
} | |
// пробуем создаем невозможный треугольник | |
RightTriangle incorrect = new RightTriangle(-10, 6); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment