Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. package shapes;
  2.  
  3. import java.lang.String;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6.  
  7. abstract class Shape {
  8.     public abstract String getName();
  9.     public abstract double getMass();
  10.     public abstract void   Scaling( double coef );
  11. }
  12.  
  13. // Point.java
  14. class Point extends Shape {
  15.  
  16.         private String _name = "Точка";
  17.         private double x, y, m;
  18.  
  19.     public Point(double x, double y) {
  20.         this.x = x;
  21.         this.y = y;
  22.         this.m = 0;
  23.     }
  24.  
  25.         public Point(double x, double y, double m) {
  26.         this.x = x;
  27.         this.y = y;
  28.         this.m = m;
  29.     }
  30.  
  31.     public double getX() {
  32.             return this.x;
  33.     }
  34.  
  35.     public double getY() {
  36.             return this.y;
  37.     }
  38.        
  39.     public void setX(double x) {
  40.             this.x = x;
  41.     }
  42.  
  43.     public void setY(double y) {
  44.             this.x = y;
  45.     }
  46.        
  47.     @Override
  48.     public String getName() {
  49.         return this._name;
  50.     }
  51.  
  52.     @Override
  53.     public double getMass() {
  54.         return m;
  55.     }
  56.  
  57.     @Override
  58.     public void Scaling(double coef) {
  59.             x = x * coef;
  60.             y = y * coef;
  61.         }
  62. }
  63.  
  64. // Line.java
  65. class Line extends Shape {
  66.  
  67.         private String _name = "Линия";
  68.    
  69.         private Point P1, P2;
  70.         private double p;
  71.  
  72.     public Line(Point P1, Point P2, double p) {
  73.         this.P1 = P1;
  74.         this.P2 = P2;
  75.         this.p = p;
  76.     }
  77.  
  78.     @Override
  79.     public String getName() {
  80.         return this._name;
  81.     }
  82.  
  83.     @Override
  84.     public double getMass() {
  85.         return Math.sqrt((P1.getX() - P2.getX()) * (P1.getX() - P2.getX()) + (P1.getY() - P2.getY()) * (P1.getY() - P2.getY())) * p;
  86.     }
  87.  
  88.     @Override
  89.     public void Scaling(double coef) {
  90.             double oldMass = this.getMass();
  91.  
  92.             P1.setX( P1.getX() * coef );
  93.             P1.setY( P1.getY() * coef );
  94.             P2.setX( P2.getX() * coef );
  95.             P2.setY( P2.getY() * coef );
  96.             p = p * oldMass / this.getMass();
  97.         }
  98. }
  99.  
  100. // Ellipse.java
  101. class Ellipse extends Shape {
  102.  
  103.         private String _name = "Эллипс";
  104.  
  105.         private Point P;
  106.         private double rx, ry, p;
  107.  
  108.     public Ellipse(Point P, double rx, double ry, double p) {
  109.         this.P = P;
  110.         this.rx = rx;
  111.         this.ry = ry;
  112.         this.p = p;
  113.     }
  114.  
  115.     @Override
  116.     public String getName() {
  117.         return this._name;
  118.     }
  119.  
  120.     @Override
  121.     public double getMass() {
  122.         return 4 * ( ( Math.PI * rx * ry + ( rx - ry ) * ( rx - ry ) ) / ( rx + ry ) ) * p;
  123.     }
  124.  
  125.     @Override
  126.     public void Scaling(double coef) {
  127.             double oldMass = this.getMass();
  128.  
  129.             P.setX( P.getX() * coef );
  130.             P.setY( P.getY() * coef );
  131.             rx = rx * coef;
  132.             ry = ry * coef;
  133.             p = p * oldMass / this.getMass();
  134.            
  135.         }
  136. }
  137.  
  138. // Triangle.java
  139. class Triangle extends Shape {
  140.        
  141.         private String _name = "Треугольник";
  142.  
  143.         private Point P1, P2, P3;
  144.         private double p;
  145.  
  146.     public Triangle(Point P1, Point P2, Point P3, double p) {
  147.         this.P1 = P1;
  148.         this.P2 = P2;
  149.         this.P3 = P3;
  150.         this.p = p;
  151.     }
  152.        
  153.     @Override
  154.     public String getName() {
  155.         return this._name;
  156.     }
  157.  
  158.     @Override
  159.     public double getMass() {
  160.         return Math.abs((P2.getX() - P1.getX()) * (P3.getY() - P1.getY()) - (P3.getX() - P1.getX()) * (P2.getY() - P1.getY())) / 2 * p;
  161.     }
  162.  
  163.     @Override
  164.     public void Scaling(double coef) {
  165.             double oldMass = this.getMass();
  166.  
  167.             P1.setX( P1.getX() * coef );
  168.             P1.setY( P1.getY() * coef );
  169.             P2.setX( P2.getX() * coef );
  170.             P2.setY( P2.getY() * coef );
  171.             P3.setX( P3.getX() * coef );
  172.             P3.setY( P3.getY() * coef );
  173.             p = p * oldMass / this.getMass();
  174.         }
  175. }
  176.  
  177. // Polygon.java
  178. class Polygon extends Shape {
  179.    
  180.         private String _name = "Многоугольник";
  181.    
  182.         private Point[] Coord;
  183.         private double p;
  184.  
  185.     public Polygon(Point[] Coord, double p) {
  186.                 this.Coord = Coord;
  187.         this.p = p;
  188.     }
  189.        
  190.     @Override
  191.     public String getName() {
  192.         return this._name;
  193.     }
  194.  
  195.     @Override
  196.     public double getMass() {
  197.                 double s = 0;
  198.                 int n = Coord.length;
  199.  
  200.                 for ( int i = 0; i < n; ++i )
  201.                 {
  202.                   s += Math.abs( Coord[i].getX() *  Coord[(i + 1) % n].getY() - Coord[(i + 1) % n].getX() *  Coord[i].getY() );
  203.                 }
  204.                 s *= 0.5;
  205.        
  206.         return s * p;
  207.     }
  208.  
  209.     @Override
  210.     public void Scaling(double coef) {
  211.             double oldMass = this.getMass();
  212.  
  213.             for ( int i = 0; i < Coord.length; ++i ) {
  214.                 Coord[i].setX(Coord[i].getX() * coef);
  215.                 Coord[i].setY(Coord[i].getY() * coef);
  216.             }
  217.             p = p * oldMass / this.getMass();
  218.         }
  219. }
  220.  
  221. class Shapes {
  222.  
  223.     private Shape[] shapes;
  224.  
  225.     public Shapes(Shape[] shapes) {
  226.             this.shapes = shapes;
  227.     }
  228.  
  229.     public double getTotalMass() {
  230.             double m = 0;
  231.             for (int i = 0; i < shapes.length; i++) {
  232.                     m += shapes[i].getMass();
  233.             }
  234.             return m;
  235.     }
  236.  
  237.     public double getAverageMass() {
  238.             return getTotalMass() / shapes.length;
  239.     }
  240.  
  241.     public void Shuffle() {
  242.         Collections.shuffle(Arrays.asList(shapes));
  243.     }
  244.  
  245.     public void Print() {
  246.         for( int i = 0; i < shapes.length; i++ ){
  247.             System.out.println( (i + 1) + ". " + shapes[i].getName() + ",  масса: " + shapes[i].getMass());
  248.         }
  249.     }
  250.    
  251.     public void DeleteMinimal( int num ) {
  252.         for( int i = 0; i < num; i++ ) {
  253.             double minMass = shapes[0].getMass();
  254.             int index = 0;
  255.  
  256.             for( int j = 1; j < shapes.length; j++ ) {
  257.  
  258.                  if ( shapes[j].getMass() < minMass) {
  259.                         minMass = shapes[j].getMass();
  260.                         index = j;
  261.                 }
  262.             }
  263.            
  264.             // Удалить элемент  index  массива  shapes[]
  265.            
  266.         }
  267.     }
  268.    
  269.     public void Scaling(double koef) {
  270.         for (int i = 0; i < shapes.length; i++){
  271.             shapes[i].Scaling( koef );
  272.         }
  273.     }
  274.  
  275.     public static void main(String[] args) {
  276.        
  277.             Shapes ShapeSystem = new Shapes(new Shape[] {
  278.                     new Point(1, 2, 3),
  279.                     new Point(2, 1, 4),
  280.                     new Line(new Point(1, 5), new Point(7, 6), 6),
  281.                     new Triangle(new Point(1, 5), new Point(7, 6), new Point(3, 7), 1),
  282.                     new Polygon(new Point[] {new Point(1, 5), new Point(7, 6), new Point(9, 7)}, 4),
  283.             });
  284.            
  285.             ShapeSystem.Print();
  286.            
  287.             System.out.println("Суммарная масса: " + ShapeSystem.getTotalMass());
  288.  
  289.             ShapeSystem.Shuffle();
  290.             ShapeSystem.Print();
  291.    
  292.             ShapeSystem.Scaling( 1.35 );
  293.  
  294.             System.out.println("Суммарная масса: " + ShapeSystem.getTotalMass());
  295.            
  296.             ShapeSystem.DeleteMinimal( 3 );
  297.             ShapeSystem.Print();
  298.     }
  299. }
Add Comment
Please, Sign In to add comment