Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trianglemain;
- class Triangle{
- protected double a,b,c;
- Triangle(double a,double b,double c)throws SidesException,NegativeException{
- if(a<=0||b<=0||c<=0)throw new NegativeException();
- if(test(a,b,c))throw new SidesException();
- this.a=a;
- this.b=b;
- this.c=c;
- }
- Triangle(double ab,double c)throws SidesException,NegativeException{
- if(ab<=0||c<=0)throw new NegativeException();
- if(test(ab,ab,c))throw new SidesException();
- this.a=ab;
- this.b=ab;
- this.c=c;
- }
- Triangle(double a)throws SidesException,NegativeException{
- if(a<=0)throw new NegativeException();
- if(test(a,a,a))throw new SidesException();
- this.a=a;
- this.b=a;
- this.c=a;
- }
- Triangle(Triangle t){
- /*А вот здесь исключения не нужны. Если уж человек умудрится
- создать объект с вырождеными значениями, значит оно ему надо*/
- this.a=t.a;
- this.b=t.b;
- this.c=t.c;
- }
- Triangle()throws SidesException,NegativeException{
- this(1);
- }
- static boolean test(double q,double w,double e){
- if(q+w>e&&q+e>w&&e+w>q) return false;
- return true;
- }
- double p(){//Периметр
- return a+b+c;
- }
- double s(){//Площадь
- double P=p()/2;
- return Math.sqrt(P*(P-a)*(P-b)*(P-c));
- }
- double rr(){//Радиус описанной окружности
- return (a*b*c)/(4*s());
- }
- double r(){//Радиус вписанной окружности
- return ((2*s())/(a+b+c));
- }
- static boolean equals(Triangle one, Triangle two){ //Сравнить треугольники
- if (one.a==two.a){
- if(one.b==two.b){
- if(one.c==two.c){
- return true;
- }
- }
- if(one.b==two.c){
- if(one.c==two.b){
- return true;
- }
- }
- }
- if (one.b==two.a){
- if(one.c==two.b){
- if(one.a==two.c){
- return true;
- }
- }
- if(one.c==two.c){
- if(one.a==two.b){
- return true;
- }
- }
- }
- if (one.c==two.a){
- if(one.a==two.b){
- if(one.b==two.c){
- return true;
- }
- }
- if(one.a==two.c){
- if(one.b==two.b){
- return true;
- }
- }
- }
- return false;
- }
- Triangle copy()throws SidesException,NegativeException{
- return new Triangle(a,b,c);
- }
- String get(){
- return "a="+a+" b="+b+" c="+c;
- }
- public String toString(){
- return "треугольник со сторонами a="+a+", b="+b+", c="+c;
- }
- }
- /*-------------------------------------------*/
- class NegativeException extends Exception{
- public String toString(){
- return "Длина стороны треугольника не может быть отрицательной!";
- }
- }
- class SidesException extends Exception{
- public String toString(){
- return "В невырожденном треугольнике сумма длин двух его сторон больше длины третьей стороны";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment