sad-ronin-v

TRIANGLE

Nov 18th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.71 KB | None | 0 0
  1. <----MAIN PROGRAMM---->
  2. package vrnkv6;
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class VRNKV6 {
  10.  
  11.     public static void main(String[] args) {
  12.      Scanner scanner = new Scanner(System.in);
  13.      int N = scanner.nextInt();
  14.      ArrayList<Triangle> list = new ArrayList<>();
  15.      Random rand = new Random();
  16.        
  17.       for(int i=0;i<N;i++){
  18.        Triangle trngl = new Triangle(
  19.            
  20.             rand.nextInt(10),
  21.             rand.nextInt(10),
  22.             rand.nextInt(10),
  23.             rand.nextInt(10),
  24.             rand.nextInt(10),
  25.             rand.nextInt(10));
  26.                    
  27.             list.add(trngl);
  28.             }
  29.        
  30.             for(Triangle item:list){
  31.             System.out.println("--------------------------\n");
  32.             System.out.println("Сторона AB = " + item.getSideAB() + " ; " +
  33.                                "Cторона BC = " + item.getSideBC() + " ; " +
  34.                                "Cторона AC = " + item.getSideAC()
  35.                               );
  36.             if (item.getTrnglVerif()== true){
  37.             System.out.println("Это треугольник!");
  38.             System.out.println(item.getAngles());
  39.             System.out.println("Периметр = " + item.getPerimeter());
  40.             System.out.println("Площадь = " + item.getArea());
  41.             }
  42.             else{
  43.             System.out.println("Это не треугольник!");
  44.             }
  45.         }
  46. }
  47. }
  48.  
  49. <----CLASS TRIANGLE---->
  50.  
  51. package vrnkv6;
  52.  
  53.  
  54. public class Triangle {
  55.     private Point A;
  56.     private Point B;
  57.     private Point C;
  58.  
  59.     public Triangle(Point A, Point B, Point C ) {
  60.         this.A = A;
  61.         this.B = B;
  62.         this.C = C;
  63.     }
  64.    
  65.     public Triangle(double x1, double y1, double x2,double y2,double x3,double y3)
  66.     {
  67.     this.A = new Point(x1,y1);
  68.     this.B = new Point(x2,y2);
  69.     this.C = new Point(x3,y3);
  70.     }
  71.    
  72.     public double getSideAB()
  73.     {
  74.         double AB = Math.sqrt(Math.pow((B.getX()- A.getX()),2.0) +
  75.                     Math.pow((B.getY()-A.getY()),2.0));
  76.         return AB;
  77.     }
  78.    
  79.     public double getSideBC()
  80.     {
  81.         double BC = Math.sqrt(Math.pow((C.getX()- B.getX()),2.0) +
  82.                     Math.pow((C.getY()-B.getY()),2.0));
  83.         return BC;
  84.     }
  85.    
  86.     public double getSideAC(){
  87.        
  88.         double AC = Math.sqrt(Math.pow((C.getX()- A.getX()),2.0) +
  89.                     Math.pow((C.getY()-A.getY()),2.0));
  90.         return AC;
  91.     }
  92.    
  93.     public boolean getTrnglVerif(){
  94.        
  95.         if(getSideAB() + getSideBC() > getSideAC() && getSideAB() + getSideAC()>
  96.            getSideBC() && getSideBC() + getSideAC() > getSideAB()){
  97.        
  98.             return true;
  99.         }
  100.         else
  101.        
  102.             return false;
  103.     }
  104.    
  105.     public String getAngles(){
  106.         double angA = Math.acos((Math.pow(getSideAB(),2.0) + Math.pow(getSideAC(),2.0) -
  107.                       Math.pow(getSideBC(),2.0)) / (2.0 * getSideAB() * getSideAC()));
  108.        
  109.         double angB = Math.acos((Math.pow(getSideAB(),2.0) + Math.pow(getSideAC(),2.0) -
  110.                       Math.pow(getSideBC(),2.0)) / (2.0 * getSideAB() * getSideAC()));
  111.        
  112.         double angC = Math.acos((Math.pow(getSideAB(),2.0) + Math.pow(getSideAC(),2.0) -
  113.                       Math.pow(getSideBC(),2.0)) / (2.0 * getSideAB() * getSideAC()));
  114.        
  115.         return "Угол А =" + angA + "; Угол B =" + angB + "; Угол C =" + angC + " ";
  116.     }
  117.    
  118.     public double getPerimeter(){
  119.        
  120.         double P = getSideAB() + getSideBC() + getSideAC();
  121.         return P;
  122.        
  123.     }
  124.    
  125.     public double getArea(){
  126.        
  127.         double p=getPerimeter()/2;//полупериметр
  128.         double S = Math.sqrt(p *(p - getSideAB()) * (p - getSideBC()) *
  129.                    (p - getSideAC()) ); //Формула Герона
  130.         return S;
  131.        
  132.     }
  133.    
  134.    
  135.     /*@Override
  136.     public String toString(){
  137.     return "Point A("+A.getX()+" ; "+ A.getY()+")\nPoint B("+B.getX()+" ; "+ B.getY()+")\nPoint C("+C.getX()+" ; "+ C.getY()+")";
  138.    
  139.     }*/
  140.    
  141.        
  142. }
  143. <----CLASS POINT---->
  144. package vrnkv6;
  145.  
  146.  
  147. public class Point {
  148.     private double x;
  149.     private double y;
  150.  
  151.     public Point(double x, double y) {
  152.         this.x = x;
  153.         this.y = y;
  154.     }
  155.    
  156.      public Point() {
  157.         this.x = 0;
  158.         this.y = 0;
  159.     }
  160.  
  161.     public double getX() {
  162.         return x;
  163.     }
  164.  
  165.     public double getY() {
  166.         return y;
  167.     }
  168.  
  169.     public void setX(double x) {
  170.         this.x = x;
  171.     }
  172.  
  173.     public void setY(double y) {
  174.         this.y = y;
  175.     }
  176. }
Add Comment
Please, Sign In to add comment