Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package shapes;
- import java.lang.String;
- import java.util.Arrays;
- import java.util.Collections;
- abstract class Shape {
- public abstract String getName();
- public abstract double getMass();
- public abstract void Scaling( double coef );
- }
- // Point.java
- class Point extends Shape {
- private String _name = "Точка";
- private double x, y, m;
- public Point(double x, double y) {
- this.x = x;
- this.y = y;
- this.m = 0;
- }
- public Point(double x, double y, double m) {
- this.x = x;
- this.y = y;
- this.m = m;
- }
- public double getX() {
- return this.x;
- }
- public double getY() {
- return this.y;
- }
- public void setX(double x) {
- this.x = x;
- }
- public void setY(double y) {
- this.x = y;
- }
- @Override
- public String getName() {
- return this._name;
- }
- @Override
- public double getMass() {
- return m;
- }
- @Override
- public void Scaling(double coef) {
- x = x * coef;
- y = y * coef;
- }
- }
- // Line.java
- class Line extends Shape {
- private String _name = "Линия";
- private Point P1, P2;
- private double p;
- public Line(Point P1, Point P2, double p) {
- this.P1 = P1;
- this.P2 = P2;
- this.p = p;
- }
- @Override
- public String getName() {
- return this._name;
- }
- @Override
- public double getMass() {
- return Math.sqrt((P1.getX() - P2.getX()) * (P1.getX() - P2.getX()) + (P1.getY() - P2.getY()) * (P1.getY() - P2.getY())) * p;
- }
- @Override
- public void Scaling(double coef) {
- double oldMass = this.getMass();
- P1.setX( P1.getX() * coef );
- P1.setY( P1.getY() * coef );
- P2.setX( P2.getX() * coef );
- P2.setY( P2.getY() * coef );
- p = p * oldMass / this.getMass();
- }
- }
- // Ellipse.java
- class Ellipse extends Shape {
- private String _name = "Эллипс";
- private Point P;
- private double rx, ry, p;
- public Ellipse(Point P, double rx, double ry, double p) {
- this.P = P;
- this.rx = rx;
- this.ry = ry;
- this.p = p;
- }
- @Override
- public String getName() {
- return this._name;
- }
- @Override
- public double getMass() {
- return 4 * ( ( Math.PI * rx * ry + ( rx - ry ) * ( rx - ry ) ) / ( rx + ry ) ) * p;
- }
- @Override
- public void Scaling(double coef) {
- double oldMass = this.getMass();
- P.setX( P.getX() * coef );
- P.setY( P.getY() * coef );
- rx = rx * coef;
- ry = ry * coef;
- p = p * oldMass / this.getMass();
- }
- }
- // Triangle.java
- class Triangle extends Shape {
- private String _name = "Треугольник";
- private Point P1, P2, P3;
- private double p;
- public Triangle(Point P1, Point P2, Point P3, double p) {
- this.P1 = P1;
- this.P2 = P2;
- this.P3 = P3;
- this.p = p;
- }
- @Override
- public String getName() {
- return this._name;
- }
- @Override
- public double getMass() {
- return Math.abs((P2.getX() - P1.getX()) * (P3.getY() - P1.getY()) - (P3.getX() - P1.getX()) * (P2.getY() - P1.getY())) / 2 * p;
- }
- @Override
- public void Scaling(double coef) {
- double oldMass = this.getMass();
- P1.setX( P1.getX() * coef );
- P1.setY( P1.getY() * coef );
- P2.setX( P2.getX() * coef );
- P2.setY( P2.getY() * coef );
- P3.setX( P3.getX() * coef );
- P3.setY( P3.getY() * coef );
- p = p * oldMass / this.getMass();
- }
- }
- // Polygon.java
- class Polygon extends Shape {
- private String _name = "Многоугольник";
- private Point[] Coord;
- private double p;
- public Polygon(Point[] Coord, double p) {
- this.Coord = Coord;
- this.p = p;
- }
- @Override
- public String getName() {
- return this._name;
- }
- @Override
- public double getMass() {
- double s = 0;
- int n = Coord.length;
- for ( int i = 0; i < n; ++i )
- {
- s += Math.abs( Coord[i].getX() * Coord[(i + 1) % n].getY() - Coord[(i + 1) % n].getX() * Coord[i].getY() );
- }
- s *= 0.5;
- return s * p;
- }
- @Override
- public void Scaling(double coef) {
- double oldMass = this.getMass();
- for ( int i = 0; i < Coord.length; ++i ) {
- Coord[i].setX(Coord[i].getX() * coef);
- Coord[i].setY(Coord[i].getY() * coef);
- }
- p = p * oldMass / this.getMass();
- }
- }
- class Shapes {
- private Shape[] shapes;
- public Shapes(Shape[] shapes) {
- this.shapes = shapes;
- }
- public double getTotalMass() {
- double m = 0;
- for (int i = 0; i < shapes.length; i++) {
- m += shapes[i].getMass();
- }
- return m;
- }
- public double getAverageMass() {
- return getTotalMass() / shapes.length;
- }
- public void Shuffle() {
- Collections.shuffle(Arrays.asList(shapes));
- }
- public void Print() {
- for( int i = 0; i < shapes.length; i++ ){
- System.out.println( (i + 1) + ". " + shapes[i].getName() + ", масса: " + shapes[i].getMass());
- }
- }
- public void DeleteMinimal( int num ) {
- for( int i = 0; i < num; i++ ) {
- double minMass = shapes[0].getMass();
- int index = 0;
- for( int j = 1; j < shapes.length; j++ ) {
- if ( shapes[j].getMass() < minMass) {
- minMass = shapes[j].getMass();
- index = j;
- }
- }
- // Удалить элемент index массива shapes[]
- }
- }
- public void Scaling(double koef) {
- for (int i = 0; i < shapes.length; i++){
- shapes[i].Scaling( koef );
- }
- }
- public static void main(String[] args) {
- Shapes ShapeSystem = new Shapes(new Shape[] {
- new Point(1, 2, 3),
- new Point(2, 1, 4),
- new Line(new Point(1, 5), new Point(7, 6), 6),
- new Triangle(new Point(1, 5), new Point(7, 6), new Point(3, 7), 1),
- new Polygon(new Point[] {new Point(1, 5), new Point(7, 6), new Point(9, 7)}, 4),
- });
- ShapeSystem.Print();
- System.out.println("Суммарная масса: " + ShapeSystem.getTotalMass());
- ShapeSystem.Shuffle();
- ShapeSystem.Print();
- ShapeSystem.Scaling( 1.35 );
- System.out.println("Суммарная масса: " + ShapeSystem.getTotalMass());
- ShapeSystem.DeleteMinimal( 3 );
- ShapeSystem.Print();
- }
- }
Add Comment
Please, Sign In to add comment